Class: IknowParams::Serializer::Integer

Inherits:
IknowParams::Serializer show all
Defined in:
lib/iknow_params/serializer.rb

Instance Attribute Summary

Attributes inherited from IknowParams::Serializer

#clazz

Instance Method Summary collapse

Methods inherited from IknowParams::Serializer

#dump, for, for!, json_value?, #matches_type!, #matches_type?, singleton

Constructor Details

#initializeInteger

Returns a new instance of Integer.



105
106
107
# File 'lib/iknow_params/serializer.rb', line 105

def initialize
  super(::Integer)
end

Instance Method Details

#load(str_or_num) ⇒ Object

JSON only supports floats, so we have to accept a value which may have already been parsed into a Ruby Float or Integer.



111
112
113
114
115
116
# File 'lib/iknow_params/serializer.rb', line 111

def load(str_or_num)
  raise LoadError.new("Invalid integer: #{str_or_num}") unless [::String, ::Integer].any? { |t| str_or_num.is_a?(t) }
  Integer(str_or_num)
rescue ArgumentError => e
  raise LoadError.new(e.message)
end