interactor 2.0.1 → 2.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +1 -0
- data/interactor.gemspec +1 -1
- data/lib/interactor.rb +2 -2
- data/lib/interactor/context.rb +8 -2
- data/lib/interactor/organizer.rb +7 -1
- data/spec/interactor/context_spec.rb +31 -0
- data/spec/interactor/organizer_spec.rb +13 -0
- data/spec/spec_helper.rb +4 -2
- data/spec/support/lint.rb +7 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c80e8f066f85441403b978f5ac73afb31c327309
|
4
|
+
data.tar.gz: a8e6c26945bfa58ec2f1002ba424978722f75e63
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bdc2f6fedb3587b76a24cca6e5f87acdcabcb0ecf66b0a591604e074a813f4a790b8c154afc6876fff5c0727c3bec49f45d5816efed313eaaece3a7ef0dc304c
|
7
|
+
data.tar.gz: 7d7beedb7f72fb0445ceae4ddec156342d13e38a6a768c3b790d6f29791af54035ad2003ca6bc2a31db0b7e1f3b12992f21d7987fbf954160e8536fee0a148e5
|
data/Gemfile
CHANGED
data/interactor.gemspec
CHANGED
data/lib/interactor.rb
CHANGED
@@ -43,10 +43,10 @@ module Interactor
|
|
43
43
|
end
|
44
44
|
|
45
45
|
def method_missing(method, *)
|
46
|
-
context.fetch(method) { super }
|
46
|
+
context.fetch(method) { context.fetch(method.to_s) { super } }
|
47
47
|
end
|
48
48
|
|
49
49
|
def respond_to_missing?(method, *)
|
50
|
-
(context && context.key?(method)) || super
|
50
|
+
(context && (context.key?(method) || context.key?(method.to_s))) || super
|
51
51
|
end
|
52
52
|
end
|
data/lib/interactor/context.rb
CHANGED
@@ -1,7 +1,13 @@
|
|
1
|
+
require "delegate"
|
2
|
+
|
1
3
|
module Interactor
|
2
|
-
class Context <
|
4
|
+
class Context < SimpleDelegator
|
3
5
|
def self.build(context = {})
|
4
|
-
self === context ? context : new
|
6
|
+
self === context ? context : new(context.dup)
|
7
|
+
end
|
8
|
+
|
9
|
+
def initialize(context = {})
|
10
|
+
super(context)
|
5
11
|
end
|
6
12
|
|
7
13
|
def success?
|
data/lib/interactor/organizer.rb
CHANGED
@@ -26,7 +26,13 @@ module Interactor
|
|
26
26
|
|
27
27
|
def perform
|
28
28
|
interactors.each do |interactor|
|
29
|
-
|
29
|
+
begin
|
30
|
+
instance = interactor.perform(context)
|
31
|
+
rescue
|
32
|
+
rollback
|
33
|
+
raise
|
34
|
+
end
|
35
|
+
|
30
36
|
rollback && break if failure?
|
31
37
|
performed << instance
|
32
38
|
end
|
@@ -17,6 +17,18 @@ module Interactor
|
|
17
17
|
expect(context).to eq({})
|
18
18
|
end
|
19
19
|
|
20
|
+
it "doesn't affect the original hash" do
|
21
|
+
hash = { foo: "bar" }
|
22
|
+
context = Context.build(hash)
|
23
|
+
|
24
|
+
expect(context).to be_a(Context)
|
25
|
+
expect {
|
26
|
+
context[:foo] = "baz"
|
27
|
+
}.not_to change {
|
28
|
+
hash[:foo]
|
29
|
+
}
|
30
|
+
end
|
31
|
+
|
20
32
|
it "preserves an already built context" do
|
21
33
|
context1 = Context.build(foo: "bar")
|
22
34
|
context2 = Context.build(context1)
|
@@ -30,6 +42,25 @@ module Interactor
|
|
30
42
|
end
|
31
43
|
end
|
32
44
|
|
45
|
+
describe "#initialize" do
|
46
|
+
it "defaults to empty" do
|
47
|
+
expect {
|
48
|
+
expect(Context.new).to eq({})
|
49
|
+
}.not_to raise_error
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
describe "#[]" do
|
54
|
+
it "maintains indifferent access" do
|
55
|
+
require "active_support/hash_with_indifferent_access"
|
56
|
+
|
57
|
+
context = Context.build(HashWithIndifferentAccess.new(foo: "bar"))
|
58
|
+
|
59
|
+
expect(context[:foo]).to eq("bar")
|
60
|
+
expect(context["foo"]).to eq("bar")
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
33
64
|
describe "#success?" do
|
34
65
|
let(:context) { Context.build }
|
35
66
|
|
@@ -104,6 +104,19 @@ module Interactor
|
|
104
104
|
|
105
105
|
instance.perform
|
106
106
|
end
|
107
|
+
|
108
|
+
it "aborts and rolls back on error" do
|
109
|
+
error = StandardError.new("foo")
|
110
|
+
expect(interactor2).to receive(:perform).once.with(context).ordered { instance2 }
|
111
|
+
expect(interactor3).to receive(:perform).once.with(context).ordered { raise error }
|
112
|
+
expect(interactor4).not_to receive(:perform)
|
113
|
+
|
114
|
+
expect(instance).to receive(:rollback).once.ordered do
|
115
|
+
expect(instance.performed).to eq([instance2])
|
116
|
+
end
|
117
|
+
|
118
|
+
expect { instance.perform }.to raise_error(error)
|
119
|
+
end
|
107
120
|
end
|
108
121
|
|
109
122
|
describe "#rollback" do
|
data/spec/spec_helper.rb
CHANGED
data/spec/support/lint.rb
CHANGED
@@ -128,7 +128,7 @@ shared_examples :lint do
|
|
128
128
|
|
129
129
|
describe "context deferral" do
|
130
130
|
context "initialized" do
|
131
|
-
let(:instance) { interactor.new(foo: "bar") }
|
131
|
+
let(:instance) { interactor.new(foo: "bar", "hello" => "world") }
|
132
132
|
|
133
133
|
it "defers to keys that exist in the context" do
|
134
134
|
expect(instance).to respond_to(:foo)
|
@@ -136,6 +136,12 @@ shared_examples :lint do
|
|
136
136
|
expect { instance.method(:foo) }.not_to raise_error
|
137
137
|
end
|
138
138
|
|
139
|
+
it "defers to string keys that exist in the context" do
|
140
|
+
expect(instance).to respond_to(:hello)
|
141
|
+
expect(instance.hello).to eq("world")
|
142
|
+
expect { instance.method(:hello) }.not_to raise_error
|
143
|
+
end
|
144
|
+
|
139
145
|
it "bombs if the key does not exist in the context" do
|
140
146
|
expect(instance).not_to respond_to(:baz)
|
141
147
|
expect { instance.baz }.to raise_error(NoMethodError)
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: interactor
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.0
|
4
|
+
version: 2.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Collective Idea
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-
|
11
|
+
date: 2013-09-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|