class Rudux::HashReducer

Public Class Methods

new(reducer) click to toggle source
# File lib/rudux/hash_reducer.rb, line 6
def initialize reducer
  @subreducer = reducer
end

Public Instance Methods

reduce(state, action) click to toggle source
# File lib/rudux/hash_reducer.rb, line 10
def reduce state, action
  unless state.is_a? Hash
    raise 'HashReducer not given hash'
  end

  state.keys.map do |key|
    result = @subreducer.reduce(state[key], action)

    if result.respond_to? :[]
      Hash[result[:id], result]
    else
      Hash[result.id, result]
    end
  end.reduce(&:merge!)
end