subroutine 0.10.0.beta2 → 0.10.0.beta3

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f2c3e4d90c09ae08102e8ab71974246b2052203ad830d838328e25345d92593e
4
- data.tar.gz: a1f1e10be4abe4940ca34811ce4951d498ece0aa3bb2b199c776e3c13e142653
3
+ metadata.gz: ad452be684c7aa5f556a1d203c5f6d02a8780600f74276743c164391a91d6fde
4
+ data.tar.gz: 3f2d5c3303bb579c3741a10e659af80b0adcb7c3a64373dcf6f7f2d82bb37703
5
5
  SHA512:
6
- metadata.gz: 420d85e72ba91cb3f53025fb43d0dd5276bfbc057f3910acba283d53676667408fc4a52cc5ee5f6d18fc00ffd385ba180bf7b2bf62f608a9a0c37f5541b645cb
7
- data.tar.gz: 47556a1a5be391f9f0fabad3f656b7a8f813aca312c2a82f278b8f347dddecb024ec5d0cb82d564e7488445ccbac3b50697be68aa769aaa78392d0af8e5186e6
6
+ metadata.gz: aa63944122d676ca984525137e05430766b767b20fee7f65c216b45c5ecf523781c9d4e35119d17d7fd99dd58580f43e67405b72a7027886c96166103dac6bc5
7
+ data.tar.gz: 21c97fa2d75c0fbbd85f691d89e90666cddf490b7dd37fc676a76543bb1b6a5c7654959a6ba2aa19640ccd658ea8f0786c405bafbb2e7f63d0ba7ce64496cd08
@@ -37,6 +37,22 @@ module Subroutine
37
37
  nil
38
38
  end
39
39
 
40
+ def has_default?
41
+ config.key?(:default)
42
+ end
43
+
44
+ def get_default
45
+ value = config[:default]
46
+ if value.respond_to?(:call)
47
+ value = value.call
48
+ elsif value.try(:duplicable?) # from active_support
49
+ # Some classes of default values need to be duplicated, or the instance field value will end up referencing
50
+ # the class global default value, and potentially modify it.
51
+ value = value.deep_dup # from active_support
52
+ end
53
+ value
54
+ end
55
+
40
56
  def inheritable_options
41
57
  config.slice(*INHERITABLE_OPTIONS)
42
58
  end
@@ -48,6 +48,8 @@ module Subroutine
48
48
  end
49
49
  EV
50
50
  end
51
+
52
+ config
51
53
  end
52
54
  alias input field
53
55
 
