rails_param 0.9.0 → 0.11.1

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
- SHA1:
3
- metadata.gz: 372c36387af839acafd00fac76073bcdd666b19e
4
- data.tar.gz: 2d9d467a8352bf7f0e671ab8324af3993fcc64f1
2
+ SHA256:
3
+ metadata.gz: 9a684ef9319b35a00602467f103f2377fdef1b9a2dd3b711ee1d8ca38d01ed8a
4
+ data.tar.gz: e2682cd56dc1b84939284de6f796cad46f8f015b7de9db72e986a554460dfb48
5
5
  SHA512:
6
- metadata.gz: c616739e789c723637b6f1b545ecf261a957645f8d92c7bafe456850e49d99420be36d775c3b1af7727ed4d1eac307658390215d94e6e49cfdfec01ee0c66849
7
- data.tar.gz: 5079e1ba2ff3c5f1b634ad8cc0e9760633a07fa449d601030e079eeb7505512293de6a45559303885e8ae2e9708bb105e609b89fe132916ef1b2e07ecbfc9cf2
6
+ metadata.gz: e02a281c85694dc3db8f0862b169800d01d5d4a206341cf49b62536b524cb5b3ce6b992f71b7f8310cf74795e403923ec1bc72b45ded00b7b259540d2666ccc1
7
+ data.tar.gz: c312758e48df2d5f96f9eea11640e00c991e67faa7aae7a70d7ce705e9132ba2c6947eb40309f0eb3bddd435031ececad9dcd12aa6b46dd9f137d9c9f7373886
data/README.md CHANGED
@@ -1,6 +1,7 @@
1
- # rails-param
1
+ # rails_param
2
2
  _Parameter Validation & Type Coercion for Rails_
3
3
 
