Class: ActiveRecord::ConnectionAdapters::PostgreSQL::OID::DateTime

Inherits:
Type::DateTime show all
Includes:
Infinity
Defined in:
lib/active_record/connection_adapters/postgresql/oid/date_time.rb

Overview

:nodoc:

Instance Attribute Summary

Attributes inherited from Type::Value

#limit, #precision, #scale

Instance Method Summary collapse

Methods included from Infinity

#infinity

Methods inherited from Type::DateTime

#type

Methods included from Type::TimeValue

#klass, #type_cast_for_schema

Methods inherited from Type::Value

#==, #binary?, #changed?, #changed_in_place?, #initialize, #klass, #number?, #text?, #type, #type_cast_for_schema, #type_cast_from_database, #type_cast_from_user

Constructor Details

This class inherits a constructor from ActiveRecord::Type::Value

Instance Method Details

#cast_value(value) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/active_record/connection_adapters/postgresql/oid/date_time.rb', line 17

def cast_value(value)
  if value.is_a?(::String)
    case value
    when 'infinity' then ::Float::INFINITY
    when '-infinity' then -::Float::INFINITY
    when / BC$/
      astronomical_year = format("%04d", -value[/^\d+/].to_i + 1)
      super(value.sub(/ BC$/, "").sub(/^\d+/, astronomical_year))
    else
      super
    end
  else
    value
  end
end

#type_cast_for_database(value) ⇒ Object



8
9
10
11
12
13
14
15
# File 'lib/active_record/connection_adapters/postgresql/oid/date_time.rb', line 8

def type_cast_for_database(value)
  if has_precision? && value.acts_like?(:time) && value.year <= 0
    bce_year = format("%04d", -value.year + 1)
    super.sub(/^-?\d+/, bce_year) + " BC"
  else
    super
  end
end