Class: ActiveRecord::ConnectionAdapters::PostgreSQL::OID::Interval

Inherits:
Type::Value
  • Object
show all
Defined in:
lib/active_record/connection_adapters/postgresql/oid/interval.rb

Overview

:nodoc:

Instance Method Summary collapse

Instance Method Details

#cast_value(value) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/active_record/connection_adapters/postgresql/oid/interval.rb', line 14

def cast_value(value)
  case value
  when ::ActiveSupport::Duration
    value
  when ::String
    begin
      ::ActiveSupport::Duration.parse(value)
    rescue ::ActiveSupport::Duration::ISO8601Parser::ParsingError
      nil
    end
  else
    super
  end
end

#serialize(value) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/active_record/connection_adapters/postgresql/oid/interval.rb', line 29

def serialize(value)
  case value
  when ::ActiveSupport::Duration
    value.iso8601(precision: self.precision)
  when ::Numeric
    # Sometimes operations on Times returns just float number of seconds so we need to handle that.
    # Example: Time.current - (Time.current + 1.hour) # => -3600.000001776 (Float)
    value.seconds.iso8601(precision: self.precision)
  else
    super
  end
end

#typeObject



10
11
12
# File 'lib/active_record/connection_adapters/postgresql/oid/interval.rb', line 10

def type
  :interval
end

#type_cast_for_schema(value) ⇒ Object



42
43
44
# File 'lib/active_record/connection_adapters/postgresql/oid/interval.rb', line 42

def type_cast_for_schema(value)
  serialize(value).inspect
end