Class: Necromancer::NumericConverters::StringToNumericConverter

Inherits:
Converter
  • Object
show all
Defined in:
lib/necromancer/converters/numeric.rb

Overview

An object that converts a String to a Numeric

Instance Attribute Summary

Attributes inherited from Converter

#convert, #source, #target

Instance Method Summary collapse

Methods inherited from Converter

create, #fail_conversion_type, #initialize

Constructor Details

This class inherits a constructor from Necromancer::Converter

Instance Method Details

#call(value, options = {}) ⇒ Object

Convert string to numeric value

Examples:

converter.call('1.0')  # => 1.0
converter.call('1')   # => 1


69
70
71
72
73
74
75
76
77
78
79
# File 'lib/necromancer/converters/numeric.rb', line 69

def call(value, options = {})
  strict = options.fetch(:strict, config.strict)
  case value
  when INTEGER_MATCHER
    StringToIntegerConverter.new(:string, :integer).call(value, options)
  when FLOAT_MATCHER
    StringToFloatConverter.new(:string, :float).call(value, options)
  else
    strict ? fail_conversion_type(value) : value
  end
end