Class: Alba::Layout

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/alba/layout.rb,
sig/alba/layout.rbs

Overview

Layout serialization

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file:, inline:) ⇒ Layout

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Layout.

Parameters:

  • file (String)

    name of the layout file

  • inline (Proc)

    a proc returning JSON string or a Hash representing JSON



16
17
18
19
20
21
22
23
24
# File 'lib/alba/layout.rb', line 16

def initialize(file:, inline:)
  @body = if file
            check_and_return(file, 'File layout must be a String representing filename', String)
          elsif inline
            check_and_return(inline, 'Inline layout must be a Proc returning a Hash or a String', Proc)
          else
            raise ArgumentError, 'Layout must be either String or Proc'
          end
end

Instance Attribute Details

#serialized_jsonString (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (String)


44
45
46
# File 'lib/alba/layout.rb', line 44

def serialized_json
  @serialized_json
end

Instance Method Details

#check_and_return(obj, message, klass) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

  • obj (Object)
  • message (String)
  • klass (Class)

Returns:

  • (Object)

Raises:

  • (ArgumentError)


46
47
48
49
50
# File 'lib/alba/layout.rb', line 46

def check_and_return(obj, message, klass)
  raise ArgumentError, message unless obj.is_a?(klass)

  obj
end

#encode(hash) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This methods exists here instead of delegation because Alba::Resource#encode is private and it prints warning if we use def_delegators

Parameters:

  • hash (generic_hash)

Returns:

  • (String)


68
69
70
# File 'lib/alba/layout.rb', line 68

def encode(hash)
  @resource.instance_eval { encode(hash) }
end

#objectObject

Returns:

  • (Object)


8
# File 'sig/alba/layout.rbs', line 8

def object: () -> untyped

#paramsgeneric_hash

Returns:

  • (generic_hash)


9
# File 'sig/alba/layout.rbs', line 9

def params: () -> generic_hash

#serializable_hashgeneric_hash

Returns:

  • (generic_hash)


10
# File 'sig/alba/layout.rbs', line 10

def serializable_hash: () -> generic_hash

#serialize(resource:, serialized_json:, binding:) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Serialize within layout

Parameters:

  • resource (Alba::Resource)

    the original resource calling this layout

  • serialized_json (String)

    JSON string for embedding

  • binding (Binding)

    context for serialization



31
32
33
34
35
36
37
38
39
40
# File 'lib/alba/layout.rb', line 31

def serialize(resource:, serialized_json:, binding:)
  @resource = resource
  @serialized_json = serialized_json

  if @body.is_a?(String)
    serialize_within_string_layout(binding)
  else
    serialize_within_inline_layout
  end
end

#serialize_within_inline_layoutString

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (String)


56
57
58
59
60
61
62
63
64
# File 'lib/alba/layout.rb', line 56

def serialize_within_inline_layout
  inline = instance_eval(&@body)
  case inline
  when Hash then encode(inline)
  when String then inline
  else
    raise Alba::Error, 'Inline layout must be a Proc returning a Hash or a String'
  end
end

#serialize_within_string_layout(bnd) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

  • bnd (Binding)

Returns:

  • (String)


52
53
54
# File 'lib/alba/layout.rb', line 52

def serialize_within_string_layout(bnd)
  ERB.new(File.read(@body)).result(bnd)
end

#to_hgeneric_hash

Returns:

  • (generic_hash)


11
# File 'sig/alba/layout.rbs', line 11

def to_h: () -> generic_hash