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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3738599fd10bf883b7ccdc29ff2fbe4322a75851
4
- data.tar.gz: 71e339e19b1dbcb3c4a874060ae4f38e1cd37702
3
+ metadata.gz: c80e8f066f85441403b978f5ac73afb31c327309
4
+ data.tar.gz: a8e6c26945bfa58ec2f1002ba424978722f75e63
5
5
  SHA512:
6
- metadata.gz: c8c053d53c21e3ee48e905959e8ff8298b3caa93c7df9ad2c60793b3108cf0b1b3dd106563bd5630898bb54944b7bc6eb9b1b63dc88ee736eebdfe060265581d
7
- data.tar.gz: b0c7b00554345b259ffbcaae25c1817f99e7796300a1c11e8f1f1939eb9e9cd20693b57667c247b74a7105fa5fadb9490adcace3d7527b8b869f61d94b222c04
6
+ metadata.gz: bdc2f6fedb3587b76a24cca6e5f87acdcabcb0ecf66b0a591604e074a813f4a790b8c154afc6876fff5c0727c3bec49f45d5816efed313eaaece3a7ef0dc304c
7
+ data.tar.gz: 7d7beedb7f72fb0445ceae4ddec156342d13e38a6a768c3b790d6f29791af54035ad2003ca6bc2a31db0b7e1f3b12992f21d7987fbf954160e8536fee0a148e5
data/Gemfile CHANGED
@@ -3,6 +3,7 @@ source "https://rubygems.org"
3
3
  gemspec
4
4
 
5
5
  group :test do
6
+ gem "activesupport", "~> 4.0", require: false
6
7
  gem "coveralls", "~> 0.6.7", require: false
7
8
  gem "rspec", "~> 2.14"
8
9
  end
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |spec|
4
4
  spec.name = "interactor"
5
- spec.version = "2.0.1"
5
+ spec.version = "2.1.0"
6
6
 
7
7
  spec.author = "Collective Idea"
8
8
  spec.email = "info@collectiveidea.com"
@@ -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
@@ -1,7 +1,13 @@
1
+ require "delegate"
2
+
1
3
  module Interactor
2
- class Context < ::Hash
4
+ class Context < SimpleDelegator
3
5
  def self.build(context = {})
4
- self === context ? context : new.replace(context)
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?
@@ -26,7 +26,13 @@ module Interactor
26
26
 
27
27
  def perform
28
28
  interactors.each do |interactor|
29
- instance = interactor.perform(context)
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
@@ -1,5 +1,7 @@
1
- require "coveralls"
2
- Coveralls.wear!
1
+ if ENV["TRAVIS"]
2
+ require "coveralls"
3
+ Coveralls.wear!
4
+ end
3
5
 
4
6
  require "interactor"
5
7
 
@@ -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.1
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-08-28 00:00:00.000000000 Z
11
+ date: 2013-09-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler