interactor_with_steroids 1.3.0 → 1.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/interactor.gemspec +1 -1
- data/lib/interactor/declaration.rb +25 -5
- data/spec/interactor/declaration_spec.rb +72 -25
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a641ca1acbb8c2de2ec87757ef54c644189997860477ccdbfd138ded126ccd3b
|
4
|
+
data.tar.gz: c5d85b0aa88784f4b60c5dafbc49338d7508674f589528a75671568dcc697383
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e703700a5efd8100e78c846fd31aa9b6ee1bd535608f8bfb92f6d8512804e279e73b2a62f2b9d70bcd822a1cbd1e0ad16012d6d874aa519bf2edf32682c209b4
|
7
|
+
data.tar.gz: 55c97b6ad6da3e4f9f35ccc6be6df32414e8bf12a00c3c865af9fe72dfa3d19ab895e53d1bf7fd182b3994d91dbbb0aeb7f57de7097edeca67c68bcc7ccaf84b
|
data/interactor.gemspec
CHANGED
@@ -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
|
67
|
-
@
|
68
|
-
|
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
|
-
|
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(#{
|
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
|
18
|
-
context
|
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
|
25
|
+
it 'cannot be initialized without foo' do
|
26
26
|
expect { subject.build }.to raise_error(ArgumentError)
|
27
27
|
end
|
28
28
|
|
29
|
-
it
|
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(
|
56
|
+
before { stub_const('Submodule', submodule) }
|
53
57
|
|
54
|
-
it
|
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
|
62
|
-
context
|
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
|
72
|
+
it 'can be initialized without foo' do
|
70
73
|
expect(subject.build.foo).to eq('bar')
|
71
74
|
end
|
72
75
|
|
73
|
-
it
|
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
|
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
|
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
|
92
|
+
it 'can be initialized without foo' do
|
90
93
|
expect(subject.build.foo).to be nil
|
91
94
|
end
|
92
95
|
|
93
|
-
it
|
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
|
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
|
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
|
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
|
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.
|
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:
|
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.
|
104
|
+
rubygems_version: 3.3.7
|
105
105
|
signing_key:
|
106
106
|
specification_version: 4
|
107
107
|
summary: Simple interactor implementation
|