Class: ReeMapper::Integer

Inherits:
AbstractType show all
Defined in:
lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/integer.rb

Instance Method Summary collapse

Instance Method Details

#cast(value) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/integer.rb', line 14

def cast(value)
  if value.is_a?(Integer)
    value
  elsif value.is_a?(String)
    coerced_value = Integer(value, exception: false)
    if coerced_value.nil?
      raise ReeMapper::CoercionError.new("is invalid integer, got `#{truncate(value.inspect)}`")
    end
    coerced_value
  else
    raise ReeMapper::TypeError.new("should be an integer, got `#{truncate(value.inspect)}`")
  end
end

#db_dump(value) ⇒ Object



29
30
31
# File 'lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/integer.rb', line 29

def db_dump(value)
  serialize(value)
end

#db_load(value) ⇒ Object



34
35
36
# File 'lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/integer.rb', line 34

def db_load(value)
  cast(value)
end

#serialize(value) ⇒ Object



5
6
7
8
9
10
11
# File 'lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/integer.rb', line 5

def serialize(value)
  if value.is_a? Integer
    value
  else
    raise ReeMapper::TypeError.new("should be an integer, got `#{truncate(value.inspect)}`")
  end
end