Class: Alba::Layout
- Inherits:
-
Object
- Object
- Alba::Layout
- Extended by:
- Forwardable
- Defined in:
- lib/alba/layout.rb,
sig/alba/layout.rbs
Overview
Layout serialization
Instance Attribute Summary collapse
- #serialized_json ⇒ String readonly private
Instance Method Summary collapse
- #check_and_return(obj, message, klass) ⇒ Object private
-
#encode(hash) ⇒ String
private
This methods exists here instead of delegation because
Alba::Resource#encodeis private and it prints warning if we usedef_delegators. -
#initialize(file:, inline:) ⇒ Layout
constructor
private
A new instance of Layout.
- #object ⇒ Object
- #params ⇒ generic_hash
- #serializable_hash ⇒ generic_hash
-
#serialize(resource:, serialized_json:, binding:) ⇒ Object
private
Serialize within layout.
- #serialize_within_inline_layout ⇒ String private
- #serialize_within_string_layout(bnd) ⇒ String private
- #to_h ⇒ generic_hash
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.
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_json ⇒ String (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.
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.
46 47 48 49 50 |
# File 'lib/alba/layout.rb', line 46 def check_and_return(obj, , klass) raise ArgumentError, 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
68 69 70 |
# File 'lib/alba/layout.rb', line 68 def encode(hash) @resource.instance_eval { encode(hash) } end |
#object ⇒ Object
8 |
# File 'sig/alba/layout.rbs', line 8
def object: () -> untyped
|
#params ⇒ generic_hash
9 |
# File 'sig/alba/layout.rbs', line 9
def params: () -> generic_hash
|
#serializable_hash ⇒ 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
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_layout ⇒ 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.
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.
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_h ⇒ generic_hash
11 |
# File 'sig/alba/layout.rbs', line 11
def to_h: () -> generic_hash
|