sinatra-param-validator 0.11.0 → 0.12.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: 5b9804f13269515b113bb73dfe65c20304145df75e20d00f41ed0c231d6fd99e
4
- data.tar.gz: c0c522b2ad74c5c376a302ca4b9d141f981df498e42239fc1baa45baaab77e70
3
+ metadata.gz: b892596c037b0e00101f33a36c3fef30985e3514e3f0b74d706e55c27e03f921
4
+ data.tar.gz: 8c6d082dc416b889ca799fb49de1fef7f11af81656c5be29d30f8c191529a4c1
5
5
  SHA512:
6
- metadata.gz: 321cd999434f06ed80943b059aabe476745981d84f0446a247691fea1e836cbdbb011ce8adabe3278f43c5ecfc248f59b603f39aa187e332188531d1030130f0
7
- data.tar.gz: e7a9052b36681fe7f84e5b6b519f04db2ea915008511a23027ffa7c0134d9458e9df08b3f356507d149eda4f63c2ea4773c63dbb0acf90f9014a99c3d7a38635
6
+ metadata.gz: 85089ee9a10e79bd4cf56684e3e63a6487ea6015c093bebd17b720ea52851679790316f0c65bfc4b47bcfe399bc13d4a4c2062127c69c0c1743957663522cc14
7
+ data.tar.gz: 608f712978969fe990aefa8e2d1c90daa99871d9c9588c86b82fe0ae30873a256f8b27d4c834ad445014c32895a53a626090b32a1993469d8e40dd50bb079191
data/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.12.0] - 2022-07-15
4
+
5
+ - Switch from a delegator to extending the context
6
+
3
7
  ## [0.11.0] - 2022-07-15
4
8
 
5
9
  - Fix setting param values to false
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- sinatra-param-validator (0.11.0)
4
+ sinatra-param-validator (0.12.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -8,64 +8,66 @@ require_relative 'rule'
8
8
 
9
9
  module Sinatra
10
10
  module ParamValidator
11
- # Run the definition in the given scope
12
- class Parser < SimpleDelegator
13
- attr_reader :errors
14
-
15
- def initialize(context)
16
- super(context)
17
- @context = context
18
- @errors = {}
19
- end
20
-
21
- def parse(definition, *args)
22
- instance_exec(*args, &definition)
23
-
24
- self
25
- end
26
-
27
- def add_error(key, error)
28
- @errors[key] = @errors.fetch(key, []).concat(Array(error))
11
+ # Methods to add to the context for handling parameters
12
+ module ValidatorMethods
13
+ def _add_error(key, error)
14
+ @__validator_errors[key] = @__validator_errors.fetch(key, []).concat(Array(error))
29
15
  end
30
16
 
31
17
  def param(key, type, default: nil, message: nil, **args, &block)
32
- parameter = Parameter.new(@context.params[key], type, **args)
33
- update_params_hash key, parameter, default
18
+ parameter = Parameter.new(params[key], type, **args)
19
+ _update_params_hash key, parameter, default
34
20
  if parameter.valid?
35
- run_block(key, block) if block
21
+ _run_block(key, block) if block
36
22
  else
37
- add_error key, message || parameter.errors
23
+ _add_error key, message || parameter.errors
38
24
  end
39
25
  rescue NameError
40
26
  raise 'Invalid parameter type'
41
27
  end
42
28
 
43
29
  def rule(name, *args, **kwargs)
44
- rule = Rule.new(name, @context.params, *args, **kwargs)
30
+ rule = Rule.new(name, params, *args, **kwargs)
45
31
  unless rule.passes?
46
- @errors[:rules] ||= []
47
- @errors[:rules].push(rule.errors)
32
+ @__validator_errors[:rules] ||= []
33
+ @__validator_errors[:rules].push(rule.errors)
48
34
  end
49
35
  rescue NameError
50
36
  raise 'Invalid rule type'
51
37
  end
52
38
 
53
- private
54
-
55
- def run_block(key, block)
39
+ def _run_block(key, block)
56
40
  args = block.arity == 1 ? [self] : []
57
- @context.instance_exec(*args, &block)
41
+ instance_exec(*args, &block)
58
42
  rescue InvalidParameterError => e
59
- add_error key, e.message
43
+ _add_error key, e.message
60
44
  end
61
45
 
62
- def update_params_hash(key, parameter, default)
63
- if @context.params.key?(key)
64
- @context.params[key] = parameter.coerced unless parameter.coerced.nil?
46
+ def _update_params_hash(key, parameter, default)
47
+ if params.key?(key)
48
+ params[key] = parameter.coerced unless parameter.coerced.nil?
65
49
  elsif !default.nil?
66
- @context.params[key] = default.respond_to?(:call) ? default.call : default
50
+ params[key] = default.respond_to?(:call) ? default.call : default
67
51
  end
68
52
  end
69
53
  end
54
+
55
+ # Run the definition in the given scope
56
+ class Parser
57
+ attr_reader :errors
58
+
59
+ def initialize(context)
60
+ @context = context
61
+ end
62
+
63
+ def parse(definition, *args)
64
+ @context.extend(ValidatorMethods)
65
+ @context.instance_variable_set(:@__validator_errors, {})
66
+ @context.instance_exec(*args, &definition)
67
+ @errors = @context.instance_variable_get(:@__validator_errors)
68
+
69
+ self
70
+ end
71
+ end
70
72
  end
71
73
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Sinatra
4
4
  module ParamValidator
5
- VERSION = '0.11.0'
5
+ VERSION = '0.12.0'
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sinatra-param-validator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.11.0
4
+ version: 0.12.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rick Selby