Class: LucidComponent::ClassStoreProxy

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

Instance Method Summary collapse

Constructor Details

#initialize(component_instance) ⇒ ClassStoreProxy

Returns a new instance of ClassStoreProxy.



3
4
5
6
7
# File 'lib/lucid_component/class_store_proxy.rb', line 3

def initialize(component_instance)
  @native_component_instance = component_instance.to_n
  @component_instance = component_instance
  @component_name = component_instance.class.to_s
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/lucid_component/class_store_proxy.rb', line 9

def method_missing(key, *args, &block)
  if `args.length > 0`
    # set class state, simply a dispatch
    action = { type: 'COMPONENT_CLASS_STATE', class: @component_name, name: (`key.endsWith('=')` ? key.chop : key), value: args[0] }
    Isomorfeus.store.dispatch(action)
  else
    # get class state
    # check if we have a component local state value
    if @native_component_instance.JS['context'].JS[:component_class_state].JS[@component_name] &&
      @native_component_instance.JS['context'].JS[:component_class_state].JS[@component_name].JS.hasOwnProperty(key)
      return @native_component_instance.JS['context'].JS[:component_class_state].JS[@component_name].JS[key]
    elsif @component_instance.class.default_class_store_defined && @component_instance.class.class_store.to_h.key?(key)
      # check if a default value was given
      return @component_instance.class.class_store.to_h[key]
    end
    # otherwise return nil
    return nil
  end
end

Instance Method Details

#dispatch(action) ⇒ Object



29
30
31
# File 'lib/lucid_component/class_store_proxy.rb', line 29

def dispatch(action)
  Isomorfeus.store.dispatch(action)
end

#subscribe(&block) ⇒ Object



33
34
35
# File 'lib/lucid_component/class_store_proxy.rb', line 33

def subscribe(&block)
  Isomorfeus.store.subscribe(&block)
end

#unsubscribe(unsubscriber) ⇒ Object



37
38
39
# File 'lib/lucid_component/class_store_proxy.rb', line 37

def unsubscribe(unsubscriber)
  `unsubscriber()`
end