Archived
1
0
Fork 0
This repository has been archived on 2023-03-27. You can view files and clone it, but cannot push or open issues or pull requests.
cli-old/lib/obredux.rb

44 lines
744 B
Ruby
Raw Normal View History

2017-07-27 20:33:32 -04:00
# frozen_string_literal: true
module Obredux
2017-07-27 21:47:18 -04:00
UNDEFINED = Object.new.freeze
2017-07-27 20:33:32 -04:00
class Action; end
2017-07-27 21:47:18 -04:00
class Init < Action; end
class Store
2017-07-27 22:27:42 -04:00
attr_reader :reducer_klass, :state
2017-07-27 21:47:18 -04:00
2017-07-27 22:27:42 -04:00
def initialize(reducer_klass)
@reducer_klass = reducer_klass
@state = UNDEFINED
dispatch Init.new
2017-07-27 21:47:18 -04:00
end
def dispatch(action)
2017-07-27 22:27:42 -04:00
@state = reducer_klass.new(state, action).call
2017-07-27 21:47:18 -04:00
end
end
class Reducer
def initialize(state, action)
@state = state
@action = action
end
def call
@state = initial_state if state.equal? UNDEFINED
reduce
end
private
attr_reader :state, :action
def initial_state
raise NotImplementedError, "#{self.class}#initial_state"
end
end
2017-07-27 20:33:32 -04:00
end