rails_param 1.1.0 → 1.2.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: e99b0ab6dac912811e4d43f26b9fae9d14363bbdf619fc72fc79f9b9eb9caf31
4
- data.tar.gz: 0bce166f4e27fb282758307f6aa76a03959977ecc41c22a497fc6e02f663f9a1
3
+ metadata.gz: a28d7334766ecd5031f21b67d72d5e5b40441ba8494da496cc64a192401fccfc
4
+ data.tar.gz: 3ce0095c900ba61a7fe864cb175f19c5cf2a0decc50bfdbc038a3cd6c3b686a1
5
5
  SHA512:
6
- metadata.gz: f2d475689feed854e28bc075527c5b8528de9f5215ae5e13ff95b0a7a740106e61c04da3f44be9ffa8a40d082f93cb2d6fb145fe5ee37694a14bf0a14181d4d0
7
- data.tar.gz: cbda11bbce71ec5daeefc3fe9d2ba78ec3b3a652296efc57ac7cf8a5038ac45f9e8a99c4a7a4b1d5ce3f0a697136be177f1666095af1b4fafd3e501ee6486ff8
6
+ metadata.gz: 18c1d4e654c0ca5907452f40dbe89291b90772cdd822e900cb10a243a45bcdb35568daf62522b9537ef514fc4da3f19ea471f75ea55c450977d2fbcf8bc796c9
7
+ data.tar.gz: e09a3c0ae114cfda47ce329349934580ffa87f4976934ceb27ae0c2c07858131330aeea47ac41c821b736f2004b5a791616f01a21bf1c0aff8e50fcea3a9899a
@@ -2,18 +2,20 @@ module RailsParam
2
2
  class ParamEvaluator
3
3
  attr_accessor :params
4
4
 
5
- def initialize(params)
5
+ def initialize(params, context = nil)
6
6
  @params = params
7
+ @context = context
7
8
  end
8
9
 
9
10
  def param!(name, type, options = {}, &block)
10
11
  name = name.is_a?(Integer)? name : name.to_s
11
12
  return unless params.include?(name) || check_param_presence?(options[:default]) || options[:required]
12
13
 
14
+ parameter_name = @context ? "#{@context}[#{name}]" : name
13
15
  coerced_value = coerce(params[name], type, options)
14
16
 
15
17
  parameter = RailsParam::Parameter.new(
16
- name: name,
18
+ name: parameter_name,
17
19
  value: coerced_value,
18
20
  type: type,
19
21
  options: options,
@@ -25,8 +27,8 @@ module RailsParam
25
27
  # validate presence
26
28
  if params[name].nil? && options[:required]
27
29
  raise InvalidParameterError.new(
28
- "Parameter #{name} is required",
29
- param: name,
30
+ "Parameter #{parameter_name} is required",
31
+ param: parameter_name,
30
32
  options: options
31
33
  )
32
34
  end
@@ -51,20 +53,20 @@ module RailsParam
51
53
  if parameter.type == Array
52
54
  parameter.value.each_with_index do |element, i|
53
55
  if element.is_a?(Hash) || element.is_a?(ActionController::Parameters)
54
- recurse element, &block
56
+ recurse element, "#{parameter.name}[#{i}]", &block
55
57
  else
56
- parameter.value[i] = recurse({ i => element }, i, &block) # supply index as key unless value is hash
58
+ parameter.value[i] = recurse({ i => element }, parameter.name, i, &block) # supply index as key unless value is hash
57
59
  end
58
60
  end
59
61
  else
60
- recurse parameter.value, &block
62
+ recurse parameter.value, parameter.name, &block
61
63
  end
62
64
  end
63
65
 
64
- def recurse(element, index = nil)
66
+ def recurse(element, context, index = nil)
65
67
  raise InvalidParameterError, 'no block given' unless block_given?
66
68
 
67
- yield(ParamEvaluator.new(element), index)
69
+ yield(ParamEvaluator.new(element, context), index)
68
70
  end
69
71
 
70
72
  def check_param_presence? param
@@ -1,3 +1,3 @@
1
1
  module RailsParam #:nodoc
2
- VERSION = "1.1.0"
2
+ VERSION = "1.2.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails_param
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nicolas Blanco
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-11-26 00:00:00.000000000 Z
11
+ date: 2021-12-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec