Class: LucidComponent::InstanceStoreProxy

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

Instance Method Summary collapse

Constructor Details

#initialize(component_instance, access_key = 'state') ⇒ InstanceStoreProxy

Returns a new instance of InstanceStoreProxy.



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

def initialize(component_instance, access_key = 'state')
  @native_component_instance = component_instance.to_n
  @component_instance = component_instance
  @component_object_id = component_instance.object_id.to_s
  @access_key = access_key
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



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

def method_missing(key, *args, &block)
  @native_component_instance.JS.register_used_store_path(['component_state', @component_object_id, key])
  if `args.length > 0`
    # set instance state, simply a dispatch

    action = { type: 'COMPONENT_STATE', object_id: @component_object_id, name: (`key.endsWith('=')` ? key.chop : key), value: args[0] }
    Isomorfeus.store.dispatch(action)

  else
    # get instance state

    if @native_component_instance.JS[@access_key].JS[:isomorfeus_store].JS[:component_state].JS[@component_object_id] &&
      @native_component_instance.JS[@access_key].JS[:isomorfeus_store].JS[:component_state].JS[@component_object_id].JS.hasOwnProperty(key)
      # check if we have a component local state value
      return @native_component_instance.JS[@access_key].JS[:isomorfeus_store].JS[:component_state].JS[@component_object_id].JS[key]
    elsif @component_instance.class.default_instance_store_defined && @component_instance.class.store.to_h.key?(key)
      # check if a default value was given
      return @component_instance.class.store.to_h[key]
    end

    # otherwise return nil
    return nil
  end
end

Instance Method Details

#dispatch(action) ⇒ Object



35
36
37
# File 'lib/lucid_component/instance_store_proxy.rb', line 35

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

#subscribe(&block) ⇒ Object



39
40
41
# File 'lib/lucid_component/instance_store_proxy.rb', line 39

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

#unsubscribe(unsubscriber) ⇒ Object



43
44
45
# File 'lib/lucid_component/instance_store_proxy.rb', line 43

def unsubscribe(unsubscriber)
  `unsubscriber()`
end