rails_param 0.10.1 → 0.10.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +9 -2
- data/lib/rails_param/param.rb +17 -6
- data/lib/rails_param/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 86a75a7469730b59de76372987ffe4a28f4ebfc5f4b2956a0052af6b70c32bf9
|
4
|
+
data.tar.gz: fe6a7b5bf1dd8ba1615243c07dfb215d4714b4236674146333ecf42ce2539f6f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f0af1329f2b47c1267b87dc3c850760e223c85177202b0a1d2b4c6d2fdb6e8fdaff055b3500182862f3be52ac760679673a879329b4f2fd1b9a1b61155fbfd12
|
7
|
+
data.tar.gz: b1483e1c051a615a6517e93d6209194d1da6911f277acf46163485a8be15b88a1ca6258bf64ae3cc52a9cc732861c02733dc710183da49f6ec020a1abfde3011
|
data/README.md
CHANGED
@@ -1,6 +1,7 @@
|
|
1
|
-
#
|
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
|
@@ -76,6 +77,12 @@ You may use the [rescue_from](http://api.rubyonrails.org/classes/ActiveSupport/R
|
|
76
77
|
- `min` / `max`
|
77
78
|
- `format`
|
78
79
|
|
80
|
+
Customize exception message with option `:message`
|
81
|
+
|
82
|
+
```ruby
|
83
|
+
param! :q, String, required: true, message: "Query not specified"
|
84
|
+
```
|
85
|
+
|
79
86
|
### Defaults and Transformations
|
80
87
|
|
81
88
|
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 +154,4 @@ Nicolas Blanco
|
|
147
154
|
|
148
155
|
## License
|
149
156
|
|
150
|
-
|
157
|
+
rails_param is available under the MIT license. See the LICENSE file for more info.
|
data/lib/rails_param/param.rb
CHANGED
@@ -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
|
@@ -28,7 +35,7 @@ module RailsParam
|
|
28
35
|
end
|
29
36
|
|
30
37
|
# apply tranformation
|
31
|
-
if params
|
38
|
+
if params.include?(name) && options[:transform]
|
32
39
|
params[name] = options[:transform].to_proc.call(params[name])
|
33
40
|
end
|
34
41
|
|
@@ -99,9 +106,13 @@ module RailsParam
|
|
99
106
|
return Integer(param) if type == Integer
|
100
107
|
return Float(param) if type == Float
|
101
108
|
return String(param) if type == String
|
102
|
-
|
103
|
-
|
104
|
-
|
109
|
+
if TIME_TYPES.include? type
|
110
|
+
if options[:format].present?
|
111
|
+
return type.strptime(param, options[:format])
|
112
|
+
else
|
113
|
+
return type.parse(param)
|
114
|
+
end
|
115
|
+
end
|
105
116
|
return Array(param.split(options[:delimiter] || ",")) if type == Array
|
106
117
|
return Hash[param.split(options[:delimiter] || ",").map { |c| c.split(options[:separator] || ":") }] if type == Hash
|
107
118
|
if type == TrueClass || type == FalseClass || type == :boolean
|
@@ -135,8 +146,8 @@ module RailsParam
|
|
135
146
|
param.nil?
|
136
147
|
end
|
137
148
|
when :format
|
138
|
-
raise InvalidParameterError, "Parameter #{param_name} must be a string if using the format validation" unless param.kind_of?
|
139
|
-
raise InvalidParameterError, "Parameter #{param_name} must match format #{value}"
|
149
|
+
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 }
|
150
|
+
raise InvalidParameterError, "Parameter #{param_name} must match format #{value}" if param.kind_of?(String) && param !~ value
|
140
151
|
when :is
|
141
152
|
raise InvalidParameterError, "Parameter #{param_name} must be #{value}" unless param === value
|
142
153
|
when :in, :within, :range
|
data/lib/rails_param/version.rb
CHANGED
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.
|
4
|
+
version: 0.10.2
|
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
|
+
date: 2018-11-07 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.
|
100
|
+
rubygems_version: 2.7.8
|
101
101
|
signing_key:
|
102
102
|
specification_version: 4
|
103
103
|
summary: Parameter Validation and Type Coercion for Rails
|