Class: Decider::Module

Inherits:
Module
  • Object
show all
Defined in:
lib/decider.rb

Defined Under Namespace

Classes: Decide, Evolve, Terminal

Constant Summary collapse

DECIDE_FALLBACK =
proc { [nil, proc {}] }
EVOLVE_FALLBACK =
proc { [nil, proc { state }] }

Instance Method Summary collapse

Constructor Details

#initialize(initial_state:, deciders:, evolutions:, terminal:) ⇒ Module

Returns a new instance of Module.



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/decider.rb', line 60

def initialize(initial_state:, deciders:, evolutions:, terminal:)
  case initial_state
  in Proc
    define_method(:initial_state) do
      initial_state.call
    end
  else
    define_method(:initial_state) do
      initial_state
    end
  end

  define_method(:decide) do |command, state|
    context = Decide.new(command: command, state: state, _events: [])

    deciders.find(DECIDE_FALLBACK) do |args, _|
      case args
      in [Proc => fn]
        context.instance_exec(&fn)
      in [ctype]
        command in ^ctype
      in [ctype, stype]
        [command, state] in [^ctype, ^stype]
      else
        false
      end
    end => [_, handler]

    context.instance_exec(&handler)
    context._events
  end

  define_method(:evolve) do |*args|
    if args.empty?
      ->(state, event) { evolve(state, event) }
    else
      context = Evolve.build(*args)

      evolutions.find(EVOLVE_FALLBACK) do |args, _|
        case args
        in [Proc => fn]
          context.instance_exec(&fn)
        in [etype]
          context_event = context.event
          context_event in ^etype
        in [stype, etype]
          context_state = context.state
          context_event = context.event
          [context_state, context_event] in [^stype, ^etype]
        else
          false
        end
      end => [_, handler]

      context.instance_exec(&handler)
    end
  end

  define_method(:terminal?) do |state|
    context = Terminal.new(state: state)

    context.instance_exec(&terminal)
  end

  define_method(:lmap_on_command) do |fn|
    Decider.lmap_on_command(fn, self)
  end

  define_method(:lmap_on_state) do |fn|
    Decider.lmap_on_state(fn, self)
  end

  define_method(:map) do |fn|
    Decider.map(fn, self)
  end

  define_method(:rmap_on_state) do |fn|
    Decider.rmap_on_state(fn, self)
  end

  define_method(:dimap_on_state) do |fl:, fr:|
    Decider.dimap_on_state(fl, fr, self)
  end

  define_method(:lmap_on_event) do |fn|
    Decider.lmap_on_event(fn, self)
  end

  define_method(:rmap_on_event) do |fn|
    Decider.rmap_on_event(fn, self)
  end

  define_method(:dimap_on_event) do |fl:, fr:|
    Decider.dimap_on_event(fl, fr, self)
  end

  define_method(:many) do
    Decider.many(self)
  end

  define_method(:apply) do |f|
    Decider.apply(self, f)
  end
end