rails_param 0.10.2 → 0.11.0

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: 86a75a7469730b59de76372987ffe4a28f4ebfc5f4b2956a0052af6b70c32bf9
4
- data.tar.gz: fe6a7b5bf1dd8ba1615243c07dfb215d4714b4236674146333ecf42ce2539f6f
3
+ metadata.gz: 597be5889d0a852cb2be5b0c828adf8e84b741cbe215ee75e2a5dd47359faff8
4
+ data.tar.gz: 2169c1ab2013a2a6daef049abc3ee75d8237811debdbf73920abcec7b089985b
5
5
  SHA512:
6
- metadata.gz: f0af1329f2b47c1267b87dc3c850760e223c85177202b0a1d2b4c6d2fdb6e8fdaff055b3500182862f3be52ac760679673a879329b4f2fd1b9a1b61155fbfd12
7
- data.tar.gz: b1483e1c051a615a6517e93d6209194d1da6911f277acf46163485a8be15b88a1ca6258bf64ae3cc52a9cc732861c02733dc710183da49f6ec020a1abfde3011
6
+ metadata.gz: f1385cb2140b91b545a820056797f7fe9af681df0e059a03955717945ec75c53b722bca571ce740ef322c09a0ac28e0cef3334d224348308c99d39272054827b
7
+ data.tar.gz: e53ab6824b887a24933ae452f71af269ef8d33776221007f49d0055d175b1dc74bcce475f7a41347f0fa84ecda55ea92afbbacdd6f192ab00998fb1f7fdadc64
@@ -28,13 +28,11 @@ module RailsParam
28
28
  params[name] = coerce(params[name], type, options)
29
29
 
30
30
  # set default
31
- if options[:default].respond_to?(:call)
32
- params[name] = options[:default].call
33
- elsif params[name].nil? && check_param_presence?(options[:default])
34
- params[name] = options[:default]
31
+ if params[name].nil? && check_param_presence?(options[:default])
32
+ params[name] = options[:default].respond_to?(:call) ? options[:default].call : options[:default]
35
33
  end
36
34
 
37
- # apply tranformation
35
+ # apply transformation
38
36
  if params.include?(name) && options[:transform]
39
37
  params[name] = options[:transform].to_proc.call(params[name])
40
38
  end
@@ -100,7 +98,7 @@ module RailsParam
100
98
  return nil if param.nil?
101
99
  return param if (param.is_a?(type) rescue false)
102
100
  if (param.is_a?(Array) && type != Array) || ((param.is_a?(Hash) || param.is_a?(ActionController::Parameters)) && type != Hash)
103
- raise InvalidParameterError, "'#{param}' is not a valid #{type}"
101
+ raise ArgumentError
104
102
  end
105
103
  return param if (param.is_a?(ActionController::Parameters) && type == Hash rescue false)
106
104
  return Integer(param) if type == Integer
@@ -113,6 +111,7 @@ module RailsParam
113
111
  return type.parse(param)
114
112
  end
115
113
  end
114
+ raise ArgumentError if (type == Array || type == Hash) && !param.respond_to?(:split)
116
115
  return Array(param.split(options[:delimiter] || ",")) if type == Array
117
116
  return Hash[param.split(options[:delimiter] || ",").map { |c| c.split(options[:separator] || ":") }] if type == Hash
118
117
  if type == TrueClass || type == FalseClass || type == :boolean
@@ -123,10 +122,10 @@ module RailsParam
123
122
  end
124
123
  if type == BigDecimal
125
124
  param = param.delete('$,').strip.to_f if param.is_a?(String)
126
- return BigDecimal.new(param, (options[:precision] || DEFAULT_PRECISION))
125
+ return BigDecimal(param, (options[:precision] || DEFAULT_PRECISION))
127
126
  end
128
127
  return nil
129
- rescue ArgumentError
128
+ rescue ArgumentError, TypeError
130
129
  raise InvalidParameterError, "'#{param}' is not a valid #{type}"
131
130
  end
132
131
  end
@@ -140,7 +139,7 @@ module RailsParam
140
139
  raise InvalidParameterError, "Parameter #{param_name} cannot be blank" if !value && case param
141
140
  when String
142
141
  !(/\S/ === param)
143
- when Array, Hash
142
+ when Array, Hash, ActionController::Parameters
144
143
  param.empty?
145
144
  else
146
145
  param.nil?
@@ -165,6 +164,8 @@ module RailsParam
165
164
  raise InvalidParameterError, "Parameter #{param_name} cannot have length less than #{value}" unless param.nil? || value <= param.length
166
165
  when :max_length
167
166
  raise InvalidParameterError, "Parameter #{param_name} cannot have length greater than #{value}" unless param.nil? || value >= param.length
167
+ when :custom
168
+ value.call(param)
168
169
  end
169
170
  end
170
171
  end
@@ -1,3 +1,3 @@
1
1
  module RailsParam #:nodoc
2
- VERSION = "0.10.2"
2
+ VERSION = "0.11.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: 0.10.2
4
+ version: 0.11.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: 2018-11-07 00:00:00.000000000 Z
11
+ date: 2019-09-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -97,7 +97,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
97
97
  version: 1.3.6
98
98
  requirements: []
99
99
  rubyforge_project:
100
- rubygems_version: 2.7.8
100
+ rubygems_version: 2.7.7
101
101
  signing_key:
102
102
  specification_version: 4
103
103
  summary: Parameter Validation and Type Coercion for Rails