@@ -108,6 +110,17 @@ module Subroutine
108
110
  param_groups[:#{group_name}]
109
111
  end
110
112
 
113
+ try(:silence_redefinition_of_method, :#{group_name}_default_params)
114
+ def #{group_name}_default_params
115
+ group_field_names = fields_in_group(:#{group_name}).keys
116
+ all_default_params.slice(*group_field_names)
117
+ end
118
+
119
+ try(:silence_redefinition_of_method, :#{group_name}_params_with_defaults)
120
+ def #{group_name}_params_with_defaults
121
+ #{group_name}_default_params.merge(param_groups[:#{group_name}])
122
+ end
123
+
111
124
  try(:silence_redefinition_of_method, :without_#{group_name}_params)
112
125
  def without_#{group_name}_params
113
126
  all_params.except(*#{group_name}_params.keys)
@@ -120,7 +133,6 @@ module Subroutine
120
133
  def setup_fields(inputs = {})
121
134
  @provided_fields = {}.with_indifferent_access
122
135
  param_groups[:original] = inputs.with_indifferent_access
123
- param_groups[:default] = build_defaults
124
136
  mass_assign_initial_params
125
137
  end
126
138
 
@@ -139,16 +151,29 @@ module Subroutine
139
151
  def ungrouped_params
140
152
  get_param_group(:ungrouped)
141
153
  end
142
- alias params ungrouped_params
143
154
 
144
155
  def all_params
145
156
  get_param_group(:all)
146
157
  end
158
+ alias params all_params
147
159
 
148
- def defaults
160
+ def all_default_params
149
161
  get_param_group(:default)
150
162
  end
151
- alias default_params defaults
163
+ alias defaults all_default_params
164
+
165
+ def all_params_with_defaults
166
+ all_default_params.merge(all_params)
167
+ end
168
+ alias params_with_defaults all_params_with_defaults
169
+
170
+ def ungrouped_defaults
171
+ default_params.slice(*ungrouped_fields.keys)
172
+ end
173
+
174
+ def ungrouped_params_with_defaults
175
+ ungrouped_defaults.merge(ungrouped_params)
176
+ end
152
177
 
153
178
  def get_field_config(field_name)
154
179
  self.class.get_field_config(field_name)
@@ -160,7 +185,7 @@ module Subroutine
160
185
  end
161
186
 
162
187
  def get_field(name)
163
- all_params[name]
188
+ field_provided?(name) ? all_params[name] : all_default_params[name]
164
189
  end
165
190
 
166
191
  def set_field(name, value, track_provided: true)
@@ -181,6 +206,16 @@ module Subroutine
181
206
  end
182
207
  end
183
208
 
209
+ def fields_in_group(group_name)
210
+ self.class.fields_in_group(group_name)
211
+ end
212
+
213
+ def ungrouped_fields
214
+ fields.select { |f| f.groups.empty? }.each_with_object({}) do |f, h|
215
+ h[f.name] = f
216
+ end
217
+ end
218
+
184
219
  protected
185
220
 
186
221
  def mass_assign_initial_params
@@ -191,33 +226,16 @@ module Subroutine
191
226
 
192
227
  if original_params.key?(field_name)
193
228
  set_field(field_name, original_params[field_name])
194
- elsif defaults.key?(field_name)
195
- set_field(field_name, defaults[field_name], track_provided: false)
196
229
  end
197
- end
198
- end
199
230
 
200
- def build_defaults
201
- out = {}.with_indifferent_access
231
+ next unless config.has_default?
202
232
 
203
- field_configurations.each_pair do |field, config|
204
- next unless config.key?(:default)
205
-
206
- deflt = config[:default]
207
- if deflt.respond_to?(:call)
208
- deflt = deflt.call
209
- elsif deflt.try(:duplicable?) # from active_support
210
- # Some classes of default values need to be duplicated, or the instance field value will end up referencing
211
- # the class global default value, and potentially modify it.
212
- deflt = deflt.deep_dup # from active_support
213
- end
214
-
215
- out[field.to_s] = attempt_cast(deflt, config) do |e|
233
+ value = attempt_cast(config.get_default, config) do |e|
216
234
  "Error for default `#{field}`: #{e}"
217
235
  end
218
- end
219
236
 
220
- out
237
+ param_groups[:default][field_name] = value
238
+ end
221
239
  end
222
240
 
223
241
  def attempt_cast(value, config)
@@ -5,7 +5,7 @@ module Subroutine
5
5
  MAJOR = 0
6
6
  MINOR = 10
7
7
  PATCH = 0
8
- PRE = "beta2"
8
+ PRE = "beta3"
9
9
 
10
10
  VERSION = [MAJOR, MINOR, PATCH, PRE].compact.join(".")
11
11
 
@@ -166,12 +166,16 @@ module Subroutine
166
166
  op = ::AdminSignupOp.new(email: "foo")
167
167
  assert_equal({
168
168
  "email" => "foo",
169
- "privileges" => "min",
170
169
  }, op.params)
171
170
 
172
171
  assert_equal({
173
172
  "privileges" => "min",
174
173
  }, op.defaults)
174
+
175
+ assert_equal({
176
+ "email" => "foo",
177
+ "privileges" => "min",
178
+ }, op.params_with_defaults)
175
179
  end
176
180
 
177
181
  def test_it_allows_defaults_to_be_overridden
@@ -73,16 +73,17 @@ module Subroutine
73
73
  end
74
74
  end
75
75
 
76
- def test_params_include_defaults
76
+ def test_params_does_not_include_defaults
77
77
  instance = Whatever.new(foo: "abc")
78
78
  assert_equal({ "foo" => "foo", "bar" => 3, "qux" => "qux" }, instance.defaults)
79
- assert_equal({ "foo" => "abc", "qux" => "qux" }, instance.params)
80
- assert_equal({ "foo" => "abc", "bar" => 3, "qux" => "qux" }, instance.all_params)
79
+ assert_equal({ "foo" => "abc" }, instance.params)
80
+ assert_equal({ "foo" => "abc", "bar" => 3, "qux" => "qux" }, instance.params_with_defaults)
81
81
  end
82
82
 
83
- def test_named_params_include_defaults
83
+ def test_named_params_do_not_include_defaults_unlesss_asked_for
84
84
  instance = Whatever.new(foo: "abc")
85
- assert_equal({ "bar" => 3 }, instance.sekret_params)
85
+ assert_equal({}, instance.sekret_params)
86
+ assert_equal({ "bar" => 3 }, instance.sekret_params_with_defaults)
86
87
  end
87
88
 
88
89
  def test_fields_can_opt_out_of_mass_assignment
@@ -122,7 +123,8 @@ module Subroutine
122
123
  def test_groups_fields_are_accessible
123
124
  op = Whatever.new(foo: "bar", protekted_group_input: "pgi", bar: 8)
124
125
  assert_equal({ protekted_group_input: "pgi", bar: 8 }.with_indifferent_access, op.sekret_params)
125
- assert_equal({ foo: "bar", qux: "qux" }.with_indifferent_access, op.params)
126
+ assert_equal({ protekted_group_input: "pgi", foo: "bar", bar: 8 }.with_indifferent_access, op.params)
127
+ assert_equal({ foo: "bar" }.with_indifferent_access, op.ungrouped_params)
126
128
  end
127
129
 
128
130
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: subroutine
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.10.0.beta2
4
+ version: 0.10.0.beta3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mike Nelson
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-11-27 00:00:00.000000000 Z
11
+ date: 2020-01-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel