decide.rb 0.3.0 → 0.4.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: da945c90e06dab8eb7c391135319bf4f9e126fd2e1904ac4481aabfc4df8943a
4
- data.tar.gz: 9e70f93d1ee072b555f7a44b8babc2c0fb0bef50df27949d3bd45654b8c05ed8
3
+ metadata.gz: bde47991ca269c4a20a47e138fb8eeb5d1135262a390991bb4b946daa778362d
4
+ data.tar.gz: 5c3712f51013003b099ee4e3f7a568adecd92b0cd5d988c2f4140bb578f88e0c
5
5
  SHA512:
6
- metadata.gz: 67a5f37bf266ce6ca5ca35820cc384fa502d33ca0dae794527d160dc7b247eb7e376c96f6dedfe204c348c25436d64db6c934d9fc407daf8e8421c38e23758bd
7
- data.tar.gz: aea4f6f4de5a4509ce7b908fb04c59e6546f96c01502b762b564e5c8f44b7b235f380bf98631cf2f906d08eeaf210cde02564ce6060b5fbf9677fcbdd24b675f
6
+ metadata.gz: ae016ee97e995c8338221555a0cd444aa600a6ff2e22fa83e53fe2743747dca391392b07d032a3100cce9240978cd0605f86fb355692d0938bf0fd34560c8222
7
+ data.tar.gz: 97ca74eb8729f4fbd1fdfe86e545cb428ef58974cfc7b91645ea246f0db140e0432229e2bec5e686898dd4a365e36419ea8dfd6ca903d643a6524c4db470380d
data/CHANGELOG.md CHANGED
@@ -1,3 +1,11 @@
1
+ # 0.4.1
2
+
3
+ * `define` returns new class, not object
4
+
5
+ # 0.4.0
6
+
7
+ * Accept more data structures for commands and events
8
+
1
9
  # 0.3.0
2
10
 
3
11
  * Rename `Decider.state` to `Decider.initial_state`
data/examples/decide.md CHANGED
@@ -21,3 +21,61 @@ Raise error when using `decide!`:
21
21
  decider.decide! :unknown, decider.initial_state
22
22
  #> raise ArgumentError, Unknown command
23
23
  ```
24
+
25
+ ## Commands
26
+
27
+ Commands can be primitives like symbols:
28
+
29
+ ```ruby
30
+ decider = Decider.define do
31
+ initial_state :initial
32
+
33
+ decide :start do |command, state|
34
+ case state
35
+ in :initial, :stopped
36
+ [:started]
37
+ else
38
+ []
39
+ end
40
+ end
41
+
42
+ decide :stop do |command, state|
43
+ case state
44
+ in :started
45
+ [:stopped]
46
+ else
47
+ []
48
+ end
49
+ end
50
+ end
51
+ ```
52
+
53
+ Or any classes like [Dry::Struct](https://dry-rb.org/gems/dry-struct/) or [Data](https://rubyapi.org/3.3/o/data):
54
+
55
+ ```ruby
56
+ Start = Data.define(:value)
57
+ Stop = Data.define
58
+
59
+ decider = Decider.define do
60
+ initial_state :initial
61
+
62
+ decide Start do |command, state|
63
+ case state
64
+ in :initial, :stopped
65
+ [:started, command.value]
66
+ else
67
+ []
68
+ end
69
+ end
70
+
71
+ decide Stop do |command, state|
72
+ case state
73
+ in :started
74
+ [:stopped]
75
+ else
76
+ []
77
+ end
78
+ end
79
+ end
80
+ ```
81
+
data/examples/evolve.md CHANGED
@@ -21,3 +21,42 @@ Raise error when using `evolve!`:
21
21
  decider.evolve! decider.initial_state, :unknown
22
22
  #> raise ArgumentError, Unknown event
23
23
  ```
