Class: Alba::Layout

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

Overview

Layout serialization

Class Method 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

  • file: (String, nil)
  • inline: (Proc, nil)


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

Class Method Details

.def_delegators ⇒ void

This method returns an undefined value.

Parameters:

  • (Symbol)
  • (Symbol)


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

def self.def_delegators: (Symbol, *Symbol) -> void

Instance Method Details

#object ⇒ Object

Returns:

  • (Object)


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

def object: () -> untyped

#params ⇒ generic_hash

Returns:

  • (generic_hash)


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

def params: () -> generic_hash

#serializable_hash ⇒ generic_hash

Returns:

  • (generic_hash)


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

def serializable_hash: () -> generic_hash

#serialize(resource:, serialized_json:, binding:) ⇒ 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.

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

  • resource: (Alba::Resource::_InstanceMethods)
  • serialized_json: (String)
  • binding: (Binding)

Returns:

  • (String)


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

#to_h ⇒ generic_hash

Returns:

  • (generic_hash)


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

def to_h: () -> generic_hash