Class: SafeYAML::Transform::ToInteger

Inherits:
Object
  • Object
show all
Defined in:
lib/safe_yaml/transform/to_integer.rb

Constant Summary collapse

MATCHERS =
Deep.freeze([
  /\A[-+]?(0|([1-9][0-9_,]*))\Z/, # decimal
  /\A0[0-7]+\Z/,                  # octal
  /\A0x[0-9a-f]+\Z/i,             # hexadecimal
  /\A0b[01_]+\Z/                  # binary
])

Instance Method Summary collapse

Instance Method Details

#transform?(value) ⇒ Boolean

Returns:

  • (Boolean)


11
12
13
14
15
16
17
# File 'lib/safe_yaml/transform/to_integer.rb', line 11

def transform?(value)
  MATCHERS.each_with_index do |matcher, idx|
    value = value.gsub(/[_,]/, "") if idx == 0
    return true, Integer(value) if matcher.match(value)
  end
  try_edge_cases?(value)
end

#try_edge_cases?(value) ⇒ Boolean

Returns:

  • (Boolean)


19
20
21
22
23
# File 'lib/safe_yaml/transform/to_integer.rb', line 19

def try_edge_cases?(value)
  return true, Parse::Hexadecimal.value(value) if Parse::Hexadecimal::MATCHER.match(value)
  return true, Parse::Sexagesimal.value(value) if Parse::Sexagesimal::INTEGER_MATCHER.match(value)
  return false
end