Class: TTY::Conversion::IntegerConverter

Inherits:
Object
  • Object
show all
Defined in:
lib/tty/conversion/converter/integer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source = String) ⇒ IntegerConverter

Returns a new instance of IntegerConverter.



11
12
13
14
# File 'lib/tty/conversion/converter/integer.rb', line 11

def initialize(source = String)
  @source = source
  @target = Integer
end

Instance Attribute Details

#sourceObject (readonly)

Returns the value of attribute source.



9
10
11
# File 'lib/tty/conversion/converter/integer.rb', line 9

def source
  @source
end

#targetObject (readonly)

Returns the value of attribute target.



7
8
9
# File 'lib/tty/conversion/converter/integer.rb', line 7

def target
  @target
end

Instance Method Details

#convert(value, strict = false) ⇒ Object



17
18
19
20
21
22
23
24
25
# File 'lib/tty/conversion/converter/integer.rb', line 17

def convert(value, strict = false)
  Kernel.send(target.name.to_sym, value.to_s)
rescue
  if strict
    raise InvalidArgument, "#{value} could not be coerced into #{target.name}"
  else
    value.to_i
  end
end