Class: AmberComponent::TypedContent

Inherits:
Object
  • Object
show all
Defined in:
lib/amber_component/typed_content.rb

Overview

Contains the content and type of an asset.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type:, content:) ⇒ TypedContent

Returns a new instance of TypedContent.

Parameters:



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

#contentString, ... (readonly)

Returns:



33
34
35
# File 'lib/amber_component/typed_content.rb', line 33

def content
  @content
end

#typeSymbol? (readonly)

Returns:



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: []

Parameters:

Returns:

Raises:



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, "    `TypedContent` should be a `Hash` or `\#{self}` but was `\#{val.class}` (\#{val.inspect})\n  MSG\n\n  new(type: val[:type], content: val[:content])\nend\n" unless val.respond_to?(:[])

Instance Method Details

#to_sString Also known as: string_content

Stringified content.

Returns:



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