interactor_with_steroids 1.2.0 → 1.4.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
  SHA256:
3
- metadata.gz: ee0822c1b5d0c7e5ec36e35a2eb7989f19587f8b64a6a784e183fc6545c2ce4b
4
- data.tar.gz: 4759c36e11b659a8ddef357ac928c297f5383b370d0a075c1dd0eb20e2a392bf
3
+ metadata.gz: 2e5289e3868d4b8ae37dbe8078ff4c96e0206bc39e94fa465f32bf4c73bb4c65
4
+ data.tar.gz: 66d785650382be1320340c0791ad9d7c2efbdbbe6245e24e9048d4d82197ca09
5
5
  SHA512:
6
- metadata.gz: ce654d725ec8ebec129d6667612d2bf74e309457589c05592c6ec6402fc69f5756ce05f392a132177c3b351ca362374ea1b75ce217c26c73a6a0313ee4e08bff
7
- data.tar.gz: b562c0b40d1d4d95e16726b0aeb115b89ada4606d3371ebababda23be7ae74ca984138182c2790ee1fd9c837d614bcaf741c47dfe19f7083fd0d9d5f742f38bb
6
+ metadata.gz: 2208ac798a7531e5e8615683e9ed8251bc3377bd243a14047f433e672f1a5e37b869a84cc19918b9059c225211ee2eb73e211851566fc29138104238b1448e6e
7
+ data.tar.gz: ac631eb1de4cea38bcae466cb0ff7726fdfb04a582b0c5f483fe106da5ad0b88c106f2cec29bbc5f529f79c61119ca92796a6ff62f22c08c0f85e003ebca74fe
data/interactor.gemspec CHANGED
@@ -2,7 +2,7 @@ require "English"
2
2
 
3
3
  Gem::Specification.new do |spec|
4
4
  spec.name = "interactor_with_steroids"
5
- spec.version = "1.2.0"
5
+ spec.version = "1.4.0"
6
6
 
7
7
  spec.author = "Collective Idea/Sorare Team"
8
8
  spec.email = "hello@sorare.com"
@@ -63,18 +63,26 @@ module Interactor
63
63
  end
64
64
  end
65
65
 
66
- def hold(*held_fields)
67
- @held_fields ||= []
68
- @held_fields += held_fields
69
-
70
- delegate(*@held_fields, to: :context)
66
+ def hold(*held_fields, **held_fields_with_default_value)
67
+ attributes = [*held_fields, *held_fields_with_default_value.keys]
68
+ delegate(*attributes, to: :context)
71
69
 
72
70
  self.context_class = Class.new(context_class) do
73
71
  attr_accessor *held_fields
72
+ attr_writer *held_fields_with_default_value.keys
73
+
74
+ held_fields_with_default_value.each do |k, v|
75
+ define_method(k) do
76
+ ivar = "@#{k}"
77
+ return instance_variable_get(ivar) if instance_variable_defined?(ivar)
78
+
79
+ instance_variable_set(ivar, v.is_a?(Proc) ? instance_eval(&v) : v)
80
+ end
81
+ end
74
82
 
75
83
  class_eval %Q<
76
84
  def to_h
77
- super.merge(#{held_fields.map { |f| "#{f}: self.#{f}"}.join(', ')})
85
+ super.merge(#{attributes.map { |f| "#{f}: self.#{f}"}.join(', ')})
78
86
  end
79
87
  >
80
88
  end
@@ -27,5 +27,12 @@ module Interactor
27
27
  @context = context
28
28
  super
29
29
  end
30
+
31
+ def cause_stack
32
+ causes = [context.error_cause]
33
+ causes << causes.last.cause while causes.last&.cause
34
+
35
+ causes.compact
36
+ end
30
37
  end
31
38
  end
@@ -125,6 +125,42 @@ module Interactor
125
125
  c.foo = 'bar'
126
126
  expect(c.foo).to eq('bar')
127
127
  end
128
+
129
+ context 'with default value' do
130
+ let(:declared) {
131
+ build_declared do
132
+ hold foo: 'bar'
133
+ end
134
+ }
135
+
136
+ it 'can hold foo with default value' do
137
+ c = subject.build
138
+ expect(c.foo).to eq('bar')
139
+
140
+ c.foo = 'baz'
141
+ expect(c.foo).to eq('baz')
142
+ end
143
+
144
+ context 'when default value is a proc' do
145
+ let(:declared) {
146
+ build_declared do
147
+ hold foo: proc { [] }
148
+ end
149
+ }
150
+
151
+ it 'can hold foo with default value different for each new context through proc' do
152
+ c = subject.build
153
+ expect(c.foo).to eq([])
154
+
155
+ other_c = subject.build
156
+ expect(other_c.foo).to eq([])
157
+
158
+ c.foo << 'baz'
159
+ expect(c.foo).to eq(['baz'])
160
+ expect(other_c.foo).to eq([])
161
+ end
162
+ end
163
+ end
128
164
  end
129
165
  end
130
166
  end
@@ -0,0 +1,29 @@
1
+ module Interactor
2
+ describe Failure do
3
+ describe ".cause_stack" do
4
+ subject { failure.cause_stack }
5
+
6
+ let(:exception_1) { Class.new(Exception) }
7
+ let(:exception_2) { Class.new(Exception) }
8
+ let(:interactor) do
9
+ Class.new.send(:include, Interactor) do
10
+ def call
11
+ begin
12
+ raise exception_1
13
+ rescue StandardError
14
+ raise exception_2
15
+ end
16
+ rescue StandardError
17
+ context.fail!
18
+ end
19
+ end
20
+ end
21
+
22
+ it "returns an empty stack" do
23
+ interactor.call!
24
+ rescue Failure => e
25
+ expect(e.cause_stack).to contain_exactly(described_class, exception_1, exception_2)
26
+ end
27
+ end
28
+ end
29
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: interactor_with_steroids
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 1.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Collective Idea/Sorare Team
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-12-08 00:00:00.000000000 Z
11
+ date: 2023-01-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -77,6 +77,7 @@ files:
77
77
  - lib/interactor/organizer.rb
78
78
  - spec/interactor/context_spec.rb
79
79
  - spec/interactor/declaration_spec.rb
80
+ - spec/interactor/error_spec.rb
80
81
  - spec/interactor/hooks_spec.rb
81
82
  - spec/interactor/organizer_spec.rb
82
83
  - spec/interactor_spec.rb
@@ -100,13 +101,14 @@ required_rubygems_version: !ruby/object:Gem::Requirement
100
101
  - !ruby/object:Gem::Version
101
102
  version: '0'
102
103
  requirements: []
103
- rubygems_version: 3.3.3
104
+ rubygems_version: 3.3.7
104
105
  signing_key:
105
106
  specification_version: 4
106
107
  summary: Simple interactor implementation
107
108
  test_files:
108
109
  - spec/interactor/context_spec.rb
109
110
  - spec/interactor/declaration_spec.rb
111
+ - spec/interactor/error_spec.rb
110
112
  - spec/interactor/hooks_spec.rb
111
113
  - spec/interactor/organizer_spec.rb
112
114
  - spec/interactor_spec.rb