Class: LucidComponent::ComponentClassStoreDefaults

Inherits:
Object
  • Object
show all
Defined in:
lib/lucid_component/component_class_store_defaults.rb

Instance Method Summary collapse

Constructor Details

#initialize(state, component_name) ⇒ ComponentClassStoreDefaults

Returns a new instance of ComponentClassStoreDefaults.



3
4
5
6
7
8
9
10
11
# File 'lib/lucid_component/component_class_store_defaults.rb', line 3

def initialize(state, component_name)
  @state = state
  @component_name = component_name
  if @state.isomorfeus_store
    @state.isomorfeus_store.merge!(component_class_state: { @component_name => {} })
  else
    @state.isomorfeus_store = { component_class_state: { @component_name => {} } }
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(key, *args, &block) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/lucid_component/component_class_store_defaults.rb', line 13

def method_missing(key, *args, &block)
  if `args.length > 0`
    # set initial class state
    key = key.chop if `key.endsWith('=')`
    @state.isomorfeus_store[:component_class_state][@component_name][key] = args[0]
    current_state = Isomorfeus.store.get_state
    if !(current_state[:component_class_state].key?(@component_name) && current_state[:component_class_state][@component_name].key?(key))
      Isomorfeus.store.dispatch(type: 'COMPONENT_CLASS_STATE', class: @component_name, name: key, value: args[0])
    end
  else
    # get class state

    # check if we have a component local state value

    if @state.isomorfeus_store[:component_class_state][@component_name].key?(key)
      return @state.isomorfeus_store[:component_class_state][@component_name][key]
    end
  end
  nil
end

Instance Method Details

#to_hObject



34
35
36
# File 'lib/lucid_component/component_class_store_defaults.rb', line 34

def to_h
  @state.isomorfeus_store[:component_class_state][@component_name]
end