rails_param 0.10.2 → 1.0.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: 86a75a7469730b59de76372987ffe4a28f4ebfc5f4b2956a0052af6b70c32bf9
4
- data.tar.gz: fe6a7b5bf1dd8ba1615243c07dfb215d4714b4236674146333ecf42ce2539f6f
3
+ metadata.gz: d0d3e366d59b97f9e8453e2721f975afe0cbed9b29de26c35e786552707a3586
4
+ data.tar.gz: ed1a7c052cd69b75e2551cce07c2004094b0b95307490d72b88185a0da693c79
5
5
  SHA512:
6
- metadata.gz: f0af1329f2b47c1267b87dc3c850760e223c85177202b0a1d2b4c6d2fdb6e8fdaff055b3500182862f3be52ac760679673a879329b4f2fd1b9a1b61155fbfd12
7
- data.tar.gz: b1483e1c051a615a6517e93d6209194d1da6911f277acf46163485a8be15b88a1ca6258bf64ae3cc52a9cc732861c02733dc710183da49f6ec020a1abfde3011
6
+ metadata.gz: f70757e8463748ec31be8f61176a80fb1da2460af184c8a096a2abf928cf1c3507d77290e4a7f38f979829bad259e850afc4a1f0c31fd0f5bcd3fccad46834f4
7
+ data.tar.gz: 12074dad3f21ba4d0e1afd8bbc9db9ad827486dbe014f930c89d7fcc07ebed7c192da0aa31fe8b85041947541e1410d600d7ee630a5128522fc4da98b8bfd809
data/README.md CHANGED
@@ -26,6 +26,10 @@ All the credits go to [@mattt](https://twitter.com/mattt).
26
26
 
27
27
  It has all the features of the sinatra-param gem, I used bang methods (like param!) to indicate that they are destructive as they change the controller params object and may raise an exception.
28
28
 
29
+ ## Upgrading
30
+
31
+ Find a list of breaking changes in [UPGRADING](UPGRADING.md).
32
+
29
33
  ## Installation
30
34
 
31
35
  As usual, in your Gemfile...
@@ -67,7 +71,7 @@ By declaring parameter types, incoming parameters will automatically be transfor
67
71
 
68
72
  ### Validations
69
73
 
70
- Encapsulate business logic in a consistent way with validations. If a parameter does not satisfy a particular condition, an exception (RailsParam::Param::InvalidParameterError) is raised.
74
+ Encapsulate business logic in a consistent way with validations. If a parameter does not satisfy a particular condition, an exception (RailsParam::InvalidParameterError) is raised.
71
75
  You may use the [rescue_from](http://api.rubyonrails.org/classes/ActiveSupport/Rescuable/ClassMethods.html#method-i-rescue_from) method in your controller to catch this kind of exception.
72
76
 
73
77
  - `required`
@@ -75,6 +79,7 @@ You may use the [rescue_from](http://api.rubyonrails.org/classes/ActiveSupport/R
75
79
  - `is`
76
80
  - `in`, `within`, `range`
77
81
  - `min` / `max`
82
+ - `min_length` / `max_length`
78
83
  - `format`
79
84
 
80
85
  Customize exception message with option `:message`
@@ -0,0 +1,18 @@
1
+ module RailsParam
2
+ class Coercion
3
+ class ArrayParam < VirtualParam
4
+ def coerce
5
+ return param if param.is_a?(Array)
6
+
7
+ Array(param.split(options[:delimiter] || ","))
8
+ end
9
+
10
+ private
11
+
12
+ def argument_validation
13
+ raise ArgumentError unless type == Array
14
+ raise ArgumentError unless param.respond_to?(:split)
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,17 @@
1
+ module RailsParam
2
+ class Coercion
3
+ class BigDecimalParam < VirtualParam
4
+ DEFAULT_PRECISION = 14
5
+
6
+ def coerce
7
+ stripped_param = if param.is_a?(String)
8
+ param.delete('$,').strip.to_f
9
+ else
10
+ param
11
+ end
12
+
13
+ BigDecimal(stripped_param, options[:precision] || DEFAULT_PRECISION)
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,15 @@
1
+ module RailsParam
2
+ class Coercion
3
+ class BooleanParam < VirtualParam
4
+ FALSEY = /^(false|f|no|n|0)$/i.freeze
5
+ TRUTHY = /^(true|t|yes|y|1)$/i.freeze
6
+
7
+ def coerce
8
+ return false if FALSEY === param.to_s
9
+ return true if TRUTHY === param.to_s
10
+
11
+ raise ArgumentError
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,9 @@
1
+ module RailsParam
2
+ class Coercion
3
+ class FloatParam < VirtualParam
4
+ def coerce
5
+ Float(param)
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,18 @@
1
+ module RailsParam
2
+ class Coercion
3
+ class HashParam < VirtualParam
4
+ def coerce
5
+ return param if param.is_a?(ActionController::Parameters)
6
+ raise ArgumentError unless param.respond_to?(:split)
7
+
8
+ Hash[param.split(options[:delimiter] || ",").map { |c| c.split(options[:separator] || ":") }]
9
+ end
10
+
11
+ private
12
+
13
+ def argument_validation
14
+ raise ArgumentError unless type == Hash
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,9 @@
1
+ module RailsParam
2
+ class Coercion
3
+ class IntegerParam < VirtualParam
4
+ def coerce
5
+ Integer(param)
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ module RailsParam
2
+ class Coercion
3
+ class StringParam < VirtualParam
4
+ def coerce
5
+ String(param)
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,17 @@
1
+ module RailsParam
2
+ class Coercion
3
+ class TimeParam < VirtualParam
4
+ def coerce
5
+ return type.strptime(param, options[:format]) if options[:format].present?
6
+
7
+ type.parse(param)
8
+ end
9
+
10
+ private
11
+
12
+ def argument_validation
13
+ raise ArgumentError unless type.respond_to?(:parse)
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,24 @@
1
+ module RailsParam
2
+ class Coercion
3
+ class VirtualParam
4
+ attr_reader :param, :options, :type
5
+
6
+ def initialize(param:, options: nil, type: nil)
7
+ @param = param
8
+ @options = options
9
+ @type = type
10
+ argument_validation
11
+ end
12
+
13
+ def coerce
14
+ nil
15
+ end
16
+
17
+ private
18
+
19
+ def argument_validation
20
+ nil
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,41 @@
1
+ module RailsParam
2
+ class Coercion
3
+ attr_reader :coercion, :param
4
+
5
+ PARAM_TYPE_MAPPING = {
6
+ Integer => IntegerParam,
7
+ Float => FloatParam,
8
+ String => StringParam,
9
+ Array => ArrayParam,
10
+ Hash => HashParam,
11
+ BigDecimal => BigDecimalParam,
12
+ Date => TimeParam,
13
+ DateTime => TimeParam,
14
+ Time => TimeParam,
15
+ TrueClass => BooleanParam,
16
+ FalseClass => BooleanParam,
17
+ boolean: BooleanParam
18
+ }.freeze
19
+
20
+ TIME_TYPES = [Date, DateTime, Time].freeze
21
+ BOOLEAN_TYPES = [TrueClass, FalseClass, :boolean].freeze
22
+
23
+ def initialize(param, type, options)
24
+ @param = param
25
+ @coercion = klass_for(type).new(param: param, options: options, type: type)
26
+ end
27
+
28
+ def klass_for(type)
29
+ klass = PARAM_TYPE_MAPPING[type]
30
+ return klass if klass
31
+
32
+ raise TypeError
33
+ end
34
+
35
+ def coerce
36
+ return nil if param.nil?
37
+
38
+ coercion.coerce
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,17 @@
1
+ module RailsParam
2
+ class InvalidParameterError < StandardError
3
+ attr_accessor :param, :options
4
+
5
+ def initialize(message, param: nil, options: {})
6
+ self.param = param
7
+ self.options = options
8
+ super(message)
9
+ end
10
+
11
+ def message
12
+ return options[:message] if options.is_a?(Hash) && options.key?(:message)
13
+
14
+ super
15
+ end
16
+ end
17
+ end
@@ -1,173 +1,5 @@
1
1
  module RailsParam
2
- module Param
3
-
4
- DEFAULT_PRECISION = 14
5
- TIME_TYPES = [Date, DateTime, Time].freeze
6
- STRING_OR_TIME_TYPES = ([String] + TIME_TYPES).freeze
7
-
8
- class InvalidParameterError < StandardError
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
15
- end
16
-
17
- class MockController
18
- include RailsParam::Param
19
- attr_accessor :params
20
- end
21
-
22
- def param!(name, type, options = {}, &block)
23
- name = name.to_s unless name.is_a? Integer # keep index for validating elements
24
-
25
- return unless params.include?(name) || check_param_presence?(options[:default]) || options[:required]
26
-
27
- begin
28
- params[name] = coerce(params[name], type, options)
29
-
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]
35
- end
36
-
37
- # apply tranformation
38
- if params.include?(name) && options[:transform]
39
- params[name] = options[:transform].to_proc.call(params[name])
40
- end
41
-
42
- # validate
43
- validate!(params[name], name, options)
44
-
45
- if block_given?
46
- if type == Array
47
- params[name].each_with_index do |element, i|
48
- if element.is_a?(Hash) || element.is_a?(ActionController::Parameters)
49
- recurse element, &block
50
- else
51
- params[name][i] = recurse({ i => element }, i, &block) # supply index as key unless value is hash
52
- end
53
- end
54
- else
55
- recurse params[name], &block
56
- end
57
- end
58
- params[name]
59
-
60
- rescue InvalidParameterError => exception
61
- exception.param ||= name
62
- exception.options ||= options
63
- raise exception
64
- end
65
- end
66
-
67
- # TODO: should we reintegrate this method?
68
- # def one_of!(*names)
69
- # count = 0
70
- # names.each do |name|
71
- # if params[name] and params[name].present?
72
- # count += 1
73
- # next unless count > 1
74
- #
75
- # error = "Parameters #{names.join(', ')} are mutually exclusive"
76
- # if content_type and content_type.match(mime_type(:json))
77
- # error = {message: error}.to_json
78
- # end
79
- #
80
- # # do something with error object
81
- # end
82
- # end
83
- # end
84
-
85
- private
86
-
87
- def check_param_presence? param
88
- not param.nil?
89
- end
90
-
91
- def recurse(params, index = nil)
92
- raise InvalidParameterError, 'no block given' unless block_given?
93
- controller = RailsParam::Param::MockController.new
94
- controller.params = params
95
- yield(controller, index)
96
- end
97
-
98
- def coerce(param, type, options = {})
99
- begin
100
- return nil if param.nil?
101
- return param if (param.is_a?(type) rescue false)
102
- 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}"
104
- end
105
- return param if (param.is_a?(ActionController::Parameters) && type == Hash rescue false)
106
- return Integer(param) if type == Integer
107
- return Float(param) if type == Float
108
- return String(param) if type == String
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
116
- return Array(param.split(options[:delimiter] || ",")) if type == Array
117
- return Hash[param.split(options[:delimiter] || ",").map { |c| c.split(options[:separator] || ":") }] if type == Hash
118
- if type == TrueClass || type == FalseClass || type == :boolean
119
- return false if /^(false|f|no|n|0)$/i === param.to_s
120
- return true if /^(true|t|yes|y|1)$/i === param.to_s
121
-
122
- raise ArgumentError
123
- end
124
- if type == BigDecimal
125
- param = param.delete('$,').strip.to_f if param.is_a?(String)
126
- return BigDecimal.new(param, (options[:precision] || DEFAULT_PRECISION))
127
- end
128
- return nil
129
- rescue ArgumentError
130
- raise InvalidParameterError, "'#{param}' is not a valid #{type}"
131
- end
132
- end
133
-
134
- def validate!(param, param_name, options)
135
- options.each do |key, value|
136
- case key
137
- when :required
138
- raise InvalidParameterError, "Parameter #{param_name} is required" if value && param.nil?
139
- when :blank
140
- raise InvalidParameterError, "Parameter #{param_name} cannot be blank" if !value && case param
141
- when String
142
- !(/\S/ === param)
143
- when Array, Hash
144
- param.empty?
145
- else
146
- param.nil?
147
- end
148
- when :format
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
151
- when :is
152
- raise InvalidParameterError, "Parameter #{param_name} must be #{value}" unless param === value
153
- when :in, :within, :range
154
- raise InvalidParameterError, "Parameter #{param_name} must be within #{value}" unless param.nil? || case value
155
- when Range
156
- value.include?(param)
157
- else
158
- Array(value).include?(param)
159
- end
160
- when :min
161
- raise InvalidParameterError, "Parameter #{param_name} cannot be less than #{value}" unless param.nil? || value <= param
162
- when :max
163
- raise InvalidParameterError, "Parameter #{param_name} cannot be greater than #{value}" unless param.nil? || value >= param
164
- when :min_length
165
- raise InvalidParameterError, "Parameter #{param_name} cannot have length less than #{value}" unless param.nil? || value <= param.length
166
- when :max_length
167
- raise InvalidParameterError, "Parameter #{param_name} cannot have length greater than #{value}" unless param.nil? || value >= param.length
168
- end
169
- end
170
- end
171
-
2
+ def param!(name, type, options = {}, &block)
3
+ ParamEvaluator.new(params).param!(name, type, options, &block)
172
4
  end
173
5
  end
@@ -0,0 +1,90 @@
1
+ module RailsParam
2
+ class ParamEvaluator
3
+ attr_accessor :params
4
+
5
+ def initialize(params)
6
+ @params = params
7
+ end
8
+
9
+ def param!(name, type, options = {}, &block)
10
+ name = name.is_a?(Integer)? name : name.to_s
11
+ return unless params.include?(name) || check_param_presence?(options[:default]) || options[:required]
12
+
13
+ # coerce value
14
+ coerced_value = coerce(
15
+ params[name],
16
+ type,
17
+ options
18
+ )
19
+ parameter = RailsParam::Parameter.new(
20
+ name: name,
21
+ value: coerced_value,
22
+ type: type,
23
+ options: options,
24
+ &block
25
+ )
26
+
27
+ parameter.set_default if parameter.should_set_default?
28
+
29
+ # validate presence
30
+ if params[name].nil? && options[:required]
31
+ raise InvalidParameterError.new(
32
+ "Parameter #{name} is required",
33
+ param: name,
34
+ options: options
35
+ )
36
+ end
37
+
38
+ recurse_on_parameter(parameter, &block) if block_given?
39
+
40
+ # apply transformation
41
+ parameter.transform if params.include?(name) && options[:transform]
42
+
43
+ # validate
44
+ validate!(parameter)
45
+
46
+ # set params value
47
+ params[name] = parameter.value
48
+ end
49
+
50
+ private
51
+
52
+ def recurse_on_parameter(parameter, &block)
53
+ if parameter.type == Array
54
+ parameter.value.each_with_index do |element, i|
55
+ if element.is_a?(Hash) || element.is_a?(ActionController::Parameters)
56
+ recurse element, &block
57
+ else
58
+ parameter.value[i] = recurse({ i => element }, i, &block) # supply index as key unless value is hash
59
+ end
60
+ end
61
+ else
62
+ recurse parameter.value, &block
63
+ end
64
+ end
65
+
66
+ def recurse(element, index = nil)
67
+ raise InvalidParameterError, 'no block given' unless block_given?
68
+
69
+ yield(ParamEvaluator.new(element), index)
70
+ end
71
+
72
+ def check_param_presence? param
73
+ !param.nil?
74
+ end
75
+
76
+ def coerce(param, type, options = {})
77
+ begin
78
+ return param if (param.is_a?(type) rescue false)
79
+
80
+ Coercion.new(param, type, options).coerce
81
+ rescue ArgumentError, TypeError
82
+ raise InvalidParameterError, "'#{param}' is not a valid #{type}"
83
+ end
84
+ end
85
+
86
+ def validate!(param)
87
+ param.validate
88
+ end
89
+ end
90
+ end
@@ -0,0 +1,37 @@
1
+ module RailsParam
2
+ class Parameter
3
+ attr_accessor :name, :value, :options, :type
4
+
5
+ TIME_TYPES = [Date, DateTime, Time].freeze
6
+ STRING_OR_TIME_TYPES = ([String] + TIME_TYPES).freeze
7
+
8
+ def initialize(name:, value:, options: {}, type: nil)
9
+ @name = name
10
+ @value = value
11
+ @options = options
12
+ @type = type
13
+ end
14
+
15
+ def should_set_default?
16
+ value.nil? && check_param_presence?(options[:default])
17
+ end
18
+
19
+ def set_default
20
+ self.value = options[:default].respond_to?(:call) ? options[:default].call : options[:default]
21
+ end
22
+
23
+ def transform
24
+ self.value = options[:transform].to_proc.call(value)
25
+ end
26
+
27
+ def validate
28
+ Validator.new(self).validate!
29
+ end
30
+
31
+ private
32
+
33
+ def check_param_presence?(param)
34
+ !param.nil?
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,24 @@
1
+ module RailsParam
2
+ class Validator
3
+ class Blank < Validator
4
+ def valid_value?
5
+ return false if parameter.options[:blank]
6
+
7
+ case value
8
+ when String
9
+ (/\S/ === value)
10
+ when Array, Hash, ActionController::Parameters
11
+ !value.empty?
12
+ else
13
+ !value.nil?
14
+ end
15
+ end
16
+
17
+ private
18
+
19
+ def error_message
20
+ "Parameter #{name} cannot be blank"
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,13 @@
1
+ module RailsParam
2
+ class Validator
3
+ class Custom < Validator
4
+ def valid_value?
5
+ !options[:custom].call(value)
6
+ end
7
+
8
+ private
9
+
10
+ def error_message; end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,31 @@
1
+ module RailsParam
2
+ class Validator
3
+ class Format < Validator
4
+ def valid_value?
5
+ matches_time_types? || string_in_format?
6
+ end
7
+
8
+ private
9
+
10
+ TIME_TYPES = [Date, DateTime, Time].freeze
11
+ STRING_OR_TIME_TYPES = ([String] + TIME_TYPES).freeze
12
+
13
+ def error_message
14
+ "Parameter #{name} must be a string if using the format validation" unless matches_string_or_time_types?
15
+ "Parameter #{name} must match format #{options[:format]}" unless string_in_format?
16
+ end
17
+
18
+ def matches_time_types?
19
+ TIME_TYPES.any? { |cls| value.is_a? cls }
20
+ end
21
+
22
+ def matches_string_or_time_types?
23
+ STRING_OR_TIME_TYPES.any? { |cls| value.is_a? cls }
24
+ end
25
+
26
+ def string_in_format?
27
+ value =~ options[:format] && value.kind_of?(String)
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,20 @@
1
+ module RailsParam
2
+ class Validator
3
+ class In < Validator
4
+ def valid_value?
5
+ value.nil? || case options[:in]
6
+ when Range
7
+ options[:in].include?(value)
8
+ else
9
+ Array(options[:in]).include?(value)
10
+ end
11
+ end
12
+
13
+ private
14
+
15
+ def error_message
16
+ "Parameter #{parameter.name} must be within #{parameter.options[:in]}"
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,15 @@
1
+ module RailsParam
2
+ class Validator
3
+ class Is < Validator
4
+ def valid_value?
5
+ value === options[:is]
6
+ end
7
+
8
+ private
9
+
10
+ def error_message
11
+ "Parameter #{name} must be #{options[:is]}"
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,15 @@
1
+ module RailsParam
2
+ class Validator
3
+ class Max < Validator
4
+ def valid_value?
5
+ value.nil? || options[:max] >= value
6
+ end
7
+
8
+ private
9
+
10
+ def error_message
11
+ "Parameter #{name} cannot be greater than #{options[:max]}"
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,15 @@
1
+ module RailsParam
2
+ class Validator
3
+ class MaxLength < Validator
4
+ def valid_value?
5
+ value.nil? || options[:max_length] >= value.length
6
+ end
7
+
8
+ private
9
+
10
+ def error_message
11
+ "Parameter #{name} cannot have length greater than #{options[:max_length]}"
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,15 @@
1
+ module RailsParam
2
+ class Validator
3
+ class Min < Validator
4
+ def valid_value?
5
+ value.nil? || options[:min] <= value
6
+ end
7
+
8
+ private
9
+
10
+ def error_message
11
+ "Parameter #{name} cannot be less than #{options[:min]}"
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,15 @@
1
+ module RailsParam
2
+ class Validator
3
+ class MinLength < Validator
4
+ def valid_value?
5
+ value.nil? || options[:min_length] <= value.length
6
+ end
7
+
8
+ private
9
+
10
+ def error_message
11
+ "Parameter #{name} cannot have length less than #{options[:min_length]}"
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,15 @@
1
+ module RailsParam
2
+ class Validator
3
+ class Required < Validator
4
+ private
5
+
6
+ def valid_value?
7
+ value.present? && options[:required]
8
+ end
9
+
10
+ def error_message
11
+ "Parameter #{name} is required"
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,63 @@
1
+ require 'forwardable'
2
+
3
+ module RailsParam
4
+ class Validator
5
+ extend Forwardable
6
+
7
+ attr_reader :parameter
8
+
9
+ def_delegators :parameter, :name, :options, :value
10
+
11
+ VALIDATABLE_OPTIONS = [
12
+ :blank,
13
+ :custom,
14
+ :format,
15
+ :in,
16
+ :is,
17
+ :max_length,
18
+ :max,
19
+ :min_length,
20
+ :min,
21
+ :required
22
+ ].freeze
23
+
24
+ def initialize(parameter)
25
+ @parameter = parameter
26
+ end
27
+
28
+ def validate!
29
+ options.each_key do |key|
30
+ next unless VALIDATABLE_OPTIONS.include? key
31
+
32
+ class_name = camelize(key)
33
+ Validator.const_get(class_name).new(parameter).valid!
34
+ end
35
+ end
36
+
37
+ def valid!
38
+ return if valid_value?
39
+
40
+ raise InvalidParameterError.new(
41
+ error_message,
42
+ param: name,
43
+ options: options
44
+ )
45
+ end
46
+
47
+ private
48
+
49
+ def camelize(term)
50
+ string = term.to_s
51
+ string.split('_').collect(&:capitalize).join
52
+ end
53
+
54
+ def error_message
55
+ nil
56
+ end
57
+
58
+ def valid_value?
59
+ # Should be overwritten in subclass
60
+ false
61
+ end
62
+ end
63
+ end
@@ -1,3 +1,3 @@
1
1
  module RailsParam #:nodoc
2
- VERSION = "0.10.2"
2
+ VERSION = "1.0.0"
3
3
  end
data/lib/rails_param.rb CHANGED
@@ -1,5 +1,8 @@
1
1
  require 'rails_param/param'
2
+ Dir[File.join(__dir__, 'rails_param/validator', '*.rb')].sort.each { |file| require file }
3
+ Dir[File.join(__dir__, 'rails_param/coercion', '*.rb')].sort.reverse.each { |file| require file }
4
+ Dir[File.join(__dir__, 'rails_param', '*.rb')].sort.each { |file| require file }
2
5
 
3
6
  ActiveSupport.on_load(:action_controller) do
4
- include RailsParam::Param
7
+ include RailsParam
5
8
  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: 1.0.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: 2021-09-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -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: []
@@ -74,7 +88,31 @@ extra_rdoc_files: []
74
88
  files:
75
89
  - README.md
76
90
  - lib/rails_param.rb
91
+ - lib/rails_param/coercion.rb
92
+ - lib/rails_param/coercion/array_param.rb
93
+ - lib/rails_param/coercion/big_decimal_param.rb
94
+ - lib/rails_param/coercion/boolean_param.rb
95
+ - lib/rails_param/coercion/float_param.rb
96
+ - lib/rails_param/coercion/hash_param.rb
97
+ - lib/rails_param/coercion/integer_param.rb
98
+ - lib/rails_param/coercion/string_param.rb
99
+ - lib/rails_param/coercion/time_param.rb
100
+ - lib/rails_param/coercion/virtual_param.rb
101
+ - lib/rails_param/invalid_parameter_error.rb
77
102
  - lib/rails_param/param.rb
103
+ - lib/rails_param/param_evaluator.rb
104
+ - lib/rails_param/parameter.rb
105
+ - lib/rails_param/validator.rb
106
+ - lib/rails_param/validator/blank.rb
107
+ - lib/rails_param/validator/custom.rb
108
+ - lib/rails_param/validator/format.rb
109
+ - lib/rails_param/validator/in.rb
110
+ - lib/rails_param/validator/is.rb
111
+ - lib/rails_param/validator/max.rb
112
+ - lib/rails_param/validator/max_length.rb
113
+ - lib/rails_param/validator/min.rb
114
+ - lib/rails_param/validator/min_length.rb
115
+ - lib/rails_param/validator/required.rb
78
116
  - lib/rails_param/version.rb
79
117
  homepage: http://github.com/nicolasblanco/rails_param
80
118
  licenses:
@@ -96,8 +134,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
96
134
  - !ruby/object:Gem::Version
97
135
  version: 1.3.6
98
136
  requirements: []
99
- rubyforge_project:
100
- rubygems_version: 2.7.8
137
+ rubygems_version: 3.1.6
101
138
  signing_key:
102
139
  specification_version: 4
103
140
  summary: Parameter Validation and Type Coercion for Rails