Class: Rubord::Components::Container

Inherits:
BaseComponent show all
Defined in:
lib/rubord/components/containers/container.rb

Instance Attribute Summary collapse

Attributes inherited from BaseComponent

#type

Instance Method Summary collapse

Constructor Details

#initialize(*components, color: nil) ⇒ Container

Returns a new instance of Container.



8
9
10
11
12
13
# File 'lib/rubord/components/containers/container.rb', line 8

def initialize(*components, color: nil)
  super(17)
  @color = Rubord.Parser.color(color)
  @components = []
  components.compact.each { |c| add(c) }
end

Instance Attribute Details

#colorObject (readonly)

Returns the value of attribute color.



6
7
8
# File 'lib/rubord/components/containers/container.rb', line 6

def color
  @color
end

#componentsObject (readonly)

Returns the value of attribute components.



6
7
8
# File 'lib/rubord/components/containers/container.rb', line 6

def components
  @components
end

Instance Method Details

#add(component) ⇒ Object



15
16
17
18
19
20
21
22
# File 'lib/rubord/components/containers/container.rb', line 15

def add(component)
  unless component.respond_to?(:to_h)
    raise ArgumentError, "Invalid component inside Container: #{component.inspect}"
  end

  @components << component
  self
end

#to_hObject



24
25
26
27
28
29
30
31
32
# File 'lib/rubord/components/containers/container.rb', line 24

def to_h
  payload = {
    type: @type,
    components: @components.map(&:to_h).compact
  }

  payload[:accent_color] = @color if @color
  payload
end