params_ready 0.0.5 → 0.0.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/params_ready.rb +1 -1
- data/lib/params_ready/builder.rb +4 -0
- data/lib/params_ready/format.rb +4 -2
- data/lib/params_ready/output_parameters.rb +2 -2
- data/lib/params_ready/parameter/abstract_hash_parameter.rb +1 -1
- data/lib/params_ready/parameter/definition.rb +31 -38
- data/lib/params_ready/parameter/parameter.rb +23 -18
- data/lib/params_ready/query/variable_operator_predicate.rb +2 -2
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b4eb98d2bab40c49a0b89e33a7a61bfd54f4c4f5ca23c2df52cbab5093b9c3e8
|
4
|
+
data.tar.gz: 3a492ccded066984574cce7a75cbc5b408edf0e7f2c5be4cb3db695120e9d2ff
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 481b9530a79d65dd79b0ea290fc80fb258bfbe01305a0dd99b57436957dcad2be4323be9c11fad02ea45dad107714bda1d6957d291b7d54e375b1e6fa2f245cc
|
7
|
+
data.tar.gz: 1e96846e0e4c89dcc4f698387be8e8276cf3a8da70eb9623a66bc93843fdc575bdcd73a128dcb3f8261bf49676f1f1dc00c346a5a8976f7fb70598734d7a120e
|
data/lib/params_ready.rb
CHANGED
data/lib/params_ready/builder.rb
CHANGED
@@ -94,6 +94,10 @@ module ParamsReady
|
|
94
94
|
@definition.set_no_output Helpers::Rule(rule) || true
|
95
95
|
end
|
96
96
|
|
97
|
+
def no_input(*arr, rule: nil)
|
98
|
+
@definition.set_no_input *arr, rule: rule
|
99
|
+
end
|
100
|
+
|
97
101
|
def local(*arr, rule: nil)
|
98
102
|
@definition.set_local *arr, rule: rule
|
99
103
|
end
|
data/lib/params_ready/format.rb
CHANGED
@@ -100,8 +100,10 @@ module ParamsReady
|
|
100
100
|
@names = {
|
101
101
|
backend: Format.new(marshal: :none, omit: [], naming_scheme: :standard, remap: false, local: true, name: :backend),
|
102
102
|
frontend: Format.new(marshal: :all, omit: OMIT_ALL, naming_scheme: :alternative, remap: false, local: false, name: :frontend),
|
103
|
-
|
104
|
-
|
103
|
+
create: Format.new(marshal: :none, omit: [], naming_scheme: :standard, remap: false, local: true, name: :create),
|
104
|
+
update: Format.new(marshal: :none, omit: %i(undefined), naming_scheme: :standard, remap: false, local: true, name: :update),
|
105
|
+
json: Format.new(marshal: { except: [:array, :tuple, :boolean, :number] }, omit: [], naming_scheme: :alternative, remap: true, local: false, name: :json),
|
106
|
+
inspect: Format.new(marshal: :none, omit: [], naming_scheme: :standard, remap: false, local: false, name: :inspect)
|
105
107
|
}.freeze
|
106
108
|
|
107
109
|
def self.define(name, format)
|
@@ -104,8 +104,8 @@ module ParamsReady
|
|
104
104
|
@parameter.for_frontend(restriction: restriction, data: data)
|
105
105
|
end
|
106
106
|
|
107
|
-
def for_model(restriction: @intent.restriction)
|
108
|
-
@parameter.for_model(restriction: restriction)
|
107
|
+
def for_model(format = :update, restriction: @intent.restriction)
|
108
|
+
@parameter.for_model(format, restriction: restriction)
|
109
109
|
end
|
110
110
|
|
111
111
|
def format(format = @intent)
|
@@ -96,7 +96,7 @@ module ParamsReady
|
|
96
96
|
preprocessor: nil,
|
97
97
|
populator: nil,
|
98
98
|
postprocessor: nil,
|
99
|
-
|
99
|
+
no_input: nil,
|
100
100
|
no_output: nil,
|
101
101
|
**opts
|
102
102
|
)
|
@@ -106,7 +106,7 @@ module ParamsReady
|
|
106
106
|
@preprocessor = preprocessor
|
107
107
|
@postprocessor = postprocessor
|
108
108
|
@populator = populator
|
109
|
-
@
|
109
|
+
@no_input = no_input
|
110
110
|
@no_output = no_output
|
111
111
|
|
112
112
|
set_default(default) unless default == Extensions::Undefined
|
@@ -126,6 +126,7 @@ module ParamsReady
|
|
126
126
|
end
|
127
127
|
|
128
128
|
late_init :populator, getter: true, once: true, obligatory: false
|
129
|
+
late_init :no_input, getter: false, once: false
|
129
130
|
late_init :no_output, getter: false, once: false
|
130
131
|
late_init :memoize, getter: true, obligatory: false
|
131
132
|
|
@@ -173,53 +174,45 @@ module ParamsReady
|
|
173
174
|
validator
|
174
175
|
end
|
175
176
|
|
176
|
-
def
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
@local = Helpers::Rule(rule)
|
181
|
-
end
|
177
|
+
def set_no_input(*arr, rule: nil)
|
178
|
+
@no_input = Helpers::Rule(rule) || true
|
179
|
+
raise ParamsReadyError, "Default not expected: #{arr}" if rule == false
|
180
|
+
|
182
181
|
set_default *arr unless arr.empty?
|
183
182
|
end
|
184
183
|
|
185
|
-
def
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
else
|
194
|
-
raise ParamsReadyError, "Unexpected rule: #{@local}"
|
195
|
-
end
|
184
|
+
def set_local(*arr, rule: nil)
|
185
|
+
rule = Helpers::Rule(rule)
|
186
|
+
set_no_input(*arr, rule: rule)
|
187
|
+
set_no_output(rule || true)
|
188
|
+
end
|
189
|
+
|
190
|
+
def no_input?(format)
|
191
|
+
restricted_for_format?(@no_input, format)
|
196
192
|
end
|
197
193
|
|
198
194
|
def no_output?(format)
|
199
|
-
|
195
|
+
restricted_for_format?(@no_output, format)
|
196
|
+
end
|
197
|
+
|
198
|
+
def restricted_for_format?(rule, format)
|
199
|
+
case rule
|
200
200
|
when nil, false
|
201
|
-
|
201
|
+
false
|
202
202
|
when true
|
203
|
-
|
203
|
+
!format.local?
|
204
204
|
when Helpers::Rule
|
205
|
-
|
205
|
+
rule.include?(format.name)
|
206
206
|
else
|
207
|
-
raise ParamsReadyError, "Unexpected
|
207
|
+
raise ParamsReadyError, "Unexpected rule: #{rule}"
|
208
208
|
end
|
209
209
|
end
|
210
210
|
|
211
|
-
late_init :optional, boolean: true, getter: false, once: false
|
212
|
-
next value if value == false
|
213
|
-
raise ParamsReadyError, "Optional parameter can't have default" if default_defined?
|
214
|
-
|
215
|
-
value
|
216
|
-
end
|
211
|
+
late_init :optional, boolean: true, getter: false, once: false
|
217
212
|
|
218
213
|
late_init :default, once: false, definite: false do |value|
|
219
214
|
next value if value == Extensions::Undefined
|
220
215
|
|
221
|
-
raise ParamsReadyError, "Optional parameter can't have default" if optional?
|
222
|
-
|
223
216
|
canonical = canonical_default(value)
|
224
217
|
next canonical if canonical.nil?
|
225
218
|
|
@@ -235,16 +228,16 @@ module ParamsReady
|
|
235
228
|
|
236
229
|
|
237
230
|
def finish
|
238
|
-
if @populator && !@
|
239
|
-
raise ParamsReadyError, "Populator set for
|
231
|
+
if @populator && !@no_input
|
232
|
+
raise ParamsReadyError, "Populator set for input parameter '#{name}'"
|
240
233
|
end
|
241
234
|
|
242
|
-
if @preprocessor && @
|
243
|
-
raise ParamsReadyError, "Preprocessor set for
|
235
|
+
if @preprocessor && @no_input == true
|
236
|
+
raise ParamsReadyError, "Preprocessor set for no-input parameter '#{name}'"
|
244
237
|
end
|
245
238
|
|
246
|
-
if @postprocessor && @
|
247
|
-
raise ParamsReadyError, "Postprocessor set for
|
239
|
+
if @postprocessor && @no_input == true
|
240
|
+
raise ParamsReadyError, "Postprocessor set for no-input parameter '#{name}'"
|
248
241
|
end
|
249
242
|
|
250
243
|
super
|
@@ -8,8 +8,12 @@ module ParamsReady
|
|
8
8
|
module Parameter
|
9
9
|
module FromHash
|
10
10
|
def set_from_hash(hash, context: nil, validator: Result.new(name))
|
11
|
-
|
12
|
-
|
11
|
+
if no_input?(context)
|
12
|
+
populate(context, validator)
|
13
|
+
else
|
14
|
+
_, input = find_in_hash hash, context
|
15
|
+
set_from_input(input, context, validator)
|
16
|
+
end
|
13
17
|
end
|
14
18
|
end
|
15
19
|
|
@@ -184,7 +188,9 @@ module ParamsReady
|
|
184
188
|
end
|
185
189
|
|
186
190
|
def inspect
|
187
|
-
|
191
|
+
preserve = Format.instance(:inspect).preserve?(self)
|
192
|
+
content = preserve ? inspect_content : '[FILTERED]'
|
193
|
+
"#{self.class.name.split("::").last} #{self.name}: { #{content} }"
|
188
194
|
end
|
189
195
|
|
190
196
|
def dup
|
@@ -205,7 +211,7 @@ module ParamsReady
|
|
205
211
|
|
206
212
|
class Parameter < AbstractParameter
|
207
213
|
def_delegators :@definition,
|
208
|
-
:default, :optional?, :default_defined?, :constraints, :no_output?, :
|
214
|
+
:default, :optional?, :default_defined?, :constraints, :no_output?, :no_input?
|
209
215
|
|
210
216
|
def initialize(definition)
|
211
217
|
@value = Extensions::Undefined
|
@@ -248,6 +254,7 @@ module ParamsReady
|
|
248
254
|
|
249
255
|
def is_definite?
|
250
256
|
return true if @value != Extensions::Undefined && !@value.nil?
|
257
|
+
return false if optional? && @value.nil?
|
251
258
|
|
252
259
|
definite_default?
|
253
260
|
end
|
@@ -267,7 +274,13 @@ module ParamsReady
|
|
267
274
|
end
|
268
275
|
|
269
276
|
def is_undefined?
|
270
|
-
@value == Extensions::Undefined &&
|
277
|
+
@value == Extensions::Undefined && allows_undefined?
|
278
|
+
end
|
279
|
+
|
280
|
+
def allows_undefined?
|
281
|
+
return true if optional?
|
282
|
+
|
283
|
+
!default_defined?
|
271
284
|
end
|
272
285
|
|
273
286
|
def eligible_for_output?(intent)
|
@@ -278,14 +291,6 @@ module ParamsReady
|
|
278
291
|
format.hash_key(self)
|
279
292
|
end
|
280
293
|
|
281
|
-
def set_from_hash(hash, context: nil, validator: Result.new(name))
|
282
|
-
if local?(context)
|
283
|
-
populate(context, validator)
|
284
|
-
else
|
285
|
-
super
|
286
|
-
end
|
287
|
-
end
|
288
|
-
|
289
294
|
def set_from_input(input, context, validator)
|
290
295
|
preprocessed = definition.preprocess(input, context, validator)
|
291
296
|
set_value preprocessed, context, validator
|
@@ -389,17 +394,17 @@ module ParamsReady
|
|
389
394
|
|
390
395
|
def handle_indefinite_input(input, validator)
|
391
396
|
value_missing validator
|
392
|
-
if
|
397
|
+
if default_defined?
|
393
398
|
# if value_missing doesn't crash,
|
394
399
|
# and the parameter is optional
|
395
400
|
# it's safe to set to nil or Extensions::Undefined
|
396
|
-
@value =
|
397
|
-
elsif
|
401
|
+
@value = Extensions::Undefined
|
402
|
+
elsif optional?
|
398
403
|
# Clear possible previous state,
|
399
404
|
# will be set to default on read
|
400
|
-
@value =
|
405
|
+
@value = input
|
401
406
|
else
|
402
|
-
raise ParamsReadyError, "Unexpected state in #handle_indefinite_input" if validator.ok?
|
407
|
+
raise ParamsReadyError, "Unexpected state for '#{name}' in #handle_indefinite_input" if validator.ok?
|
403
408
|
end
|
404
409
|
end
|
405
410
|
|
@@ -33,8 +33,8 @@ module ParamsReady
|
|
33
33
|
|
34
34
|
def inspect_content
|
35
35
|
op, val = if is_definite?
|
36
|
-
|
37
|
-
|
36
|
+
@data[:operator].unwrap_or(nil)&.name || '?'
|
37
|
+
@data[:value].unwrap_or('?')
|
38
38
|
else
|
39
39
|
%w[? ?]
|
40
40
|
end
|