Module: Xqsr3::Conversion::BoolParser
- Defined in:
- lib/xqsr3/conversion/bool_parser.rb
Constant Summary collapse
- DEFAULT_TRUE_VALUES =
[ /true/i, '1' ]
- DEFAULT_FALSE_VALUES =
[ /false/i, '0' ]
Class Method Summary collapse
-
.to_bool(s, **options) ⇒ Object
Attempts to parse the given String to a Boolean value, based on the given
options
.
Class Method Details
.to_bool(s, **options) ⇒ Object
Attempts to parse the given String to a Boolean value, based on the given options
Signature
-
Parameters:
options
-
An options hash, containing any of the following options
-
Options:
:false_values
- ::Array
-
An array of strings or regular
expressions against which to match for false value. Defaults to
DEFAULT_FALSE_VALUES
:true_values
- ::Array
-
An array of strings or regular
expressions against which to match for true value. Defaults to
DEFAULT_TRUE_VALUES
:default_value
-
An object to be returned if matching fails.
Defaults to
nil
:false_value
-
An object to be returned if matching succeeds to
match against
:false_values
.:true_value
-
An object to be returned if matching succeeds to
match against
:true_values
.
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/xqsr3/conversion/bool_parser.rb', line 96 def self.to_bool s, ** true_values = [:true_values] || DEFAULT_TRUE_VALUES true_values = [ true_values ] unless true_values.is_a? ::Array false_values = [:false_values] || DEFAULT_FALSE_VALUES false_values = [ false_values ] unless false_values.is_a? ::Array default_value = [:default] || nil true_value = [:true] || true false_value = [:false] || false return true_value if true_values.any? { |v| self.matches_to_ s, v } return false_value if false_values.any? { |v| self.matches_to_ s, v } return default_value end |