decide.rb 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.standard.yml +3 -0
- data/CHANGELOG.md +3 -0
- data/LICENSE.txt +21 -0
- data/README.md +97 -0
- data/Rakefile +10 -0
- data/lib/decider/version.rb +5 -0
- data/lib/decider.rb +83 -0
- data/sig/decider.rbs +3 -0
- metadata +57 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 7b7afded6cbe7cf6fd572d9a7779875e901dbe81b25292847eea0377a549538c
|
4
|
+
data.tar.gz: 713abc6c3365c06b611a254859bb83fe0a642c26bef2ee51c8c53eb075c47e72
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 969092990e15943086821f2821d44d73e6bff46e77aa865904cf8152dd4cdadc65a979bfa6edd241583f2aee2748fd229b86f5e2a188fd04c9306c1b5209c963
|
7
|
+
data.tar.gz: 29b205b6af520c32e971c6e6a5691f9d4cd4e745a0290fd932ee96217018a69a7429722eb7fa3c66b74e204f10529406075cf6e3e2aa8e89af053139f86c9838
|
data/.standard.yml
ADDED
data/CHANGELOG.md
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2024 Jan Dudulski
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,97 @@
|
|
1
|
+
# Decider
|
2
|
+
|
3
|
+
This gem provides simple DSL for building Functional Event Sourcing Decider in Ruby. To learn more about the pattern read the original [article by Jérémie Chassaing](https://thinkbeforecoding.com/post/2021/12/17/functional-event-sourcing-decider).
|
4
|
+
|
5
|
+
Special credits for [Ismael Celis for inspiration](https://ismaelcelis.com/posts/decide-evolve-react-pattern-in-ruby/).
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
```bash
|
10
|
+
gem install decide.rb
|
11
|
+
```
|
12
|
+
|
13
|
+
or add to Gemfile
|
14
|
+
|
15
|
+
```ruby
|
16
|
+
gem "decide.rb"
|
17
|
+
```
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
```ruby
|
22
|
+
require "decide.rb"
|
23
|
+
|
24
|
+
module Commands
|
25
|
+
Increase = Data.define
|
26
|
+
Decrease = Data.define
|
27
|
+
end
|
28
|
+
|
29
|
+
module Events
|
30
|
+
ValueIncreased = Data.define
|
31
|
+
ValueDecreased = Data.define
|
32
|
+
end
|
33
|
+
|
34
|
+
ValueDecider = Decider.define do
|
35
|
+
MIN = 0
|
36
|
+
MAX = 100
|
37
|
+
|
38
|
+
# define intial state
|
39
|
+
state value: 0 do
|
40
|
+
# you can define custom methods on state
|
41
|
+
def max?
|
42
|
+
value >= MAX
|
43
|
+
end
|
44
|
+
|
45
|
+
def min?
|
46
|
+
value <= MIN
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
# decide command with state
|
51
|
+
decide Commands::Increase do |command, state|
|
52
|
+
# return collection of events
|
53
|
+
if state.max?
|
54
|
+
[]
|
55
|
+
else
|
56
|
+
[Events::ValueIncreased.new]
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
decide Commands::Decrease do |command, state|
|
61
|
+
if state.min?
|
62
|
+
[]
|
63
|
+
else
|
64
|
+
[Events::ValueDecreased.new]
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
# evolve state with events
|
69
|
+
evolve Events::ValueIncreased do |state, event|
|
70
|
+
# return new state
|
71
|
+
state.with(value: state.value + 1)
|
72
|
+
end
|
73
|
+
|
74
|
+
evolve Events::ValueDecreased do |state, event|
|
75
|
+
# state is immutable Data object
|
76
|
+
state.with(value: state.value - 1)
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
state = ValueDecider.initial_state
|
81
|
+
events = ValueDecider.decide(Commands::Increase.new, state)
|
82
|
+
new_state = events.reduce(state) { |state, event| ValueDecider.evolve(state, events)
|
83
|
+
```
|
84
|
+
|
85
|
+
## Development
|
86
|
+
|
87
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
88
|
+
|
89
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
90
|
+
|
91
|
+
## Contributing
|
92
|
+
|
93
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/decider.
|
94
|
+
|
95
|
+
## License
|
96
|
+
|
97
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/lib/decider.rb
ADDED
@@ -0,0 +1,83 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Decider
|
4
|
+
StateAlreadyDefined = Class.new(StandardError)
|
5
|
+
StateNotDefined = Class.new(StandardError)
|
6
|
+
|
7
|
+
class Module < ::Module
|
8
|
+
def initialize(initial_state_args:, deciders:, evolvers:)
|
9
|
+
define_method(:initial_state) do
|
10
|
+
new(**initial_state_args)
|
11
|
+
end
|
12
|
+
|
13
|
+
define_method(:decide) do |command, state|
|
14
|
+
handler = deciders.fetch(command.class) {
|
15
|
+
raise ArgumentError, "Unknown command: #{command.class}"
|
16
|
+
}
|
17
|
+
|
18
|
+
handler.call(command, state)
|
19
|
+
end
|
20
|
+
|
21
|
+
define_method(:evolve) do |state, event|
|
22
|
+
handler = evolvers.fetch(event.class) {
|
23
|
+
raise ArgumentError, "Unknown event: #{event.class}"
|
24
|
+
}
|
25
|
+
|
26
|
+
handler.call(state, event)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
class Builder
|
32
|
+
DEFAULT = Object.new
|
33
|
+
|
34
|
+
attr_reader :module
|
35
|
+
|
36
|
+
def initialize
|
37
|
+
@state = DEFAULT
|
38
|
+
@deciders = {}
|
39
|
+
@evolvers = {}
|
40
|
+
end
|
41
|
+
|
42
|
+
def build(&block)
|
43
|
+
instance_exec(&block) if block_given?
|
44
|
+
|
45
|
+
raise StateNotDefined if @state == DEFAULT
|
46
|
+
|
47
|
+
@module = Module.new(
|
48
|
+
initial_state_args: initial_state_args,
|
49
|
+
deciders: deciders,
|
50
|
+
evolvers: evolvers
|
51
|
+
)
|
52
|
+
|
53
|
+
@state.extend(@module)
|
54
|
+
|
55
|
+
@state
|
56
|
+
end
|
57
|
+
|
58
|
+
private
|
59
|
+
|
60
|
+
attr_reader :initial_state_args, :deciders, :evolvers
|
61
|
+
|
62
|
+
def state(**kwargs, &block)
|
63
|
+
raise StateAlreadyDefined if @state != DEFAULT
|
64
|
+
|
65
|
+
@state = Data.define(*kwargs.keys, &block)
|
66
|
+
@initial_state_args = kwargs
|
67
|
+
end
|
68
|
+
|
69
|
+
def decide(command, &block)
|
70
|
+
deciders[command] = block
|
71
|
+
end
|
72
|
+
|
73
|
+
def evolve(event, &block)
|
74
|
+
evolvers[event] = block
|
75
|
+
end
|
76
|
+
end
|
77
|
+
private_constant :Builder
|
78
|
+
|
79
|
+
def self.define(&block)
|
80
|
+
builder = Builder.new
|
81
|
+
builder.build(&block)
|
82
|
+
end
|
83
|
+
end
|
data/sig/decider.rbs
ADDED
metadata
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: decide.rb
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jan Dudulski
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2024-10-20 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Functional Event Sourcing Decider in Ruby
|
14
|
+
email:
|
15
|
+
- jan@dudulski.pl
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- ".standard.yml"
|
21
|
+
- CHANGELOG.md
|
22
|
+
- LICENSE.txt
|
23
|
+
- README.md
|
24
|
+
- Rakefile
|
25
|
+
- lib/decider.rb
|
26
|
+
- lib/decider/version.rb
|
27
|
+
- sig/decider.rbs
|
28
|
+
homepage: https://github.com/jandudulski/decider.rb
|
29
|
+
licenses:
|
30
|
+
- MIT
|
31
|
+
metadata:
|
32
|
+
allowed_push_host: https://rubygems.org
|
33
|
+
bug_tracker_uri: https://github.com/jandudulski/decider.rb/issues
|
34
|
+
changelog_uri: https://github.com/jandudulski/decider.rb/CHANGELOG.md
|
35
|
+
documentation_uri: https://github.com/jandudulski/decider.rb
|
36
|
+
homepage_uri: https://github.com/jandudulski/decider.rb
|
37
|
+
source_code_uri: https://github.com/jandudulski/decider.rb
|
38
|
+
post_install_message:
|
39
|
+
rdoc_options: []
|
40
|
+
require_paths:
|
41
|
+
- lib
|
42
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: 3.0.0
|
47
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
48
|
+
requirements:
|
49
|
+
- - ">="
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
version: '0'
|
52
|
+
requirements: []
|
53
|
+
rubygems_version: 3.5.16
|
54
|
+
signing_key:
|
55
|
+
specification_version: 4
|
56
|
+
summary: Functional Event Sourcing Decider in Ruby
|
57
|
+
test_files: []
|