chaintown 0.1.3
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 +7 -0
- data/.gitignore +11 -0
- data/.rspec +3 -0
- data/.travis.yml +5 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +54 -0
- data/LICENSE.txt +21 -0
- data/README.md +132 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/chaintown.gemspec +32 -0
- data/lib/chaintown/callbacks.rb +54 -0
- data/lib/chaintown/chain.rb +83 -0
- data/lib/chaintown/state.rb +16 -0
- data/lib/chaintown/step.rb +12 -0
- data/lib/chaintown/steps.rb +66 -0
- data/lib/chaintown/version.rb +3 -0
- data/lib/chaintown.rb +10 -0
- metadata +133 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 6d79eff11d87d85d01edc1641eb7f1960f2916e0e4b576a0b86328c8891545df
|
|
4
|
+
data.tar.gz: 4564635fa0a80fc6d000204bde45e5ea0a52e23015793f32ad7867ba9326fbdd
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 96260cf8ed3df258dbb589bee05dc306f8df879eed7fc276022852917c33b2eb7776ac4ca8f60ab3dd53c4715c526e5a3d6f12a647d9c6b9a350e71a81077b33
|
|
7
|
+
data.tar.gz: 598ad9bb05c59fb80e7fd54a0d85e9f9adf849115ec7ffd095f55ee8a80f53c984e40311d5817983bd659dfd1272718e4cca017ad0daa229ad7b75919a6d793f
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
|
2
|
+
|
|
3
|
+
## Our Pledge
|
|
4
|
+
|
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
|
10
|
+
orientation.
|
|
11
|
+
|
|
12
|
+
## Our Standards
|
|
13
|
+
|
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
|
15
|
+
include:
|
|
16
|
+
|
|
17
|
+
* Using welcoming and inclusive language
|
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
|
19
|
+
* Gracefully accepting constructive criticism
|
|
20
|
+
* Focusing on what is best for the community
|
|
21
|
+
* Showing empathy towards other community members
|
|
22
|
+
|
|
23
|
+
Examples of unacceptable behavior by participants include:
|
|
24
|
+
|
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
|
26
|
+
advances
|
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
|
28
|
+
* Public or private harassment
|
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
|
30
|
+
address, without explicit permission
|
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
|
32
|
+
professional setting
|
|
33
|
+
|
|
34
|
+
## Our Responsibilities
|
|
35
|
+
|
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
|
38
|
+
response to any instances of unacceptable behavior.
|
|
39
|
+
|
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
|
44
|
+
threatening, offensive, or harmful.
|
|
45
|
+
|
|
46
|
+
## Scope
|
|
47
|
+
|
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
|
49
|
+
when an individual is representing the project or its community. Examples of
|
|
50
|
+
representing a project or community include using an official project e-mail
|
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
|
53
|
+
further defined and clarified by project maintainers.
|
|
54
|
+
|
|
55
|
+
## Enforcement
|
|
56
|
+
|
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
|
58
|
+
reported by contacting the project team at ziolmar@gmail.com. All
|
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
|
63
|
+
|
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
|
66
|
+
members of the project's leadership.
|
|
67
|
+
|
|
68
|
+
## Attribution
|
|
69
|
+
|
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
|
72
|
+
|
|
73
|
+
[homepage]: http://contributor-covenant.org
|
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
PATH
|
|
2
|
+
remote: .
|
|
3
|
+
specs:
|
|
4
|
+
chaintown (0.1.3)
|
|
5
|
+
activesupport (~> 5)
|
|
6
|
+
|
|
7
|
+
GEM
|
|
8
|
+
remote: https://rubygems.org/
|
|
9
|
+
specs:
|
|
10
|
+
activesupport (5.2.2)
|
|
11
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
|
12
|
+
i18n (>= 0.7, < 2)
|
|
13
|
+
minitest (~> 5.1)
|
|
14
|
+
tzinfo (~> 1.1)
|
|
15
|
+
coderay (1.1.2)
|
|
16
|
+
concurrent-ruby (1.1.3)
|
|
17
|
+
diff-lcs (1.3)
|
|
18
|
+
i18n (1.2.0)
|
|
19
|
+
concurrent-ruby (~> 1.0)
|
|
20
|
+
method_source (0.9.2)
|
|
21
|
+
minitest (5.11.3)
|
|
22
|
+
pry (0.12.2)
|
|
23
|
+
coderay (~> 1.1.0)
|
|
24
|
+
method_source (~> 0.9.0)
|
|
25
|
+
rake (10.5.0)
|
|
26
|
+
rspec (3.8.0)
|
|
27
|
+
rspec-core (~> 3.8.0)
|
|
28
|
+
rspec-expectations (~> 3.8.0)
|
|
29
|
+
rspec-mocks (~> 3.8.0)
|
|
30
|
+
rspec-core (3.8.0)
|
|
31
|
+
rspec-support (~> 3.8.0)
|
|
32
|
+
rspec-expectations (3.8.2)
|
|
33
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
|
34
|
+
rspec-support (~> 3.8.0)
|
|
35
|
+
rspec-mocks (3.8.0)
|
|
36
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
|
37
|
+
rspec-support (~> 3.8.0)
|
|
38
|
+
rspec-support (3.8.0)
|
|
39
|
+
thread_safe (0.3.6)
|
|
40
|
+
tzinfo (1.2.5)
|
|
41
|
+
thread_safe (~> 0.1)
|
|
42
|
+
|
|
43
|
+
PLATFORMS
|
|
44
|
+
ruby
|
|
45
|
+
|
|
46
|
+
DEPENDENCIES
|
|
47
|
+
bundler (~> 1.16)
|
|
48
|
+
chaintown!
|
|
49
|
+
pry (~> 0.1)
|
|
50
|
+
rake (~> 10.0)
|
|
51
|
+
rspec (~> 3.0)
|
|
52
|
+
|
|
53
|
+
BUNDLED WITH
|
|
54
|
+
1.16.2
|
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2018 Marcin Ziolkowski
|
|
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,132 @@
|
|
|
1
|
+
# Chaintown
|
|
2
|
+
|
|
3
|
+
This gem provides very simple implementation of pipeline or chain of commands design pattern. If you ever had service class which had to handle complex process and you would like to make the process more explicit and easier to maintain this gem can help you with it.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
Add this line to your application's Gemfile:
|
|
8
|
+
|
|
9
|
+
```ruby
|
|
10
|
+
gem 'chaintown'
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
And then execute:
|
|
14
|
+
|
|
15
|
+
$ bundle
|
|
16
|
+
|
|
17
|
+
Or install it yourself as:
|
|
18
|
+
|
|
19
|
+
$ gem install chaintown
|
|
20
|
+
|
|
21
|
+
## Usage
|
|
22
|
+
|
|
23
|
+
To use the gem first we need to include `Chaintown::Chain` module inside our service.
|
|
24
|
+
|
|
25
|
+
```ruby
|
|
26
|
+
class AnyService
|
|
27
|
+
include Chaintown::Chain
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
The module define constructor which require two arguments. State and params. State is a class which should inherit from `Chaintown::State` and is used to share data between steps in the process. Params is a any object which provide initialization parameters. Param are frozen to be immutable. This prevents from changing it and force to use state object to share data.
|
|
33
|
+
|
|
34
|
+
```ruby
|
|
35
|
+
AnyService.new(Chaintown::State.new, params1: 'value')
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
The Chain module also provide DSL for defining steps. Every step is a method inside the class Inside every method you have access to state and params.
|
|
39
|
+
|
|
40
|
+
```ruby
|
|
41
|
+
step :step1
|
|
42
|
+
step :step2
|
|
43
|
+
|
|
44
|
+
def step1
|
|
45
|
+
puts 'Step 1'
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def step2
|
|
49
|
+
puts 'Step 2'
|
|
50
|
+
end
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
You can simply nest the steps by using `yield` inside your step.
|
|
54
|
+
|
|
55
|
+
```ruby
|
|
56
|
+
step :step1 do
|
|
57
|
+
step :step2
|
|
58
|
+
step :step3
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
def step1
|
|
62
|
+
if state.run_nested_process? # method defined in your own class
|
|
63
|
+
yield
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
There is also a way to run step based on some condition by using `if` argument. In the if block there is an access to all instance variables, state, params and current_step.
|
|
69
|
+
|
|
70
|
+
```ruby
|
|
71
|
+
step :step1, if: proc { params[:run_step_1] }
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
If in any step you set the state `valid` param to false the process will be terminated and no other steps will be called instead the process will be moved to alternative flow defined by failed steps.
|
|
75
|
+
|
|
76
|
+
```ruby
|
|
77
|
+
step :step1
|
|
78
|
+
step :step2
|
|
79
|
+
failed_step :step3
|
|
80
|
+
|
|
81
|
+
def step1
|
|
82
|
+
state.valid = false
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
def step2
|
|
86
|
+
puts 'will not be called'
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
def step3
|
|
90
|
+
puts 'handle invalid state'
|
|
91
|
+
end
|
|
92
|
+
```
|
|
93
|
+
In case you would like to add callbacks to the steps, for example to log some data to logs or send process to monitoring service you can define before, after and around actions.
|
|
94
|
+
|
|
95
|
+
```ruby
|
|
96
|
+
before_step_action :before_action_1, :before_action_2
|
|
97
|
+
after_step_action :after_action_1
|
|
98
|
+
around_step_action :around_action_1
|
|
99
|
+
|
|
100
|
+
def before_action_1
|
|
101
|
+
logger("Start #{current_step_name}")
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
def after_action_1
|
|
105
|
+
logger(state.inspect)
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
def around_step_action_1
|
|
109
|
+
statsd = Datadog::Statsd.new('localhost', 8125)
|
|
110
|
+
statsd.batch do
|
|
111
|
+
yield
|
|
112
|
+
end
|
|
113
|
+
end
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
## Development
|
|
117
|
+
|
|
118
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
|
119
|
+
|
|
120
|
+
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 tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
|
121
|
+
|
|
122
|
+
## Contributing
|
|
123
|
+
|
|
124
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/ziolmar/chaintown. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
|
125
|
+
|
|
126
|
+
## License
|
|
127
|
+
|
|
128
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
|
129
|
+
|
|
130
|
+
## Code of Conduct
|
|
131
|
+
|
|
132
|
+
Everyone interacting in the Chaintown project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/ziolmar/chaintown/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/bin/console
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
|
|
3
|
+
require "bundler/setup"
|
|
4
|
+
require "chaintown"
|
|
5
|
+
|
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
|
8
|
+
|
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
|
10
|
+
# require "pry"
|
|
11
|
+
# Pry.start
|
|
12
|
+
|
|
13
|
+
require "irb"
|
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/chaintown.gemspec
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
|
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
|
+
require "chaintown/version"
|
|
5
|
+
|
|
6
|
+
Gem::Specification.new do |spec|
|
|
7
|
+
spec.name = "chaintown"
|
|
8
|
+
spec.version = Chaintown::VERSION
|
|
9
|
+
spec.authors = ["Marcin Ziolkowski"]
|
|
10
|
+
spec.email = ["ziolmar@gmail.com"]
|
|
11
|
+
|
|
12
|
+
spec.summary = 'Simple implementation of the pipeline or chain of command design pattern.'
|
|
13
|
+
spec.description = 'Simple implementation of the pipeline or chain of command design pattern.'
|
|
14
|
+
spec.homepage = 'https://github.com/ziolmar/chaintown'
|
|
15
|
+
spec.license = "MIT"
|
|
16
|
+
|
|
17
|
+
# Specify which files should be added to the gem when it is released.
|
|
18
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
|
19
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
|
20
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
|
21
|
+
end
|
|
22
|
+
spec.bindir = "exe"
|
|
23
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
|
24
|
+
spec.require_paths = ["lib"]
|
|
25
|
+
|
|
26
|
+
spec.add_development_dependency "bundler", "~> 1.16"
|
|
27
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
|
28
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
|
29
|
+
spec.add_development_dependency "pry", "~> 0.1"
|
|
30
|
+
|
|
31
|
+
spec.add_dependency "activesupport", "~> 5"
|
|
32
|
+
end
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
module Chaintown
|
|
2
|
+
module Callbacks
|
|
3
|
+
extend ActiveSupport::Concern
|
|
4
|
+
|
|
5
|
+
included do
|
|
6
|
+
class_attribute :before_step_actions, default: []
|
|
7
|
+
class_attribute :after_step_actions, default: []
|
|
8
|
+
class_attribute :around_step_actions, default: []
|
|
9
|
+
|
|
10
|
+
delegate :before_step_actions, :after_step_actions, :around_step_actions, to: :class
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
class_methods do
|
|
14
|
+
def before_step_action(*callbacks)
|
|
15
|
+
before_step_actions.concat(callbacks)
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def after_step_action(*callbacks)
|
|
19
|
+
after_step_actions.concat(callbacks)
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def around_step_action(*callbacks)
|
|
23
|
+
around_step_actions.concat(callbacks)
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def run_before_actions
|
|
28
|
+
before_step_actions.each { |callback| send(callback) }
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def run_after_actions
|
|
32
|
+
after_step_actions.each { |callback| send(callback) }
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def with_around_actions
|
|
36
|
+
if around_step_actions.present?
|
|
37
|
+
callbacks = around_step_actions.dup
|
|
38
|
+
around_callback(callbacks.delete_at(0), callbacks) { yield }
|
|
39
|
+
else
|
|
40
|
+
yield
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def around_callback(callback, nested_callbacks, &block)
|
|
45
|
+
send(callback) do
|
|
46
|
+
if nested_callbacks.present?
|
|
47
|
+
around_callback(nested_callbacks.delete_at(0), nested_callbacks, &block)
|
|
48
|
+
else
|
|
49
|
+
block.call
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
end
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
# This module add ability to define chain of steps
|
|
2
|
+
# which will be called in order.
|
|
3
|
+
# Every step is a method in the class which include this module.
|
|
4
|
+
# Every class receive state and immutable params.
|
|
5
|
+
#
|
|
6
|
+
# State is used to keep information which will be shared between
|
|
7
|
+
# steps. State should inherit from Chaintown::State.
|
|
8
|
+
# Steps are calls only when state is valid in other case invocation
|
|
9
|
+
# is stopped and moved to run steps defined as failed.
|
|
10
|
+
#
|
|
11
|
+
# We can add conditions to step by using *if* parameter.
|
|
12
|
+
# We can also nest steps.
|
|
13
|
+
#
|
|
14
|
+
# Example:
|
|
15
|
+
#
|
|
16
|
+
# step :step1
|
|
17
|
+
# step :step2, if: proc { |state, params| params[:run_step_2] }
|
|
18
|
+
# step :step3 do
|
|
19
|
+
# step :step4
|
|
20
|
+
# end
|
|
21
|
+
# failed_step :step5
|
|
22
|
+
#
|
|
23
|
+
module Chaintown
|
|
24
|
+
module Chain
|
|
25
|
+
extend ActiveSupport::Concern
|
|
26
|
+
|
|
27
|
+
included do
|
|
28
|
+
self.extend Chaintown::Steps
|
|
29
|
+
self.include Chaintown::Callbacks
|
|
30
|
+
|
|
31
|
+
attr_reader :state, :params
|
|
32
|
+
attr_accessor :current_step
|
|
33
|
+
delegate :steps, :failed_steps, to: :class
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def initialize(state, params = {})
|
|
37
|
+
@state, @params = state, params.freeze
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def perform
|
|
41
|
+
perform_steps(steps, failed_steps)
|
|
42
|
+
state
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
protected
|
|
46
|
+
|
|
47
|
+
def current_step_name
|
|
48
|
+
current_step&.step_handler.to_s
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
private
|
|
52
|
+
|
|
53
|
+
def perform_steps(steps, failed_steps)
|
|
54
|
+
steps.each do |step|
|
|
55
|
+
break unless state.valid?
|
|
56
|
+
next unless step.if_condition.blank? || self.instance_exec(&step.if_condition)
|
|
57
|
+
perform_step(step)
|
|
58
|
+
end
|
|
59
|
+
unless state.valid?
|
|
60
|
+
failed_steps.each do |step|
|
|
61
|
+
next unless step.if_condition.blank? || self.instance_exec(&step.if_condition)
|
|
62
|
+
perform_step(step)
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
def perform_step(step)
|
|
68
|
+
self.current_step = step # set step to use in callbacks
|
|
69
|
+
run_before_actions
|
|
70
|
+
with_around_actions do
|
|
71
|
+
if step.steps.present?
|
|
72
|
+
method(step.step_handler).call do
|
|
73
|
+
perform_steps(step.steps, step.failed_steps)
|
|
74
|
+
self.current_step = step # set proper step to use in callbacks after processing nested steps
|
|
75
|
+
end
|
|
76
|
+
else
|
|
77
|
+
method(step.step_handler).call
|
|
78
|
+
end
|
|
79
|
+
end
|
|
80
|
+
run_after_actions
|
|
81
|
+
end
|
|
82
|
+
end
|
|
83
|
+
end
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
module Chaintown
|
|
2
|
+
class State
|
|
3
|
+
attr_accessor :result, :valid, :enroll_type, :enroll_count, :schedule
|
|
4
|
+
alias_method :valid?, :valid
|
|
5
|
+
|
|
6
|
+
def initialize
|
|
7
|
+
@valid = true
|
|
8
|
+
@enroll_count = 0
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def failed(result)
|
|
12
|
+
@valid = false
|
|
13
|
+
@result = result
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
end
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
# This module add DLS for defininig steps or failed_steps.
|
|
2
|
+
# All steps are of type Chaintown::Step.
|
|
3
|
+
module Chaintown
|
|
4
|
+
module Steps
|
|
5
|
+
|
|
6
|
+
# When module is included we want to make steps and failed_steps
|
|
7
|
+
# instance variables
|
|
8
|
+
def self.included(base)
|
|
9
|
+
base.class_eval do
|
|
10
|
+
attr_writer :steps, :failed_steps
|
|
11
|
+
|
|
12
|
+
define_method(:steps) do
|
|
13
|
+
instance_variable_get(:@steps) || instance_variable_set(:@steps, [])
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
define_method(:failed_steps) do
|
|
17
|
+
instance_variable_get(:@failed_steps) || instance_variable_set(:@failed_steps, [])
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
# When module is extended we want to make steps and failed steps
|
|
23
|
+
# variabled of specific class type, so every class will have its own
|
|
24
|
+
# list of steps
|
|
25
|
+
def self.extended(base)
|
|
26
|
+
base.class_eval do
|
|
27
|
+
attr_writer :steps, :failed_steps
|
|
28
|
+
|
|
29
|
+
define_singleton_method(:steps) do
|
|
30
|
+
instance_variable_get(:@steps) || instance_variable_set(:@steps, [])
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
define_singleton_method(:failed_steps) do
|
|
34
|
+
instance_variable_get(:@failed_steps) || instance_variable_set(:@failed_steps, [])
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
# DLS method to add new step to the list
|
|
40
|
+
def step(step_handler, **params, &block)
|
|
41
|
+
steps << init_step(step_handler, params, &block)
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
# DSL method to add new step called when process will fail
|
|
45
|
+
def failed_step(step_handler, **params, &block)
|
|
46
|
+
failed_steps << init_step(step_handler, params, &block)
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
# Callback, assure that we add steps from parent class
|
|
50
|
+
def inherited(subclass)
|
|
51
|
+
[:steps, :failed_steps].each do |inheritable_attribute|
|
|
52
|
+
instance_var = "@#{inheritable_attribute}"
|
|
53
|
+
subclass.instance_variable_set(instance_var, instance_variable_get(instance_var).dup || [])
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
private
|
|
58
|
+
|
|
59
|
+
def init_step(step_handler, params = {}, &block)
|
|
60
|
+
Chaintown::Step.new(step_handler).tap do |new_step|
|
|
61
|
+
new_step.if_condition = params[:if] if params.present?
|
|
62
|
+
new_step.instance_eval(&block) if block_given?
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
end
|
data/lib/chaintown.rb
ADDED
metadata
ADDED
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: chaintown
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.3
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Marcin Ziolkowski
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: exe
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2018-12-15 00:00:00.000000000 Z
|
|
12
|
+
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: bundler
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
- - "~>"
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: '1.16'
|
|
20
|
+
type: :development
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - "~>"
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: '1.16'
|
|
27
|
+
- !ruby/object:Gem::Dependency
|
|
28
|
+
name: rake
|
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
|
30
|
+
requirements:
|
|
31
|
+
- - "~>"
|
|
32
|
+
- !ruby/object:Gem::Version
|
|
33
|
+
version: '10.0'
|
|
34
|
+
type: :development
|
|
35
|
+
prerelease: false
|
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
37
|
+
requirements:
|
|
38
|
+
- - "~>"
|
|
39
|
+
- !ruby/object:Gem::Version
|
|
40
|
+
version: '10.0'
|
|
41
|
+
- !ruby/object:Gem::Dependency
|
|
42
|
+
name: rspec
|
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
|
44
|
+
requirements:
|
|
45
|
+
- - "~>"
|
|
46
|
+
- !ruby/object:Gem::Version
|
|
47
|
+
version: '3.0'
|
|
48
|
+
type: :development
|
|
49
|
+
prerelease: false
|
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
51
|
+
requirements:
|
|
52
|
+
- - "~>"
|
|
53
|
+
- !ruby/object:Gem::Version
|
|
54
|
+
version: '3.0'
|
|
55
|
+
- !ruby/object:Gem::Dependency
|
|
56
|
+
name: pry
|
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
|
58
|
+
requirements:
|
|
59
|
+
- - "~>"
|
|
60
|
+
- !ruby/object:Gem::Version
|
|
61
|
+
version: '0.1'
|
|
62
|
+
type: :development
|
|
63
|
+
prerelease: false
|
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
65
|
+
requirements:
|
|
66
|
+
- - "~>"
|
|
67
|
+
- !ruby/object:Gem::Version
|
|
68
|
+
version: '0.1'
|
|
69
|
+
- !ruby/object:Gem::Dependency
|
|
70
|
+
name: activesupport
|
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
|
72
|
+
requirements:
|
|
73
|
+
- - "~>"
|
|
74
|
+
- !ruby/object:Gem::Version
|
|
75
|
+
version: '5'
|
|
76
|
+
type: :runtime
|
|
77
|
+
prerelease: false
|
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
79
|
+
requirements:
|
|
80
|
+
- - "~>"
|
|
81
|
+
- !ruby/object:Gem::Version
|
|
82
|
+
version: '5'
|
|
83
|
+
description: Simple implementation of the pipeline or chain of command design pattern.
|
|
84
|
+
email:
|
|
85
|
+
- ziolmar@gmail.com
|
|
86
|
+
executables: []
|
|
87
|
+
extensions: []
|
|
88
|
+
extra_rdoc_files: []
|
|
89
|
+
files:
|
|
90
|
+
- ".gitignore"
|
|
91
|
+
- ".rspec"
|
|
92
|
+
- ".travis.yml"
|
|
93
|
+
- CODE_OF_CONDUCT.md
|
|
94
|
+
- Gemfile
|
|
95
|
+
- Gemfile.lock
|
|
96
|
+
- LICENSE.txt
|
|
97
|
+
- README.md
|
|
98
|
+
- Rakefile
|
|
99
|
+
- bin/console
|
|
100
|
+
- bin/setup
|
|
101
|
+
- chaintown.gemspec
|
|
102
|
+
- lib/chaintown.rb
|
|
103
|
+
- lib/chaintown/callbacks.rb
|
|
104
|
+
- lib/chaintown/chain.rb
|
|
105
|
+
- lib/chaintown/state.rb
|
|
106
|
+
- lib/chaintown/step.rb
|
|
107
|
+
- lib/chaintown/steps.rb
|
|
108
|
+
- lib/chaintown/version.rb
|
|
109
|
+
homepage: https://github.com/ziolmar/chaintown
|
|
110
|
+
licenses:
|
|
111
|
+
- MIT
|
|
112
|
+
metadata: {}
|
|
113
|
+
post_install_message:
|
|
114
|
+
rdoc_options: []
|
|
115
|
+
require_paths:
|
|
116
|
+
- lib
|
|
117
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
118
|
+
requirements:
|
|
119
|
+
- - ">="
|
|
120
|
+
- !ruby/object:Gem::Version
|
|
121
|
+
version: '0'
|
|
122
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
123
|
+
requirements:
|
|
124
|
+
- - ">="
|
|
125
|
+
- !ruby/object:Gem::Version
|
|
126
|
+
version: '0'
|
|
127
|
+
requirements: []
|
|
128
|
+
rubyforge_project:
|
|
129
|
+
rubygems_version: 2.7.7
|
|
130
|
+
signing_key:
|
|
131
|
+
specification_version: 4
|
|
132
|
+
summary: Simple implementation of the pipeline or chain of command design pattern.
|
|
133
|
+
test_files: []
|