Class: Necromancer::NumericConverters::StringToIntegerConverter

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

Overview

An object that converts a String to an Integer

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 value to integer

Examples:

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


21
22
23
24
25
26
# File 'lib/necromancer/converters/numeric.rb', line 21

def call(value, options = {})
  strict = options.fetch(:strict, config.strict)
  Integer(value)
rescue
  strict ? fail_conversion_type(value) : value.to_i
end