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

Class Method Details

.coerce(value) ⇒ nil, ...

Converts the value to a predicted type based on the value format.

Parameters:

  • value (Object)

    the value to convert.

Returns:

  • (nil, true, false, Integer, String)

    the coerced value.



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

Converts the value to true or false.

Parameters:

  • value (Object)

    the value to convert.

Returns:

  • (true, false)

    the coerced value.

Raises:

  • (CoercionError)

    if the value cannot be coerced to either true or 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.

Returns:

  • (true, false)

    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

Converts the value to an Integer.

Parameters:

  • value (Object)

    the value to convert.

Returns:

  • (Integer)

    the coerced value.

Raises:

  • (CoercionError)

    if the value cannot be coerced to an 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.

Returns:

  • (true, false)

    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

Converts the value to nil.

Parameters:

  • value (Object)

    the value to convert.

Returns:

  • (nil)

    the coerced value.

Raises:



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.

Returns:

  • (true, false)

    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