interactor_with_steroids 1.3.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: 7c29f3d44762d497592a20ae9da8a448fbdf2121116fc5c222f6323e1352c98e
4
- data.tar.gz: b6f0cca693608e9c8cfecd0dd51098c9befb29ef26395e8750c9fd0ffd253699
3
+ metadata.gz: 2e5289e3868d4b8ae37dbe8078ff4c96e0206bc39e94fa465f32bf4c73bb4c65
4
+ data.tar.gz: 66d785650382be1320340c0791ad9d7c2efbdbbe6245e24e9048d4d82197ca09
5
5
  SHA512:
6
- metadata.gz: 761a11f3eaead34c27ce5df13af3f17449d3b7317da05d77efd3091debfa1b249b2db8a5759ed189470f3f16e0aff25a70f8ac84d4b788d7ab06a08f2f1c7927
7
- data.tar.gz: '039491509364e1df4dfb16084b782d8be7983900da3a15120001f811f68509a33656d376aa388ea279530ab2b7e17e5f3ebd704f158a98b17e3e877f5e9dd91c'
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.3.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
@@ -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
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.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-22 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
@@ -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