24
+
25
+ ## Events
26
+
27
+ Events can be primitives like symbols:
28
+
29
+ ```ruby
30
+ decider = Decider.define do
31
+ initial_state :initial
32
+
33
+ evolve :started do |state, event|
34
+ :started
35
+ end
36
+
37
+ evolve :stopped do |state, event|
38
+ :stopped
39
+ end
40
+ end
41
+ ```
42
+
43
+ Or any classes like [Dry::Struct](https://dry-rb.org/gems/dry-struct/) or [Data](https://rubyapi.org/3.3/o/data):
44
+
45
+ ```ruby
46
+ State = Data.define(:speed)
47
+ Started = Data.define(:speed)
48
+ Stopped = Data.define
49
+
50
+ decider = Decider.define do
51
+ initial_state State.new(speed: 0)
52
+
53
+ evolve Started do |state, event|
54
+ State.new(speed: event.speed)
55
+ end
56
+
57
+ evolve Stopped do |state, event|
58
+ State.new(speed: 0)
59
+ end
60
+ end
61
+ ```
62
+
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Decider
4
- VERSION = "0.3.0"
4
+ VERSION = "0.4.1"
5
5
  end
data/lib/decider.rb CHANGED
@@ -29,7 +29,7 @@ module Decider
29
29
  end
30
30
 
31
31
  class Module < ::Module
32
- def initialize(initial_state:, deciders:, evolvers:, terminal:)
32
+ def initialize(initial_state:, deciders:, evolutions:, terminal:)
33
33
  define_method(:initial_state) do
34
34
  initial_state
35
35
  end
@@ -39,39 +39,43 @@ module Decider
39
39
  end
40
40
 
41
41
  define_method(:events) do
42
- evolvers.keys
42
+ evolutions.keys
43
43
  end
44
44
 
45
45
  define_method(:decide!) do |command, state|
46
- handler = deciders.fetch(command.class) {
47
- raise ArgumentError, "Unknown command: #{command.class}"
48
- }
49
-
50
- handler.call(command, state)
46
+ case deciders.find { |key, _| key === command }
47
+ in [_, handler]
48
+ handler.call(command, state)
49
+ else
50
+ raise ArgumentError, "Unknown command: #{command.inspect}"
51
+ end
51
52
  end
52
53
 
53
54
  define_method(:decide) do |command, state|
54
- handler = deciders.fetch(command.class) {
55
- return []
56
- }
57
-
58
- handler.call(command, state)
55
+ case deciders.find { |key, _| key === command }
56
+ in [_, handler]
57
+ handler.call(command, state)
58
+ else
59
+ []
60
+ end
59
61
  end
60
62
 
61
63
  define_method(:evolve!) do |state, event|
62
- handler = evolvers.fetch(event.class) {
63
- raise ArgumentError, "Unknown event: #{event.class}"
64
- }
65
-
66
- handler.call(state, event)
64
+ case evolutions.find { |key, _| key === event }
65
+ in [_, handler]
66
+ handler.call(state, event)
67
+ else
68
+ raise ArgumentError, "Unknown event: #{event.inspect}"
69
+ end
67
70
  end
68
71
 
69
72
  define_method(:evolve) do |state, event|
70
- handler = evolvers.fetch(event.class) {
71
- return state
72
- }
73
-
74
- handler.call(state, event)
73
+ case evolutions.find { |key, _| key === event }
74
+ in [_, handler]
75
+ handler.call(state, event)
76
+ else
77
+ state
78
+ end
75
79
  end
76
80
 
77
81
  define_method(:terminal?) do |state|
@@ -88,7 +92,7 @@ module Decider
88
92
  def initialize
89
93
  @initial_state = DEFAULT
90
94
  @deciders = {}
91
- @evolvers = {}
95
+ @evolutions = {}
92
96
  @terminal = ->(_state) { false }
93
97
  end
94
98
 
@@ -97,12 +101,12 @@ module Decider
97
101
 
98
102
  raise StateNotDefined if @initial_state == DEFAULT
99
103
 
100
- decider = Object.new
104
+ decider = Class.new
101
105
 
102
106
  @module = Module.new(
103
107
  initial_state: @initial_state,
104
108
  deciders: deciders,
105
- evolvers: evolvers,
109
+ evolutions: evolutions,
106
110
  terminal: terminal
107
111
  )
108
112
 
@@ -113,7 +117,7 @@ module Decider
113
117
 
114
118
  private
115
119
 
116
- attr_reader :deciders, :evolvers, :terminal
120
+ attr_reader :deciders, :evolutions, :terminal
117
121
 
118
122
  def initial_state(state)
119
123
  raise StateAlreadyDefined if @initial_state != DEFAULT
@@ -126,7 +130,7 @@ module Decider
126
130
  end
127
131
 
128
132
  def evolve(event, &block)
129
- evolvers[event] = block
133
+ evolutions[event] = block
130
134
  end
131
135
 
132
136
  def terminal?(&block)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: decide.rb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jan Dudulski