Commit 8f3c9b43 by PiYa

fixed setting default and throwing errors case boolean

parent 36e10375
......@@ -15,11 +15,12 @@ module RailsParam
def param!(name, type, options = {}, &block)
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) || (not options[:default].nil?) || options[:required]
begin
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 (not options[:default].nil?)
params[name] = options[:transform].to_proc.call(params[name]) if params[name] and options[:transform]
validate!(params[name], options)
......@@ -84,7 +85,7 @@ module RailsParam
return DateTime.parse(param) if type == DateTime
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 (/(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
param = param.delete('$,').strip.to_f if param.is_a?(String)
return BigDecimal.new(param, (options[:precision] || DEFAULT_PRECISION))
......
......@@ -172,12 +172,23 @@ describe RailsParam::Param do
controller.param! :foo, TrueClass
expect(controller.params["foo"]).to eql(false)
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
it "raises InvalidParameterError if the value is invalid" do
allow(controller).to receive(:params).and_return({"foo" => "1984-01-32"})
expect { controller.param! :foo, Date }.to raise_error(RailsParam::Param::InvalidParameterError)
end
end
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