Class: HasSetting::Formatters::IntFormatter

Inherits:
NilSafeFormatter show all
Defined in:
lib/has_setting/formatters.rb

Overview

Formatter for ints Throws ArgumentError if value can not be converted

Instance Method Summary collapse

Methods inherited from NilSafeFormatter

#to_s, #to_type

Instance Method Details

#safe_to_s(value) ⇒ Object



125
126
127
# File 'lib/has_setting/formatters.rb', line 125

def safe_to_s(value)
  Integer(Float(value)).to_s
end

#safe_to_type(value) ⇒ Object

Integer() does not treat “2.6” the same as 2.6 while 2.6 is a valid Intger() (-> 2), “2.6” is not. Note that “2” is a valid argument for Integer() and that “”.to_i is valid while Integer(”) is not… Circumvent this by first convert with Float() so everything obeys to the same rules



122
123
124
# File 'lib/has_setting/formatters.rb', line 122

def safe_to_type(value)
  Integer(Float(value))
end