Skip to content

Commit 8232d68

Browse files
committed
Move early return catches from #call to #run!
This should avoid a couple problems: - In tests you might want to instantiate the interaktor directly with `.new`, and then stub methods on it, and then manually call `#run`. If you did this then calls to `#fail!` would fail to catch thrown early returns, breaking tests. - Early returning will prevent `#run!` from calling `#called!` on the context object, breaking organizers.
1 parent e88f322 commit 8232d68

File tree

4 files changed

+19
-9
lines changed

4 files changed

+19
-9
lines changed

interaktor.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Gem::Specification.new do |spec|
22
spec.name = "interaktor"
3-
spec.version = "0.1.4"
3+
spec.version = "0.1.5"
44

55
spec.author = "Taylor Thurlow"
66
spec.email = "[email protected]"

lib/interaktor.rb

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -141,9 +141,7 @@ def call(context = {})
141141
apply_default_optional_attributes(context)
142142
verify_attribute_presence(context)
143143

144-
catch(:early_return) do
145-
new(context).tap(&:run).instance_variable_get(:@context)
146-
end
144+
new(context).tap(&:run).instance_variable_get(:@context)
147145
end
148146

149147
# Invoke an Interaktor. This method behaves identically to `#call`, with
@@ -160,9 +158,7 @@ def call!(context = {})
160158
apply_default_optional_attributes(context)
161159
verify_attribute_presence(context)
162160

163-
catch(:early_return) do
164-
new(context).tap(&:run!).instance_variable_get(:@context)
165-
end
161+
new(context).tap(&:run!).instance_variable_get(:@context)
166162
end
167163

168164
private
@@ -284,7 +280,10 @@ def run
284280
# @return [void]
285281
def run!
286282
with_hooks do
287-
call
283+
catch(:early_return) do
284+
call
285+
end
286+
288287
@context.called!(self)
289288
end
290289
rescue StandardError

spec/integration_spec.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ def build_interaktor(&block)
88
interaktor = Class.new.send(:include, Interaktor)
99
interaktor.class_eval(&block) if block
1010
interaktor.class_eval do
11+
optional :steps
12+
1113
def unexpected_error!
1214
raise "foo"
1315
end
@@ -20,6 +22,8 @@ def build_organizer(options = {}, &block)
2022
organizer.organize(options[:organize]) if options[:organize]
2123
organizer.class_eval(&block) if block
2224
organizer.class_eval do
25+
optional :steps
26+
2327
def unexpected_error!
2428
raise "foo"
2529
end

spec/support/lint.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
shared_examples "lint" do
2-
let(:interaktor) { Class.new.include(described_class) }
2+
let(:interaktor) do
3+
klass = Class.new.include(described_class)
4+
klass.class_eval do
5+
optional :foo
6+
end
7+
8+
klass
9+
end
310

411
describe ".call" do
512
let(:context) { instance_double(Interaktor::Context) }

0 commit comments

Comments
 (0)