params_ready 0.0.5 → 0.0.7

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: 3fe16e13531ab2ae207b8656a5daf5e71ad4db06adf638936e5a4f80803dc0a8
4
- data.tar.gz: 0357fb20e9c206e68a257936760d8d20c1c9fdf863c61f50d730d29340362742
3
+ metadata.gz: b4eb98d2bab40c49a0b89e33a7a61bfd54f4c4f5ca23c2df52cbab5093b9c3e8
4
+ data.tar.gz: 3a492ccded066984574cce7a75cbc5b408edf0e7f2c5be4cb3db695120e9d2ff
5
5
  SHA512:
6
- metadata.gz: bd90dfe134db52a495589a8a56245be744e20239285afef6a04dc38f47337ecdadcbf02a758391e936d48654da3ffd9b72a938654aec05a3b9cc19390240bdf5
7
- data.tar.gz: 2389b613a4fe45e2fb32cc2469da23b761e670e6af9d5305758caa52254a66545237606046955d8ec848f4a58aea2e27c8b878f281fbecbd9e0064edc899d9d7
6
+ metadata.gz: 481b9530a79d65dd79b0ea290fc80fb258bfbe01305a0dd99b57436957dcad2be4323be9c11fad02ea45dad107714bda1d6957d291b7d54e375b1e6fa2f245cc
7
+ data.tar.gz: 1e96846e0e4c89dcc4f698387be8e8276cf3a8da70eb9623a66bc93843fdc575bdcd73a128dcb3f8261bf49676f1f1dc00c346a5a8976f7fb70598734d7a120e
data/lib/params_ready.rb CHANGED
@@ -28,7 +28,7 @@ require_relative 'params_ready/query/variable_operator_predicate'
28
28
 
29
29
 
30
30
  module ParamsReady
31
- VERSION = '0.0.5'.freeze
31
+ VERSION = '0.0.7'.freeze
32
32
 
33
33
  def self.gem_version
34
34
  ::Gem::Version.new(VERSION)
@@ -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
@@ -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
- attributes: Format.new(marshal: :none, omit: %i(undefined), naming_scheme: :standard, remap: false, local: true, name: :attributes),
104
- json: Format.new(marshal: { except: [:array, :tuple, :boolean, :number] }, omit: [], naming_scheme: :alternative, remap: true, local: false, name: :json)
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)
@@ -69,7 +69,7 @@ module ParamsReady
69
69
  for_output(format, restriction: restriction, data: data)
70
70
  end
71
71
 
72
- def for_model(format = :attributes, restriction: nil)
72
+ def for_model(format = :update, restriction: nil)
73
73
  for_output(format, restriction: restriction)
74
74
  end
75
75
 
@@ -96,7 +96,7 @@ module ParamsReady
96
96
  preprocessor: nil,
97
97
  populator: nil,
98
98
  postprocessor: nil,
99
- local: false,
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
- @local = local
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 set_local(*arr, rule: nil)
177
- if rule.nil?
178
- @local = true
179
- else
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 local?(format)
186
- case @local
187
- when nil, false
188
- false
189
- when true
190
- !format.local?
191
- when Helpers::Rule
192
- @local.include? format.name
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
- case @no_output
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
- return local?(format)
201
+ false
202
202
  when true
203
- return !format.local?
203
+ !format.local?
204
204
  when Helpers::Rule
205
- @no_output.include? format.name
205
+ rule.include?(format.name)
206
206
  else
207
- raise ParamsReadyError, "Unexpected option: '#{@no_output}'"
207
+ raise ParamsReadyError, "Unexpected rule: #{rule}"
208
208
  end
209
209
  end
210
210
 
211
- late_init :optional, boolean: true, getter: false, once: false do |value|
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 && !@local
239
- raise ParamsReadyError, "Populator set for non-local parameter '#{name}'"
231
+ if @populator && !@no_input
232
+ raise ParamsReadyError, "Populator set for input parameter '#{name}'"
240
233
  end
241
234
 
242
- if @preprocessor && @local
243
- raise ParamsReadyError, "Preprocessor set for local parameter '#{name}'"
235
+ if @preprocessor && @no_input == true
236
+ raise ParamsReadyError, "Preprocessor set for no-input parameter '#{name}'"
244
237
  end
245
238
 
246
- if @postprocessor && @local
247
- raise ParamsReadyError, "Postprocessor set for local parameter '#{name}'"
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
- _, input = find_in_hash hash, context
12
- set_from_input(input, context, validator)
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
- "#{self.class.name.split("::").last} #{self.name}: { #{inspect_content} }"
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?, :local?
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 && !default_defined?
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 optional?
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 = input
397
- elsif default_defined?
401
+ @value = Extensions::Undefined
402
+ elsif optional?
398
403
  # Clear possible previous state,
399
404
  # will be set to default on read
400
- @value = Extensions::Undefined
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
- op = @data[:operator].unwrap_or(nil)&.name || '?'
37
- val = @data[:value].unwrap_or('?')
36
+ @data[:operator].unwrap_or(nil)&.name || '?'
37
+ @data[:value].unwrap_or('?')
38
38
  else
39
39
  %w[? ?]
40
40
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: params_ready
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tomas Milsimer