Class: ViewComponents::ComponentsBuilder::Component

Inherits:
Object
  • Object
show all
Defined in:
lib/view_components/components_builder.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context, attributes) ⇒ Component

Returns a new instance of Component.



8
9
10
11
12
# File 'lib/view_components/components_builder.rb', line 8

def initialize(context, attributes)
  @context = context
  @attributes = attributes
  @data = Hash.new
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



5
6
7
# File 'lib/view_components/components_builder.rb', line 5

def attributes
  @attributes
end

#contextObject (readonly)

Returns the value of attribute context.



5
6
7
# File 'lib/view_components/components_builder.rb', line 5

def context
  @context
end

Class Method Details

.create!(name, sections, attributes) ⇒ Object



18
19
20
21
22
23
24
25
26
# File 'lib/view_components/components_builder.rb', line 18

def self.create!(name, sections, attributes)
  (self.classes ||= {})[name] = Class.new(self) do
    sections.each do |section|
      define_method section do |*args, &block|
        instance_variable_get("@data")[section] = context.capture(&block)
      end
    end
  end
end

Instance Method Details

#to_hObject



14
15
16
# File 'lib/view_components/components_builder.rb', line 14

def to_h
  @attributes.merge(@data)
end