Module: ParamChecker
- Defined in:
- lib/param_checker.rb,
lib/param_checker/version.rb,
lib/param_checker/hash_ext.rb
Defined Under Namespace
Modules: HashExt
Constant Summary collapse
- VERSION =
"0.3.0"
Class Method Summary collapse
-
.check_boolean(param, default, options = {}) ⇒ Object
Check a parameter string if it represents a valid boolean and return its boolean value.
-
.check_float(param, default, options = {}) ⇒ Object
Check a parameter string if it is a valid float and return its float value.
-
.check_integer(param, default, options = {}) ⇒ Object
Check a parameter string if it is a valid integer and return its integer value.
-
.check_string(param, default, options = {}) ⇒ Object
Check a parameter string if it is a valid sting and return its string value.
-
.check_symbol(param, default, options = {}) ⇒ Object
Check a parameter string if it is a valid symbol and return its symbol value.
Instance Method Summary collapse
-
#check_boolean(param, default, options = {}) ⇒ Object
Check a parameter string if it represents a valid boolean and return its boolean value.
-
#check_float(param, default, options = {}) ⇒ Object
Check a parameter string if it is a valid float and return its float value.
-
#check_integer(param, default, options = {}) ⇒ Object
Check a parameter string if it is a valid integer and return its integer value.
-
#check_string(param, default, options = {}) ⇒ Object
Check a parameter string if it is a valid sting and return its string value.
-
#check_symbol(param, default, options = {}) ⇒ Object
Check a parameter string if it is a valid symbol and return its symbol value.
Class Method Details
.check_boolean(param, default, options = {}) ⇒ Object
Check a parameter string if it represents a valid boolean and return its boolean value.
-
param
: the string parameter to check -
default
: the default boolean to return if the check fails -
options
: a hash of options:-
true
: an array of string representations of true (by default “1” and “true”) -
false
: an array of string representations of false (by default “0” and “false”)
-
81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/param_checker.rb', line 81 def check_boolean(param, default, = {}) true_values = ([:true] ? [:true] : ["1", "true"]) false_values = ([:false] ? [:false] : ["0", "false"]) if (true_values.include?(param)) true elsif (false_values.include?(param)) false else default end end |
.check_float(param, default, options = {}) ⇒ Object
Check a parameter string if it is a valid float and return its float value.
-
param
: the string parameter to check -
default
: the default float to return if the check fails -
options
: a hash of options:-
min
: the minimum float value allowed (optional) -
max
: the maximum float value allowed (optional)
-
28 29 30 31 32 33 34 35 36 37 |
# File 'lib/param_checker.rb', line 28 def check_float(param, default, = {}) min = ([:min] ? param.to_f >= [:min] : true) max = ([:max] ? param.to_f <= [:max] : true) if (param && param.strip =~ /^-?[0-9]+(\.[0-9]+)?$/ && min && max) param.to_f else default end end |
.check_integer(param, default, options = {}) ⇒ Object
Check a parameter string if it is a valid integer and return its integer value.
-
param
: the string parameter to check -
default
: the default integer to return if the check fails -
options
: a hash of options:-
min
: the minimum integer value allowed -
max
: the maximum integer value allowed
-
11 12 13 14 15 16 17 18 19 20 |
# File 'lib/param_checker.rb', line 11 def check_integer(param, default, = {}) min = ([:min] ? param.to_i >= [:min] : true) max = ([:max] ? param.to_i <= [:max] : true) if (param && param.strip =~ /^-?[0-9]+$/ && min && max) param.to_i else default end end |
.check_string(param, default, options = {}) ⇒ Object
Check a parameter string if it is a valid sting and return its string value.
-
param
: the string parameter to check -
default
: the default string to return if the check fails -
options
: a hash of options:-
allowed
: the allowed string values to checkparam
against; could be a regular expression, a string or an array of strings
-
44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/param_checker.rb', line 44 def check_string(param, default, = {}) allowed = [: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) param else default end end |
.check_symbol(param, default, options = {}) ⇒ Object
Check a parameter string if it is a valid symbol and return its symbol value.
-
param
: the string parameter to check -
default
: the default symbol to return if the check fails -
options
: a hash of options:-
allowed
: the allowed symbol values to checkparam
against; could be a regular expression, a symbol or an array of symbols.
-
62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/param_checker.rb', line 62 def check_symbol(param, default, = {}) allowed = [:allowed] if (param && !param.empty? && allowed.class == Regexp && param =~ allowed) param.to_sym elsif (param && !param.empty? && allowed.class == Array && allowed.map { |a| a.to_sym }.include?(param.to_sym)) param.to_sym elsif (param && !param.empty? && (allowed.class == String || allowed.class == Symbol) && allowed.to_sym == param.to_sym) param.to_sym else default end end |
Instance Method Details
#check_boolean(param, default, options = {}) ⇒ Object
Check a parameter string if it represents a valid boolean and return its boolean value.
-
param
: the string parameter to check -
default
: the default boolean to return if the check fails -
options
: a hash of options:-
true
: an array of string representations of true (by default “1” and “true”) -
false
: an array of string representations of false (by default “0” and “false”)
-
81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/param_checker.rb', line 81 def check_boolean(param, default, = {}) true_values = ([:true] ? [:true] : ["1", "true"]) false_values = ([:false] ? [:false] : ["0", "false"]) if (true_values.include?(param)) true elsif (false_values.include?(param)) false else default end end |
#check_float(param, default, options = {}) ⇒ Object
Check a parameter string if it is a valid float and return its float value.
-
param
: the string parameter to check -
default
: the default float to return if the check fails -
options
: a hash of options:-
min
: the minimum float value allowed (optional) -
max
: the maximum float value allowed (optional)
-
28 29 30 31 32 33 34 35 36 37 |
# File 'lib/param_checker.rb', line 28 def check_float(param, default, = {}) min = ([:min] ? param.to_f >= [:min] : true) max = ([:max] ? param.to_f <= [:max] : true) if (param && param.strip =~ /^-?[0-9]+(\.[0-9]+)?$/ && min && max) param.to_f else default end end |
#check_integer(param, default, options = {}) ⇒ Object
Check a parameter string if it is a valid integer and return its integer value.
-
param
: the string parameter to check -
default
: the default integer to return if the check fails -
options
: a hash of options:-
min
: the minimum integer value allowed -
max
: the maximum integer value allowed
-
11 12 13 14 15 16 17 18 19 20 |
# File 'lib/param_checker.rb', line 11 def check_integer(param, default, = {}) min = ([:min] ? param.to_i >= [:min] : true) max = ([:max] ? param.to_i <= [:max] : true) if (param && param.strip =~ /^-?[0-9]+$/ && min && max) param.to_i else default end end |
#check_string(param, default, options = {}) ⇒ Object
Check a parameter string if it is a valid sting and return its string value.
-
param
: the string parameter to check -
default
: the default string to return if the check fails -
options
: a hash of options:-
allowed
: the allowed string values to checkparam
against; could be a regular expression, a string or an array of strings
-
44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/param_checker.rb', line 44 def check_string(param, default, = {}) allowed = [: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) param else default end end |
#check_symbol(param, default, options = {}) ⇒ Object
Check a parameter string if it is a valid symbol and return its symbol value.
-
param
: the string parameter to check -
default
: the default symbol to return if the check fails -
options
: a hash of options:-
allowed
: the allowed symbol values to checkparam
against; could be a regular expression, a symbol or an array of symbols.
-
62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/param_checker.rb', line 62 def check_symbol(param, default, = {}) allowed = [:allowed] if (param && !param.empty? && allowed.class == Regexp && param =~ allowed) param.to_sym elsif (param && !param.empty? && allowed.class == Array && allowed.map { |a| a.to_sym }.include?(param.to_sym)) param.to_sym elsif (param && !param.empty? && (allowed.class == String || allowed.class == Symbol) && allowed.to_sym == param.to_sym) param.to_sym else default end end |