Module: Redux::Reducers

Defined in:
lib/redux/reducers.rb

Class Method Summary collapse

Class Method Details

.add_application_reducers_to_storeObject



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/redux/reducers.rb', line 3

def self.add_application_reducers_to_store
  unless @_application_reducers_added
    @_application_reducers_added = true
    app_reducer = Redux.create_reducer do |prev_state, action|
      case action[:type]
      when 'APPLICATION_STATE'
        if action.key?(:set_state)
          action[:set_state]
        else
          new_state = {}.merge!(prev_state) # make a copy of state
          if action.key?(:collected)
            action[:collected].each do |act|
              new_state.merge!(act[:name] => act[:value])
            end
          else
            new_state.merge!(action[:name] => action[:value])
          end
          new_state
        end
      else
        prev_state
      end
    end

    instance_reducer = Redux.create_reducer do |prev_state, action|
      case action[:type]
      when 'INSTANCE_STATE'
        if action.key?(:set_state)
          action[:set_state]
        else
          new_state = {}.merge!(prev_state) # make a copy of state
          if action.key?(:collected)
            action[:collected].each do |act|
              new_state[act[:object_id]] = {} unless new_state.key?(act[:object_id])
              new_state[act[:object_id]].merge!(act[:name] => act[:value])
            end
          else
            new_state[action[:object_id]] = {} unless new_state.key?(action[:object_id])
            new_state[action[:object_id]].merge!(action[:name] => action[:value])
          end
          new_state
        end
      else
        prev_state
      end
    end

    class_reducer = Redux.create_reducer do |prev_state, action|
      case action[:type]
      when 'CLASS_STATE'
        if action.key?(:set_state)
          action[:set_state]
        else
          new_state = {}.merge!(prev_state) # make a copy of state
          if action.key?(:collected)
            action[:collected].each do |act|
              new_state[act[:class]] = {} unless new_state.key?(act[:class])
              new_state[act[:class]].merge!(act[:name] => act[:value])
            end
          else
            new_state[action[:class]] = {} unless new_state.key?(action[:class])
            new_state[action[:class]].merge!(action[:name] => action[:value])
          end
          new_state
        end
      else
        prev_state
      end
    end
    Redux::Store.preloaded_state_merge!(application_state: {}, instance_state: {}, class_state: {})
    Redux::Store.add_reducers(application_state: app_reducer, instance_state: instance_reducer, class_state: class_reducer)
  end
end