class Rudux::Reducer

Public Class Methods

base() click to toggle source
# File lib/rudux/reducer.rb, line 44
def self.base
  Rudux::Combined.new({})
end
reduce(states, action) click to toggle source
# File lib/rudux/reducer.rb, line 6
def self.reduce states, action
  if states.is_a? Array
    reduce_array_of_states(states, action)
  else
    reduce_single_state(states, action)
  end
end
reduce_array_of_states(states, action) click to toggle source
# File lib/rudux/reducer.rb, line 14
def self.reduce_array_of_states states, action
  states.map do |state|
    reduce_single_state(state, action)
  end
end
reduce_single_state(state, action) click to toggle source
# File lib/rudux/reducer.rb, line 20
def self.reduce_single_state state, action
  base = self.base.reduce(state, action)
  unless action.respond_to? :to_sym
    puts "Action #{action.inspect} received to_sym"
  end
  if self.respond_to? action.to_sym
    this = self.send(action.to_sym, state, action)
  else
    this = {}
  end
  merged = base.merge!(this)
  if merged.empty?
    state
  else
    if state.respond_to? :copy
      # oop style
      state.copy(merged)
    else
      # hash style
      merged
    end
  end
end