guided_interactor 1.0.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 +18 -0
- data/.rspec +4 -0
- data/.rubocop.yml +745 -0
- data/.travis.yml +7 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +35 -0
- data/LICENSE.txt +21 -0
- data/README.md +83 -0
- data/Rakefile +6 -0
- data/VERSION +1 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/guided_interactor.gemspec +27 -0
- data/lib/guided_interactor.rb +72 -0
- data/lib/guided_interactor/context.rb +40 -0
- data/lib/guided_interactor/delegator.rb +36 -0
- data/lib/guided_interactor/error.rb +12 -0
- data/lib/guided_interactor/hooks.rb +74 -0
- data/lib/guided_interactor/organizer.rb +36 -0
- data/lib/guided_interactor/version.rb +5 -0
- metadata +106 -0
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
guided_interactor (1.0.3)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
diff-lcs (1.3)
|
10
|
+
rake (10.5.0)
|
11
|
+
rspec (3.8.0)
|
12
|
+
rspec-core (~> 3.8.0)
|
13
|
+
rspec-expectations (~> 3.8.0)
|
14
|
+
rspec-mocks (~> 3.8.0)
|
15
|
+
rspec-core (3.8.0)
|
16
|
+
rspec-support (~> 3.8.0)
|
17
|
+
rspec-expectations (3.8.2)
|
18
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
19
|
+
rspec-support (~> 3.8.0)
|
20
|
+
rspec-mocks (3.8.0)
|
21
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
22
|
+
rspec-support (~> 3.8.0)
|
23
|
+
rspec-support (3.8.0)
|
24
|
+
|
25
|
+
PLATFORMS
|
26
|
+
ruby
|
27
|
+
|
28
|
+
DEPENDENCIES
|
29
|
+
bundler (~> 1.17)
|
30
|
+
guided_interactor!
|
31
|
+
rake (~> 10.0)
|
32
|
+
rspec (~> 3.0)
|
33
|
+
|
34
|
+
BUNDLED WITH
|
35
|
+
1.17.2
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2019
|
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,83 @@
|
|
1
|
+
# Guided Interactor
|
2
|
+
|
3
|
+
Guided Interactor is a spin-off from the [interactor](https://github.com/collectiveidea/interactor) gem. The Interactor
|
4
|
+
Design Pattern allows us to build simple, single-purpose objects that move the business logic away from controllers and
|
5
|
+
models. However, it also has some downsides, with the biggest pain point for me being the "Black Box Effect"
|
6
|
+
with `context` - you never know what goes in or what comes out.
|
7
|
+
|
8
|
+
```
|
9
|
+
# Typical interactor:
|
10
|
+
|
11
|
+
class Foo
|
12
|
+
include Interactor
|
13
|
+
|
14
|
+
def call
|
15
|
+
context.message = "#{context.greeting} #{context.name.capitalize}!"
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
Foo.call(greeting: "Hello", name: "Waldo")
|
20
|
+
=> #<Interactor::Context greeting="Hello", name="Waldo", message="Hello Waldo!">
|
21
|
+
|
22
|
+
Foo.call(greeting: "Hi")
|
23
|
+
NoMethodError: undefined method `capitalize' for nil:NilClass
|
24
|
+
```
|
25
|
+
|
26
|
+
Guided Interactor is trying to fill the gap by providing more flexibility and clarity. Previously we have agreed to use
|
27
|
+
`delegate :foo, to: :context` on two separate lines to define parameters expected and parameters that are set inside the
|
28
|
+
interactor. This gem introduces 3 additional methods: `expects`, `expects!` and `provides`:
|
29
|
+
|
30
|
+
```
|
31
|
+
class MyInteractor
|
32
|
+
include GuidedInteractor
|
33
|
+
|
34
|
+
expects :foo
|
35
|
+
expects! :bar
|
36
|
+
provides :waldo
|
37
|
+
|
38
|
+
def call
|
39
|
+
# do smth
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
43
|
+
```
|
44
|
+
|
45
|
+
`expects` and `provides` are aliases and simply delegate the specified parameters to the context. `expexts!` does the same,
|
46
|
+
but adds a before hook that fails the context if any of the parameters is missing. The example above could then be rewritten
|
47
|
+
like this:
|
48
|
+
|
49
|
+
```
|
50
|
+
# Guided interactor:
|
51
|
+
|
52
|
+
class Foo
|
53
|
+
include GuidedInteractor
|
54
|
+
|
55
|
+
expects :greeting
|
56
|
+
expects! :first_name, :last_name
|
57
|
+
provides :message
|
58
|
+
|
59
|
+
def call
|
60
|
+
context.message = "#{greeting} #{first_name.capitalize} #{last_name.capitalize}!"
|
61
|
+
puts message
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
Foo.call(greeting: "Hello", first_name: "Waldo", last_name: "smith")
|
66
|
+
Hello Waldo Smith!
|
67
|
+
=> #<GuidedInteractor::Context greeting="Hello", first_name="Waldo", last_name="smith", message="Hello Waldo Smith!">
|
68
|
+
|
69
|
+
result = Foo.call(greeting: "Hi")
|
70
|
+
=> #<GuidedInteractor::Context greeting="Hi">
|
71
|
+
result.success?
|
72
|
+
=> false
|
73
|
+
|
74
|
+
Foo.call!(greeting: "Hi", first_name: 'John')
|
75
|
+
GuidedInteractor::Failure: #<GuidedInteractor::Context greeting="Hi", first_name="John">
|
76
|
+
```
|
77
|
+
|
78
|
+
## Future plans
|
79
|
+
In the future, there are plans to add further guidance to guided interactors, including more validations, defaults and error handling.
|
80
|
+
|
81
|
+
## License
|
82
|
+
|
83
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
1.0.3
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "guided_interactor"
|
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
@@ -0,0 +1,27 @@
|
|
1
|
+
lib = File.expand_path("lib", __dir__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
require "guided_interactor/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "guided_interactor"
|
7
|
+
spec.version = GuidedInteractor::VERSION
|
8
|
+
spec.authors = ["Pille"]
|
9
|
+
spec.email = ["pille.peri@gmail.com"]
|
10
|
+
|
11
|
+
spec.summary = "A guided interactor"
|
12
|
+
spec.homepage = "https://github.com/pillep/guided_interactor"
|
13
|
+
spec.license = "MIT"
|
14
|
+
|
15
|
+
# Specify which files should be added to the gem when it is released.
|
16
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
17
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
18
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
19
|
+
end
|
20
|
+
spec.bindir = "exe"
|
21
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
22
|
+
spec.require_paths = ["lib"]
|
23
|
+
|
24
|
+
spec.add_development_dependency "bundler", "~> 1.17"
|
25
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
26
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
27
|
+
end
|
@@ -0,0 +1,72 @@
|
|
1
|
+
require 'guided_interactor/version'
|
2
|
+
require 'guided_interactor/context'
|
3
|
+
require 'guided_interactor/error'
|
4
|
+
require 'guided_interactor/hooks'
|
5
|
+
require 'guided_interactor/organizer'
|
6
|
+
require 'guided_interactor/delegator'
|
7
|
+
|
8
|
+
# Usage
|
9
|
+
#
|
10
|
+
# class MyInteractor
|
11
|
+
# include GuidedInteractor
|
12
|
+
#
|
13
|
+
# expects :foo
|
14
|
+
# expects! :bar, :baz
|
15
|
+
# provides :waldo
|
16
|
+
#
|
17
|
+
# def call
|
18
|
+
# context.waldo = 'waldo'
|
19
|
+
# puts foo
|
20
|
+
# puts waldo
|
21
|
+
# end
|
22
|
+
# end
|
23
|
+
module GuidedInteractor
|
24
|
+
|
25
|
+
def self.included(base)
|
26
|
+
base.class_eval do
|
27
|
+
extend ClassMethods
|
28
|
+
include Hooks
|
29
|
+
include Delegator
|
30
|
+
|
31
|
+
attr_reader :context
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
module ClassMethods
|
36
|
+
|
37
|
+
def call(context = {})
|
38
|
+
new(context).tap(&:run).context
|
39
|
+
end
|
40
|
+
|
41
|
+
def call!(context = {})
|
42
|
+
new(context).tap(&:run!).context
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
46
|
+
|
47
|
+
def initialize(context = {})
|
48
|
+
@context = Context.build(context)
|
49
|
+
end
|
50
|
+
|
51
|
+
def run
|
52
|
+
run!
|
53
|
+
rescue Failure
|
54
|
+
end
|
55
|
+
|
56
|
+
def run!
|
57
|
+
with_hooks do
|
58
|
+
call
|
59
|
+
context.called!(self)
|
60
|
+
end
|
61
|
+
rescue StandardError
|
62
|
+
context.rollback!
|
63
|
+
raise
|
64
|
+
end
|
65
|
+
|
66
|
+
def call
|
67
|
+
end
|
68
|
+
|
69
|
+
def rollback
|
70
|
+
end
|
71
|
+
|
72
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require 'ostruct'
|
2
|
+
|
3
|
+
module GuidedInteractor
|
4
|
+
class Context < OpenStruct
|
5
|
+
|
6
|
+
def self.build(context = {})
|
7
|
+
self === context ? context : new(context)
|
8
|
+
end
|
9
|
+
|
10
|
+
def success?
|
11
|
+
!failure?
|
12
|
+
end
|
13
|
+
|
14
|
+
def failure?
|
15
|
+
@failure || false
|
16
|
+
end
|
17
|
+
|
18
|
+
def fail!(context = {})
|
19
|
+
context.each { |key, value| modifiable[key.to_sym] = value }
|
20
|
+
@failure = true
|
21
|
+
raise Failure, self
|
22
|
+
end
|
23
|
+
|
24
|
+
def called!(interactor)
|
25
|
+
_called << interactor
|
26
|
+
end
|
27
|
+
|
28
|
+
def rollback!
|
29
|
+
return false if @rolled_back
|
30
|
+
|
31
|
+
_called.reverse_each(&:rollback)
|
32
|
+
@rolled_back = true
|
33
|
+
end
|
34
|
+
|
35
|
+
def _called
|
36
|
+
@called ||= []
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
module GuidedInteractor
|
2
|
+
module Delegator
|
3
|
+
|
4
|
+
def self.included(base)
|
5
|
+
base.class_eval do
|
6
|
+
extend ClassMethods
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
module ClassMethods
|
11
|
+
|
12
|
+
def expects(*params)
|
13
|
+
params.each do |param|
|
14
|
+
define_method param do
|
15
|
+
context.public_send(param)
|
16
|
+
end
|
17
|
+
private param
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def expects!(*params)
|
22
|
+
expects(*params)
|
23
|
+
|
24
|
+
before do
|
25
|
+
params.each do |param|
|
26
|
+
context.fail! if context.public_send(param).nil?
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
alias_method :provides, :expects
|
32
|
+
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,74 @@
|
|
1
|
+
module GuidedInteractor
|
2
|
+
module Hooks
|
3
|
+
|
4
|
+
def self.included(base)
|
5
|
+
base.class_eval do
|
6
|
+
extend ClassMethods
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
module ClassMethods
|
11
|
+
|
12
|
+
def around(*hooks, &block)
|
13
|
+
hooks << block if block
|
14
|
+
hooks.each { |hook| around_hooks.push(hook) }
|
15
|
+
end
|
16
|
+
|
17
|
+
def before(*hooks, &block)
|
18
|
+
hooks << block if block
|
19
|
+
hooks.each { |hook| before_hooks.push(hook) }
|
20
|
+
end
|
21
|
+
|
22
|
+
def after(*hooks, &block)
|
23
|
+
hooks << block if block
|
24
|
+
hooks.each { |hook| after_hooks.unshift(hook) }
|
25
|
+
end
|
26
|
+
|
27
|
+
def around_hooks
|
28
|
+
@around_hooks ||= []
|
29
|
+
end
|
30
|
+
|
31
|
+
def before_hooks
|
32
|
+
@before_hooks ||= []
|
33
|
+
end
|
34
|
+
|
35
|
+
def after_hooks
|
36
|
+
@after_hooks ||= []
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
40
|
+
|
41
|
+
private
|
42
|
+
|
43
|
+
def with_hooks
|
44
|
+
run_around_hooks do
|
45
|
+
run_before_hooks
|
46
|
+
yield
|
47
|
+
run_after_hooks
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def run_around_hooks(&block)
|
52
|
+
self.class.around_hooks.reverse.reduce(block) do |chain, hook|
|
53
|
+
proc { run_hook(hook, chain) }
|
54
|
+
end.call
|
55
|
+
end
|
56
|
+
|
57
|
+
def run_before_hooks
|
58
|
+
run_hooks(self.class.before_hooks)
|
59
|
+
end
|
60
|
+
|
61
|
+
def run_after_hooks
|
62
|
+
run_hooks(self.class.after_hooks)
|
63
|
+
end
|
64
|
+
|
65
|
+
def run_hooks(hooks)
|
66
|
+
hooks.each { |hook| run_hook(hook) }
|
67
|
+
end
|
68
|
+
|
69
|
+
def run_hook(hook, *args)
|
70
|
+
hook.is_a?(Symbol) ? send(hook, *args) : instance_exec(*args, &hook)
|
71
|
+
end
|
72
|
+
|
73
|
+
end
|
74
|
+
end
|