4
+ [![Gem Version](https://badge.fury.io/rb/rails_param.svg)](https://rubygems.org/gems/rails_param)
4
5
  [![Build Status](https://travis-ci.org/nicolasblanco/rails_param.svg?branch=master)](https://travis-ci.org/nicolasblanco/rails_param)
5
6
 
6
7
  ## Introduction
@@ -74,8 +75,15 @@ You may use the [rescue_from](http://api.rubyonrails.org/classes/ActiveSupport/R
74
75
  - `is`
75
76
  - `in`, `within`, `range`
76
77
  - `min` / `max`
78
+ - `min_length` / `max_length`
77
79
  - `format`
78
80
 
81
+ Customize exception message with option `:message`
82
+
83
+ ```ruby
84
+ param! :q, String, required: true, message: "Query not specified"
85
+ ```
86
+
79
87
  ### Defaults and Transformations
80
88
 
81
89
  Passing a `default` option will provide a default value for a parameter if none is passed. A `default` can defined as either a default or as a `Proc`:
@@ -147,4 +155,4 @@ Nicolas Blanco
147
155
 
148
156
  ## License
149
157
 
150
- rails-param is available under the MIT license. See the LICENSE file for more info.
158
+ rails_param is available under the MIT license. See the LICENSE file for more info.
@@ -2,9 +2,16 @@ module RailsParam
2
2
  module Param
3
3
 
4
4
  DEFAULT_PRECISION = 14
5
+ TIME_TYPES = [Date, DateTime, Time].freeze
6
+ STRING_OR_TIME_TYPES = ([String] + TIME_TYPES).freeze
5
7
 
6
8
  class InvalidParameterError < StandardError
7
9
  attr_accessor :param, :options
10
+
11
+ def message
12
+ return options[:message] if options.is_a?(Hash) && options.key?(:message)
13
+ super
14
+ end
8
15
  end
9
16
 
10
17
  class MockController
@@ -14,19 +21,34 @@ module RailsParam
14
21
 
15
22
  def param!(name, type, options = {}, &block)
16
23
  name = name.to_s unless name.is_a? Integer # keep index for validating elements
17
-
18
- return unless params.member?(name) || check_param_presence?(options[:default]) || options[:required]
24
+
25
+ return unless params.include?(name) || check_param_presence?(options[:default]) || options[:required]
19
26
 
20
27
  begin
21
28
  params[name] = coerce(params[name], type, options)
22
- params[name] = (options[:default].call if options[:default].respond_to?(:call)) || options[:default] if params[name].nil? and check_param_presence?(options[:default])
23
- params[name] = options[:transform].to_proc.call(params[name]) if params[name] and options[:transform]
24
- validate!(params[name], options)
29
+
30
+ # set default
31
+ if params[name].nil? && check_param_presence?(options[:default])
32
+ params[name] = options[:default].respond_to?(:call) ? options[:default].call : options[:default]
33
+ end
34
+
35
+ # validate presence
36
+ if params[name].nil? && options[:required]
37
+ raise InvalidParameterError, "Parameter #{name} is required"
38
+ end
39
+
40
+ # apply transformation
41
+ if params.include?(name) && options[:transform]
42
+ params[name] = options[:transform].to_proc.call(params[name])
43
+ end
44
+
45
+ # validate
46
+ validate!(params[name], name, options)
25
47
 
26
48
  if block_given?
27
49
  if type == Array
28
50
  params[name].each_with_index do |element, i|
29
- if element.is_a?(Hash)
51
+ if element.is_a?(Hash) || element.is_a?(ActionController::Parameters)
30
52
  recurse element, &block
31
53
  else
32
54
  params[name][i] = recurse({ i => element }, i, &block) # supply index as key unless value is hash
@@ -80,59 +102,73 @@ module RailsParam
80
102
  begin
81
103
  return nil if param.nil?
82
104
  return param if (param.is_a?(type) rescue false)
105
+ if (param.is_a?(Array) && type != Array) || ((param.is_a?(Hash) || param.is_a?(ActionController::Parameters)) && type != Hash)
106
+ raise ArgumentError
107
+ end
108
+ return param if (param.is_a?(ActionController::Parameters) && type == Hash rescue false)
83
109
  return Integer(param) if type == Integer
84
110
  return Float(param) if type == Float
85
111
  return String(param) if type == String
86
- return Date.parse(param) if type == Date
87
- return Time.parse(param) if type == Time
88
- return DateTime.parse(param) if type == DateTime
112
+ if TIME_TYPES.include? type
113
+ if options[:format].present?
114
+ return type.strptime(param, options[:format])
115
+ else
116
+ return type.parse(param)
117
+ end
118
+ end
119
+ raise ArgumentError if (type == Array || type == Hash) && !param.respond_to?(:split)
89
120
  return Array(param.split(options[:delimiter] || ",")) if type == Array
90
121
  return Hash[param.split(options[:delimiter] || ",").map { |c| c.split(options[:separator] || ":") }] if type == Hash
91
- return (/^(false|f|no|n|0)$/i === param.to_s ? false : (/^(true|t|yes|y|1)$/i === param.to_s ? true : (raise ArgumentError))) if type == TrueClass || type == FalseClass || type == :boolean
122
+ if type == TrueClass || type == FalseClass || type == :boolean
123
+ return false if /^(false|f|no|n|0)$/i === param.to_s
124
+ return true if /^(true|t|yes|y|1)$/i === param.to_s
125
+
126
+ raise ArgumentError
127
+ end
92
128
  if type == BigDecimal
93
129
  param = param.delete('$,').strip.to_f if param.is_a?(String)
94
- return BigDecimal.new(param, (options[:precision] || DEFAULT_PRECISION))
130
+ return BigDecimal(param, (options[:precision] || DEFAULT_PRECISION))
95
131
  end
96
132
  return nil
97
- rescue ArgumentError
133
+ rescue ArgumentError, TypeError
98
134
  raise InvalidParameterError, "'#{param}' is not a valid #{type}"
99
135
  end
100
136
  end
101
137
 
102
- def validate!(param, options)
138
+ def validate!(param, param_name, options)
103
139
  options.each do |key, value|
104
140
  case key
105
- when :required
106
- raise InvalidParameterError, "Parameter is required" if value && param.nil?
107
141
  when :blank
108
- raise InvalidParameterError, "Parameter cannot be blank" if !value && case param
142
+ raise InvalidParameterError, "Parameter #{param_name} cannot be blank" if !value && case param
109
143
  when String
110
144
  !(/\S/ === param)
111
- when Array, Hash
145
+ when Array, Hash, ActionController::Parameters
112
146
  param.empty?
113
147
  else
114
148
  param.nil?
115
149
  end
116
150
  when :format
117
- raise InvalidParameterError, "Parameter must be a string if using the format validation" unless param.kind_of?(String)
118
- raise InvalidParameterError, "Parameter must match format #{value}" unless param =~ value
151
+ raise InvalidParameterError, "Parameter #{param_name} must be a string if using the format validation" unless STRING_OR_TIME_TYPES.any? { |cls| param.kind_of? cls }
152
+ raise InvalidParameterError, "Parameter #{param_name} must match format #{value}" if param.kind_of?(String) && param !~ value
119
153
  when :is
120
- raise InvalidParameterError, "Parameter must be #{value}" unless param === value
154
+ raise InvalidParameterError, "Parameter #{param_name} must be #{value}" unless param === value
121
155
  when :in, :within, :range
122
- raise InvalidParameterError, "Parameter must be within #{value}" unless param.nil? || case value
156
+ raise InvalidParameterError, "Parameter #{param_name} must be within #{value}" unless param.nil? || case value
123
157
  when Range
124
158
  value.include?(param)
125
159
  else
126
160
  Array(value).include?(param)
127
161
  end
128
162
  when :min
129
- raise InvalidParameterError, "Parameter cannot be less than #{value}" unless param.nil? || value <= param
163
+ raise InvalidParameterError, "Parameter #{param_name} cannot be less than #{value}" unless param.nil? || value <= param
130
164
  when :max
131
- raise InvalidParameterError, "Parameter cannot be greater than #{value}" unless param.nil? || value >= param
165
+ raise InvalidParameterError, "Parameter #{param_name} cannot be greater than #{value}" unless param.nil? || value >= param
132
166
  when :min_length
133
- raise InvalidParameterError, "Parameter cannot have length less than #{value}" unless param.nil? || value <= param.length
167
+ raise InvalidParameterError, "Parameter #{param_name} cannot have length less than #{value}" unless param.nil? || value <= param.length
134
168
  when :max_length
135
- raise InvalidParameterError, "Parameter cannot have length greater than #{value}" unless param.nil? || value >= param.length
169
+ raise InvalidParameterError, "Parameter #{param_name} cannot have length greater than #{value}" unless param.nil? || value >= param.length
170
+ when :custom
171
+ value.call(param)
136
172
  end
137
173
  end
138
174
  end
@@ -1,3 +1,3 @@
1
1
  module RailsParam #:nodoc
2
- VERSION = "0.9.0"
2
+ VERSION = "0.11.1"
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.9.0
4
+ version: 0.11.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nicolas Blanco
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-09-02 00:00:00.000000000 Z
11
+ date: 2020-12-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -16,28 +16,28 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '2.11'
19
+ version: '3.4'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '2.11'
26
+ version: '3.4'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rspec-rails
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '2.11'
33
+ version: '3.4'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '2.11'
40
+ version: '3.4'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: actionpack
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -66,6 +66,20 @@ dependencies:
66
66
  - - ">="
67
67
  - !ruby/object:Gem::Version
68
68
  version: 3.2.0
69
+ - !ruby/object:Gem::Dependency
70
+ name: rails
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: 3.2.0
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: 3.2.0
69
83
  description: "\n Parameter Validation and Type Coercion for Rails\n "
70
84
  email: nicolas@nicolasblanco.fr
71
85
  executables: []
@@ -80,7 +94,7 @@ homepage: http://github.com/nicolasblanco/rails_param
80
94
  licenses:
81
95
  - MIT
82
96
  metadata: {}
83
- post_install_message:
97
+ post_install_message:
84
98
  rdoc_options:
85
99
  - "--charset=UTF-8"
86
100
  require_paths:
@@ -96,9 +110,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
96
110
  - !ruby/object:Gem::Version
97
111
  version: 1.3.6
98
112
  requirements: []
99
- rubyforge_project:
100
- rubygems_version: 2.4.8
101
- signing_key:
113
+ rubygems_version: 3.1.2
114
+ signing_key:
102
115
  specification_version: 4
103
116
  summary: Parameter Validation and Type Coercion for Rails
104
117
  test_files: []