interactor_with_steroids 1.3.0 → 1.5.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: 7c29f3d44762d497592a20ae9da8a448fbdf2121116fc5c222f6323e1352c98e
4
- data.tar.gz: b6f0cca693608e9c8cfecd0dd51098c9befb29ef26395e8750c9fd0ffd253699
3
+ metadata.gz: a641ca1acbb8c2de2ec87757ef54c644189997860477ccdbfd138ded126ccd3b
4
+ data.tar.gz: c5d85b0aa88784f4b60c5dafbc49338d7508674f589528a75671568dcc697383
5
5
  SHA512:
6
- metadata.gz: 761a11f3eaead34c27ce5df13af3f17449d3b7317da05d77efd3091debfa1b249b2db8a5759ed189470f3f16e0aff25a70f8ac84d4b788d7ab06a08f2f1c7927
7
- data.tar.gz: '039491509364e1df4dfb16084b782d8be7983900da3a15120001f811f68509a33656d376aa388ea279530ab2b7e17e5f3ebd704f158a98b17e3e877f5e9dd91c'
6
+ metadata.gz: e703700a5efd8100e78c846fd31aa9b6ee1bd535608f8bfb92f6d8512804e279e73b2a62f2b9d70bcd822a1cbd1e0ad16012d6d874aa519bf2edf32682c209b4
7
+ data.tar.gz: 55c97b6ad6da3e4f9f35ccc6be6df32414e8bf12a00c3c865af9fe72dfa3d19ab895e53d1bf7fd182b3994d91dbbb0aeb7f57de7097edeca67c68bcc7ccaf84b
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.3.0"
5
+ spec.version = "1.5.0"
6
6
 
7
7
  spec.author = "Collective Idea/Sorare Team"
8
8
  spec.email = "hello@sorare.com"
@@ -18,6 +18,8 @@ module Interactor
18
18
  new_required_arguments = required_arguments - @required_arguments
19
19
  @required_arguments += new_required_arguments
20
20
 
21
+ (required_arguments + optional_arguments.keys).each { |arg| received_arguments << arg }
22
+
21
23
  delegate(*new_required_arguments, to: :context) unless new_required_arguments.empty?
22
24
  delegate(*optional_arguments.keys, to: :context) unless optional_arguments.empty?
23
25
 
@@ -63,23 +65,41 @@ module Interactor
63
65
  end
64
66
  end
65
67
 
66
- def hold(*held_fields)
67
- @held_fields ||= []
68
- @held_fields += held_fields
68
+ def received_arguments
69
+ @received_arguments ||= []
70
+ end
71
+
72
+ def hold(*held_fields, **held_fields_with_default_value)
73
+ attributes = [*held_fields, *held_fields_with_default_value.keys]
74
+ delegate(*attributes, to: :context)
69
75
 
70
- delegate(*@held_fields, to: :context)
76
+ attributes.each { |attr| held_attributes << attr }
71
77
 
72
78
  self.context_class = Class.new(context_class) do
73
79
  attr_accessor *held_fields
80
+ attr_writer *held_fields_with_default_value.keys
81
+
82
+ held_fields_with_default_value.each do |k, v|
83
+ define_method(k) do
84
+ ivar = "@#{k}"
85
+ return instance_variable_get(ivar) if instance_variable_defined?(ivar)
86
+
87
+ instance_variable_set(ivar, v.is_a?(Proc) ? instance_eval(&v) : v)
88
+ end
89
+ end
74
90
 
75
91
  class_eval %Q<
76
92
  def to_h
