lib/param_checker.rb in param_checker-0.0.1 vs lib/param_checker.rb in param_checker-0.0.2
- old
+ new
@@ -1,29 +1,32 @@
module ParamChecker
- def self.check_integer_param(param, default, min = nil, max = nil)
+
+ module_function
+
+ def check_integer_param(param, default, min = nil, max = nil)
min_lambda = (min.nil? ? lambda { true } : lambda { param.strip.to_i >= min })
max_lambda = (max.nil? ? lambda { true } : lambda { param.strip.to_i <= max })
if (param && param.strip =~ /^-?[0-9]+$/ && min_lambda.call && max_lambda.call)
param.strip.to_i
else
default
end
end
- def self.check_float_param(param, default, min = nil, max = nil)
+ def check_float_param(param, default, min = nil, max = nil)
min_lambda = (min.nil? ? lambda { true } : lambda { param.strip.to_i >= min })
max_lambda = (max.nil? ? lambda { true } : lambda { param.strip.to_i <= max })
if (param && param.strip =~ /^-?[0-9]+(\.[0-9]+)?$/ && min_lambda.call && max_lambda.call)
param.strip.to_f
else
default.to_f
end
end
- def self.check_string_param(param, default, allowed)
+ def check_string_param(param, default, allowed)
if (param && allowed.class == Regexp && param =~ allowed)
param
elsif (param && allowed.class == Array && allowed.include?(param))
param
elsif (param && allowed.class == String && allowed == param)
@@ -31,11 +34,11 @@
else
default
end
end
- def self.check_symbol_param(param, default, allowed)
+ def check_symbol_param(param, default, allowed)
begin
if (param && allowed.class == Regexp && param.to_s =~ allowed)
param.to_sym
elsif (param && allowed.class == Array && allowed.map { |i| i.to_sym }.include?(param.to_sym))
param.to_sym
@@ -47,15 +50,21 @@
rescue
default.to_sym
end
end
- def self.check_boolean_param(param, default)
+ def check_boolean_param(param, default)
if (param && param == "1" || param == "true")
true
elsif (param && param == "0" || param == "false")
false
else
default
end
end
+
+ public :check_integer_param,
+ :check_float_param,
+ :check_string_param,
+ :check_symbol_param,
+ :check_boolean_param
end