Module: Cuprum::Cli::Coercion
- Defined in:
- lib/cuprum/cli/coercion.rb
Overview
Utility for coercing raw string values to other literal types.
Defined Under Namespace
Classes: CoercionError
Class Method Summary collapse
-
.coerce(value) ⇒ nil, ...
Converts the value to a predicted type based on the value format.
-
.coerce_boolean(value) ⇒ true, false
Converts the value to true or false.
-
.coerce_boolean?(value) ⇒ true, false
True if the value can be safely coerced to either true or false, otherwise false.
-
.coerce_integer(value) ⇒ Integer
Converts the value to an Integer.
-
.coerce_integer?(value) ⇒ true, false
True if the value can be safely coerced to an Integer, otherwise false.
-
.coerce_nil(value) ⇒ nil
Converts the value to nil.
-
.coerce_nil?(value) ⇒ true, false
True if the value can be safely coerced to nil, otherwise false.
Class Method Details
.coerce(value) ⇒ nil, ...
Converts the value to a predicted type based on the value format.
28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/cuprum/cli/coercion.rb', line 28 def self.coerce(value) skip_validation = true return coerce_nil(value, skip_validation:) if coerce_nil?(value) return coerce_boolean(value, skip_validation:) if coerce_boolean?(value) return coerce_integer(value, skip_validation:) if coerce_integer?(value) return value if value.is_a?(String) value.inspect end |
.coerce_boolean(value) ⇒ true, false
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/cuprum/cli/coercion.rb', line 49 def self.coerce_boolean(value, skip_validation: false) return false if value.nil? if !skip_validation && !coerce_boolean?(value) raise CoercionError, "unable to coerce #{value.inspect} to true or false" end value = value.downcase return false if FALSY_VALUES.include?(value) return true if TRUTHY_VALUES.include?(value) nil end |
.coerce_boolean?(value) ⇒ true, false
Returns true if the value can be safely coerced to either true or false, otherwise false.
67 68 69 70 71 72 73 74 |
# File 'lib/cuprum/cli/coercion.rb', line 67 def self.coerce_boolean?(value) return true if value.nil? return false unless value.is_a?(String) value = value.downcase FALSY_VALUES.include?(value) || TRUTHY_VALUES.include?(value) end |
.coerce_integer(value) ⇒ Integer
84 85 86 87 88 89 90 91 |
# File 'lib/cuprum/cli/coercion.rb', line 84 def self.coerce_integer(value, skip_validation: false) if !skip_validation && !coerce_integer?(value) raise CoercionError, "unable to coerce #{value.inspect} to an Integer" end value.tr('_,', '').to_i end |
.coerce_integer?(value) ⇒ true, false
Returns true if the value can be safely coerced to an Integer, otherwise false.
95 96 97 98 99 100 101 |
# File 'lib/cuprum/cli/coercion.rb', line 95 def self.coerce_integer?(value) return false unless value.is_a?(String) value = value.downcase INTEGER_PATTERN.match?(value) end |
.coerce_nil(value) ⇒ nil
111 112 113 114 115 116 117 118 119 120 |
# File 'lib/cuprum/cli/coercion.rb', line 111 def self.coerce_nil(value, skip_validation: false) return nil if value.nil? if !skip_validation && !coerce_nil?(value) raise CoercionError, "unable to coerce #{value.inspect} to nil" end nil end |
.coerce_nil?(value) ⇒ true, false
Returns true if the value can be safely coerced to nil, otherwise false.
124 125 126 127 128 129 |
# File 'lib/cuprum/cli/coercion.rb', line 124 def self.coerce_nil?(value) return true if value.nil? return false unless value.is_a?(String) NULLISH_VALUES.include?(value.downcase) end |