Class: Maglev::BaseComponent

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Inspector
Defined in:
app/components/maglev/base_component.rb

Direct Known Subclasses

BlockComponent, PageComponent, SectionComponent

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Inspector

#inspect

Instance Attribute Details

#view_contextObject

Returns the value of attribute view_context.



10
11
12
# File 'app/components/maglev/base_component.rb', line 10

def view_context
  @view_context
end

Instance Method Details

#build(component_class, attributes) ⇒ Object



12
13
14
15
16
# File 'app/components/maglev/base_component.rb', line 12

def build(component_class, attributes)
  component_class.new(**attributes).tap do |component|
    component.view_context = view_context
  end
end

#build_content(scope, content, setting) ⇒ Object



40
41
42
# File 'app/components/maglev/base_component.rb', line 40

def build_content(scope, content, setting)
  Maglev::Content::Builder.build(scope, content, setting)
end

#build_settings_map(settings) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
# File 'app/components/maglev/base_component.rb', line 28

def build_settings_map(settings)
  definition.settings.inject({}) do |memo, setting_def|
    memo.merge(
      setting_def.id.to_sym => build_content(
        self,
        find_setting_value(settings, setting_def),
        setting_def
      )
    )
  end
end

#settings_proxy(map) ⇒ Object

Useful helper to avoid manipulating hashes eg.: <%= section.settings.body %> instead of <%= section.settings %>



20
21
22
23
24
25
26
# File 'app/components/maglev/base_component.rb', line 20

def settings_proxy(map)
  settings_keys = map.keys
  return nil if settings_keys.blank?

  settings_struct = Struct.new(*settings_keys)
  settings_struct.new(*map.values)
end