Class: AmberComponent::TypedContent
- Inherits:
-
Object
- Object
- AmberComponent::TypedContent
- Defined in:
- lib/amber_component/typed_content.rb
Overview
Contains the content and type of an asset.
Instance Attribute Summary collapse
- #content ⇒ String, ... readonly
- #type ⇒ Symbol? readonly
Class Method Summary collapse
- .wrap(val) ⇒ self (also: [])
Instance Method Summary collapse
-
#initialize(type:, content:) ⇒ TypedContent
constructor
A new instance of TypedContent.
-
#to_s ⇒ String
(also: #string_content)
Stringified content.
Constructor Details
#initialize(type:, content:) ⇒ TypedContent
Returns a new instance of TypedContent.
24 25 26 27 28 |
# File 'lib/amber_component/typed_content.rb', line 24 def initialize(type:, content:) @type = type&.to_sym @content = content freeze end |
Instance Attribute Details
#content ⇒ String, ... (readonly)
33 34 35 |
# File 'lib/amber_component/typed_content.rb', line 33 def content @content end |
#type ⇒ Symbol? (readonly)
31 32 33 |
# File 'lib/amber_component/typed_content.rb', line 31 def type @type end |
Class Method Details
.wrap(val) ⇒ self Also known as: []
9 10 11 12 13 14 15 16 17 |
# File 'lib/amber_component/typed_content.rb', line 9 def wrap(val) return val if val.is_a?(self) raise InvalidTypeError, <<~MSG unless val.respond_to?(:[]) `TypedContent` should be a `Hash` or `#{self}` but was `#{val.class}` (#{val.inspect}) MSG new(type: val[:type], content: val[:content]) end |
Instance Method Details
#to_s ⇒ String Also known as: string_content
Stringified content.
38 39 40 41 42 |
# File 'lib/amber_component/typed_content.rb', line 38 def to_s return @content.call.to_s if @content.is_a?(::Proc) @content.to_s end |