77
- super.merge(#{held_fields.map { |f| "#{f}: self.#{f}"}.join(', ')})
93
+ super.merge(#{attributes.map { |f| "#{f}: self.#{f}"}.join(', ')})
78
94
  end
79
95
  >
80
96
  end
81
97
 
82
98
  end
99
+
100
+ def held_attributes
101
+ @held_attributes ||= []
102
+ end
83
103
  end
84
104
  end
85
105
  end
@@ -14,22 +14,26 @@ module Interactor
14
14
 
15
15
  subject { declared.context_class }
16
16
 
17
- describe "#receive" do
18
- context "with a required argument" do
19
- let(:declared) {
17
+ describe '#receive' do
18
+ context 'with a required argument' do
19
+ let(:declared) do
20
20
  build_declared do
21
21
  receive :foo
22
22
  end
23
- }
23
+ end
24
24
 
25
- it "cannot be initialized without foo" do
25
+ it 'cannot be initialized without foo' do
26
26
  expect { subject.build }.to raise_error(ArgumentError)
27
27
  end
28
28
 
29
- it "can be initialized with foo" do
29
+ it 'can be initialized with foo' do
30
30
  expect(subject.build(foo: 'bar').foo).to eq('bar')
31
31
  end
32
32
 
33
+ it 'can introspect the received arguments' do
34
+ expect(declared.received_arguments).to eq([:foo])
35
+ end
36
+
33
37
  context 'when duplicated in a submodule' do
34
38
  let(:submodule) do
35
39
  Module.new do
@@ -41,79 +45,82 @@ module Interactor
41
45
  end
42
46
  end
43
47
 
44
- let(:declared) {
48
+ let(:declared) do
45
49
  build_declared do
46
50
  include Submodule
47
51
 
48
52
  receive :foo
49
53
  end
50
- }
54
+ end
51
55
 
52
- before { stub_const("Submodule", submodule) }
56
+ before { stub_const('Submodule', submodule) }
53
57
 
54
- it "can be initialized with foo" do
58
+ it 'can be initialized with foo' do
55
59
  expect(subject.build(foo: 'bar').foo).to eq('bar')
56
60
  end
57
61
  end
58
-
59
62
  end
60
63
 
61
- context "with an optional argument" do
62
- context "with a constant default value" do
63
- let(:declared) {
64
+ context 'with an optional argument' do
65
+ context 'with a constant default value' do
66
+ let(:declared) do
64
67
  build_declared do
65
68
  receive foo: 'bar'
66
69
  end
67
- }
70
+ end
68
71
 
69
- it "can be initialized without foo" do
72
+ it 'can be initialized without foo' do
70
73
  expect(subject.build.foo).to eq('bar')
71
74
  end
72
75
 
73
- it "can be initialized with foo" do
76
+ it 'can be initialized with foo' do
74
77
  expect(subject.build(foo: 'baz').foo).to eq('baz')
75
78
  end
76
79
 
77
- it "can be initialized with nil" do
80
+ it 'can be initialized with nil' do
78
81
  expect(subject.build(foo: nil).foo).to be nil
79
82
  end
80
83
  end
81
84
 
82
- context "with a nil default value" do
85
+ context 'with a nil default value' do
83
86
  let(:declared) {
84
87
  build_declared do
85
88
  receive foo: nil
86
89
  end
87
90
  }
88
91
 
89
- it "can be initialized without foo" do
92
+ it 'can be initialized without foo' do
90
93
  expect(subject.build.foo).to be nil
91
94
  end
92
95
 
93
- it "can be initialized with foo" do
96
+ it 'can be initialized with foo' do
94
97
  expect(subject.build(foo: 'baz').foo).to eq('baz')
95
98
  end
96
99
  end
97
100
 
98
- context "with a Proc default value" do
101
+ context 'with a Proc default value' do
99
102
  let(:declared) {
100
103
  build_declared do
101
104
  receive :bar, foo: ->(context) { context.bar }
102
105
  end
103
106
  }
104
107
 
105
- it "can be initialized without foo" do
108
+ it 'can be initialized without foo' do
106
109
  expect(subject.build(bar: 'bar').foo).to eq('bar')
107
110
  end
108
111
 
109
- it "can be initialized with foo" do
112
+ it 'can be initialized with foo' do
110
113
  expect(subject.build(bar: 'bar', foo: 'baz').foo).to eq('baz')
111
114
  end
115
+
116
+ it 'can introspect the received arguments' do
117
+ expect(declared.received_arguments).to eq(%i[bar foo])
118
+ end
112
119
  end
113
120
  end
114
121
  end
115
122
 
116
- describe "#hold" do
123
+ describe '#hold' do
117
124
  let(:declared) {
118
125
  build_declared do
119
126
  hold :foo
@@ -125,6 +132,46 @@ module Interactor
125
132
  c.foo = 'bar'
126
133
  expect(c.foo).to eq('bar')
127
134
  end
135
+
136
+ it 'can introspect the held attributes' do
137
+ expect(declared.held_attributes).to eq([:foo])
138
+ end
139
+
140
+ context 'with default value' do
141
+ let(:declared) {
142
+ build_declared do
143
+ hold foo: 'bar'
144
+ end
145
+ }
146
+
147
+ it 'can hold foo with default value' do
148
+ c = subject.build
149
+ expect(c.foo).to eq('bar')
150
+
151
+ c.foo = 'baz'
152
+ expect(c.foo).to eq('baz')
153
+ end
154
+
155
+ context 'when default value is a proc' do
156
+ let(:declared) {
157
+ build_declared do
158
+ hold foo: proc { [] }
159
+ end
160
+ }
161
+
162
+ it 'can hold foo with default value different for each new context through proc' do
163
+ c = subject.build
164
+ expect(c.foo).to eq([])
165
+
166
+ other_c = subject.build
167
+ expect(other_c.foo).to eq([])
168
+
169
+ c.foo << 'baz'
170
+ expect(c.foo).to eq(['baz'])
171
+ expect(other_c.foo).to eq([])
172
+ end
173
+ end
174
+ end
128
175
  end
129
176
  end
130
177
  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.3.0
4
+ version: 1.5.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-22 00:00:00.000000000 Z
11
+ date: 2023-04-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -101,7 +101,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
101
101
  - !ruby/object:Gem::Version
102
102
  version: '0'
103
103
  requirements: []
104
- rubygems_version: 3.3.3
104
+ rubygems_version: 3.3.7
105
105
  signing_key:
106
106
  specification_version: 4
107
107
  summary: Simple interactor implementation