Commit 11078e87 by Nicolas Blanco

Merge pull request #17 from piya23300/master

fix setting default value and throwing errors case param type is boolean
parents 36e10375 528e7f04
...@@ -14,12 +14,12 @@ module RailsParam ...@@ -14,12 +14,12 @@ module RailsParam
def param!(name, type, options = {}, &block) def param!(name, type, options = {}, &block)
name = name.to_s unless name.is_a? Integer # keep index for validating elements name = name.to_s unless name.is_a? Integer # keep index for validating elements
return unless params.member?(name) || options[:default].present? || options[:required] return unless params.member?(name) || check_param_presence?(options[:default]) || options[:required]
begin begin
params[name] = coerce(params[name], type, options) params[name] = coerce(params[name], type, options)
params[name] = (options[:default].call if options[:default].respond_to?(:call)) || options[:default] if params[name].nil? and options[:default] params[name] = (options[:default].call if options[:default].respond_to?(:call)) || options[:default] if params[name].nil? and check_param_presence?(options[:default])
params[name] = options[:transform].to_proc.call(params[name]) if params[name] and options[:transform] params[name] = options[:transform].to_proc.call(params[name]) if params[name] and options[:transform]
validate!(params[name], options) validate!(params[name], options)
...@@ -65,6 +65,10 @@ module RailsParam ...@@ -65,6 +65,10 @@ module RailsParam
private private
def check_param_presence? param
not param.nil?
end
def recurse(params, index = nil) def recurse(params, index = nil)
raise InvalidParameterError, 'no block given' unless block_given? raise InvalidParameterError, 'no block given' unless block_given?
controller = RailsParam::Param::MockController.new controller = RailsParam::Param::MockController.new
...@@ -84,7 +88,7 @@ module RailsParam ...@@ -84,7 +88,7 @@ module RailsParam
return DateTime.parse(param) if type == DateTime return DateTime.parse(param) if type == DateTime
return Array(param.split(options[:delimiter] || ",")) if type == Array return Array(param.split(options[:delimiter] || ",")) if type == Array
return Hash[param.split(options[:delimiter] || ",").map { |c| c.split(options[:separator] || ":") }] if type == Hash return Hash[param.split(options[:delimiter] || ",").map { |c| c.split(options[:separator] || ":") }] if type == Hash
return (/(false|f|no|n|0)$/i === param.to_s ? false : (/(true|t|yes|y|1)$/i === param.to_s ? true : nil)) if type == TrueClass || type == FalseClass || type == :boolean 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
if type == BigDecimal if type == BigDecimal
param = param.delete('$,').strip.to_f if param.is_a?(String) param = param.delete('$,').strip.to_f if param.is_a?(String)
return BigDecimal.new(param, (options[:precision] || DEFAULT_PRECISION)) return BigDecimal.new(param, (options[:precision] || DEFAULT_PRECISION))
......
...@@ -172,12 +172,23 @@ describe RailsParam::Param do ...@@ -172,12 +172,23 @@ describe RailsParam::Param do
controller.param! :foo, TrueClass controller.param! :foo, TrueClass
expect(controller.params["foo"]).to eql(false) expect(controller.params["foo"]).to eql(false)
end end
it "return InvalidParameterError if value not boolean" do
allow(controller).to receive(:params).and_return({"foo" => "1111"})
expect { controller.param! :foo, :boolean }.to raise_error(RailsParam::Param::InvalidParameterError)
end
it "set default boolean" do
allow(controller).to receive(:params).and_return({})
controller.param! :foo, :boolean, default: false
expect(controller.params["foo"]).to eql(false)
end
end end
it "raises InvalidParameterError if the value is invalid" do it "raises InvalidParameterError if the value is invalid" do
allow(controller).to receive(:params).and_return({"foo" => "1984-01-32"}) allow(controller).to receive(:params).and_return({"foo" => "1984-01-32"})
expect { controller.param! :foo, Date }.to raise_error(RailsParam::Param::InvalidParameterError) expect { controller.param! :foo, Date }.to raise_error(RailsParam::Param::InvalidParameterError)
end end
end end
describe 'validating nested hash' do describe 'validating nested hash' do
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment