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:

  • type (Symbol, String, nil)
  • content (String, Proc, nil)


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:

  • (String, Proc, nil)


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

def content
  @content
end

#typeSymbol? (readonly)

Returns:

  • (Symbol, nil)


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:

  • val (Hash, self)

Returns:

  • (self)

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, <<~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_sString Also known as: string_content

Stringified content.

Returns:

  • (String)


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