Class: ReeMapper::Rational

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

Instance Method Summary collapse

Instance Method Details

#cast(value) ⇒ Object



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

def cast(value)
  if value.is_a?(Rational)
    value
  elsif value.is_a?(String)
    begin
      Rational(value)
    rescue ArgumentError, ZeroDivisionError
      raise ReeMapper::CoercionError.new("is invalid rational, got `#{truncate(value.inspect)}`")
    end
  elsif value.is_a?(Numeric)
    Rational(value)
  else
    raise ReeMapper::TypeError.new("should be a rational, got `#{truncate(value.inspect)}`")
  end
end

#db_dump(value) ⇒ Object



31
32
33
# File 'lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/rational.rb', line 31

def db_dump(value)
  serialize(value).to_s
end

#db_load(value) ⇒ Object



36
37
38
# File 'lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/rational.rb', line 36

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/rational.rb', line 5

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