decide.rb 0.7.0 → 0.7.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/decider/reactor.rb +105 -103
- data/lib/decider/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8ac9878aee3455667ca2fa5e7b4226af3588387e8f42a5a67e102b96c9b2e74d
|
4
|
+
data.tar.gz: 3f168429cc20342dca21ab57ad82ab498e3ee2d83623713c3f730966d05e7032
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b8b3240f864d16a1c31eb2575ec700a0bd5674feee8818fe2e6342e2c0807cc8f0c6ac9fc780699d6ccc0c896ce615f829b7d121c7d059c2f3dad1aef192e321
|
7
|
+
data.tar.gz: 3098b0a090dbe5b71eb90cefa2e57127632d40b333aa678c0a0ee6a09bc26e351b3005554bf259ad2011cc4745aef32a158f57cd8d93980efe2c0df33edf3c53
|
data/lib/decider/reactor.rb
CHANGED
@@ -1,141 +1,143 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
module Decider
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
3
|
+
module Decider
|
4
|
+
module Reactor
|
5
|
+
class Module < ::Module
|
6
|
+
REACT_FALLBACK = proc { [nil, proc {}] }
|
7
|
+
|
8
|
+
React = Data.define(:action_result, :_actions) do
|
9
|
+
def issue(*actions)
|
10
|
+
_actions.push(*actions)
|
11
|
+
end
|
10
12
|
end
|
11
|
-
end
|
12
|
-
|
13
|
-
def initialize(reactions:)
|
14
|
-
define_method(:react) do |action_result|
|
15
|
-
context = React.new(action_result: action_result, _actions: [])
|
16
|
-
|
17
|
-
reactions.find(REACT_FALLBACK) do |arg, _|
|
18
|
-
case arg
|
19
|
-
in Proc => fn
|
20
|
-
context.instance_exec(&fn)
|
21
|
-
in artype
|
22
|
-
action_result in ^artype
|
23
|
-
else
|
24
|
-
false
|
25
|
-
end
|
26
|
-
end => [_, handler]
|
27
13
|
|
28
|
-
|
29
|
-
|
30
|
-
|
14
|
+
def initialize(reactions:)
|
15
|
+
define_method(:react) do |action_result|
|
16
|
+
context = React.new(action_result: action_result, _actions: [])
|
17
|
+
|
18
|
+
reactions.find(REACT_FALLBACK) do |arg, _|
|
19
|
+
case arg
|
20
|
+
in Proc => fn
|
21
|
+
context.instance_exec(&fn)
|
22
|
+
in artype
|
23
|
+
action_result in ^artype
|
24
|
+
else
|
25
|
+
false
|
26
|
+
end
|
27
|
+
end => [_, handler]
|
28
|
+
|
29
|
+
context.instance_exec(&handler)
|
30
|
+
context._actions
|
31
|
+
end
|
31
32
|
|
32
|
-
|
33
|
-
|
34
|
-
|
33
|
+
define_method(:lmap_on_action_result) do |fn|
|
34
|
+
Decider::Reactor.lmap_on_action_result(fn, self)
|
35
|
+
end
|
35
36
|
|
36
|
-
|
37
|
-
|
38
|
-
|
37
|
+
define_method(:rmap_on_action) do |fn|
|
38
|
+
Decider::Reactor.rmap_on_action(fn, self)
|
39
|
+
end
|
39
40
|
|
40
|
-
|
41
|
-
|
42
|
-
|
41
|
+
define_method(:map_on_action) do |fn|
|
42
|
+
Decider::Reactor.rmap_on_action(fn, self)
|
43
|
+
end
|
43
44
|
|
44
|
-
|
45
|
-
|
45
|
+
define_method(:combine_with_decider) do |decider|
|
46
|
+
Decider::Reactor.combine_with_decider(self, decider)
|
47
|
+
end
|
46
48
|
end
|
47
49
|
end
|
48
|
-
end
|
49
50
|
|
50
|
-
|
51
|
-
|
51
|
+
class Builder
|
52
|
+
DEFAULT = Object.new
|
52
53
|
|
53
|
-
|
54
|
+
attr_reader :module
|
54
55
|
|
55
|
-
|
56
|
-
|
57
|
-
|
56
|
+
def initialize
|
57
|
+
@reactions = {}
|
58
|
+
end
|
58
59
|
|
59
|
-
|
60
|
-
|
60
|
+
def build(&block)
|
61
|
+
instance_exec(&block) if block_given?
|
61
62
|
|
62
|
-
|
63
|
+
reactor = Class.new
|
63
64
|
|
64
|
-
|
65
|
-
|
66
|
-
|
65
|
+
@module = Module.new(
|
66
|
+
reactions: reactions
|
67
|
+
)
|
67
68
|
|
68
|
-
|
69
|
+
reactor.extend(@module)
|
69
70
|
|
70
|
-
|
71
|
-
|
71
|
+
reactor
|
72
|
+
end
|
72
73
|
|
73
|
-
|
74
|
+
private
|
74
75
|
|
75
|
-
|
76
|
+
attr_reader :reactions
|
76
77
|
|
77
|
-
|
78
|
-
|
78
|
+
def react(arg, &block)
|
79
|
+
reactions[arg] = block
|
80
|
+
end
|
79
81
|
end
|
80
|
-
|
81
|
-
private_constant :Builder
|
82
|
+
private_constant :Builder
|
82
83
|
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
84
|
+
def self.define(&block)
|
85
|
+
builder = Builder.new
|
86
|
+
builder.build(&block)
|
87
|
+
end
|
87
88
|
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
89
|
+
def self.lmap_on_action_result(fn, reactor)
|
90
|
+
define do
|
91
|
+
react proc { true } do
|
92
|
+
reactor.react(fn.call(action_result)).each do |action|
|
93
|
+
issue action
|
94
|
+
end
|
93
95
|
end
|
94
96
|
end
|
95
97
|
end
|
96
|
-
end
|
97
98
|
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
99
|
+
def self.rmap_on_action(fn, reactor)
|
100
|
+
define do
|
101
|
+
react proc { true } do
|
102
|
+
reactor.react(action_result).each do |action|
|
103
|
+
issue fn.call(action)
|
104
|
+
end
|
103
105
|
end
|
104
106
|
end
|
105
107
|
end
|
106
|
-
end
|
107
108
|
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
def self.combine_with_decider(reactor, decider)
|
113
|
-
Decider.define do
|
114
|
-
initial_state decider.initial_state
|
115
|
-
|
116
|
-
decide proc { true } do
|
117
|
-
fn = ->(commands, events, ds) {
|
118
|
-
case commands
|
119
|
-
in []
|
120
|
-
events
|
121
|
-
in [head, *tail]
|
122
|
-
new_events = decider.decide(head, ds)
|
123
|
-
new_commands = new_events.flat_map { |action_result| reactor.react(action_result) }
|
124
|
-
new_state = new_events.reduce(ds, &decider.evolve)
|
125
|
-
|
126
|
-
fn.call(tail + new_commands, events + new_events, new_state)
|
127
|
-
end
|
128
|
-
}
|
109
|
+
def self.map_on_action(fn, reactor)
|
110
|
+
rmap_on_action(fn, reactor)
|
111
|
+
end
|
129
112
|
|
130
|
-
|
131
|
-
|
113
|
+
def self.combine_with_decider(reactor, decider)
|
114
|
+
Decider.define do
|
115
|
+
initial_state decider.initial_state
|
116
|
+
|
117
|
+
decide proc { true } do
|
118
|
+
fn = ->(commands, events, ds) {
|
119
|
+
case commands
|
120
|
+
in []
|
121
|
+
events
|
122
|
+
in [head, *tail]
|
123
|
+
new_events = decider.decide(head, ds)
|
124
|
+
new_commands = new_events.flat_map { |action_result| reactor.react(action_result) }
|
125
|
+
new_state = new_events.reduce(ds, &decider.evolve)
|
126
|
+
|
127
|
+
fn.call(tail + new_commands, events + new_events, new_state)
|
128
|
+
end
|
129
|
+
}
|
130
|
+
|
131
|
+
fn.call([command], [], state).each { |event| emit event }
|
132
|
+
end
|
132
133
|
|
133
|
-
|
134
|
-
|
135
|
-
|
134
|
+
evolve proc { true } do
|
135
|
+
decider.evolve(state, event)
|
136
|
+
end
|
136
137
|
|
137
|
-
|
138
|
-
|
138
|
+
terminal? do
|
139
|
+
decider.terminal?(state)
|
140
|
+
end
|
139
141
|
end
|
140
142
|
end
|
141
143
|
end
|
data/lib/decider/version.rb
CHANGED
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: decide.rb
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.7.
|
4
|
+
version: 0.7.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jan Dudulski
|
8
8
|
bindir: exe
|
9
9
|
cert_chain: []
|
10
|
-
date: 2025-07-
|
10
|
+
date: 2025-07-23 00:00:00.000000000 Z
|
11
11
|
dependencies:
|
12
12
|
- !ruby/object:Gem::Dependency
|
13
13
|
name: concurrent-ruby
|