Class: ActiveRecord::ConnectionAdapters::PostgreSQL::OID::Range

Inherits:
Type::Value show all
Defined in:
activerecord/lib/active_record/connection_adapters/postgresql/oid/range.rb

Overview

:nodoc:

Instance Attribute Summary collapse

Attributes inherited from Type::Value

#limit, #precision, #scale

Instance Method Summary collapse

Methods inherited from Type::Value

#==, #binary?, #changed?, #changed_in_place?, #hash, #klass, #number?, #text?, #type_cast_from_database, #type_cast_from_user

Constructor Details

#initialize(subtype, type) ⇒ Range

Returns a new instance of Range.


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

def initialize(subtype, type)
  @subtype = subtype
  @type = type
end

Instance Attribute Details

#subtypeObject (readonly)

Returns the value of attribute subtype


8
9
10
# File 'activerecord/lib/active_record/connection_adapters/postgresql/oid/range.rb', line 8

def subtype
  @subtype
end

#typeObject (readonly)

Returns the value of attribute type


8
9
10
# File 'activerecord/lib/active_record/connection_adapters/postgresql/oid/range.rb', line 8

def type
  @type
end

Instance Method Details

#cast_value(value) ⇒ Object


19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'activerecord/lib/active_record/connection_adapters/postgresql/oid/range.rb', line 19

def cast_value(value)
  return if value == 'empty'
  return value if value.is_a?(::Range)

  extracted = extract_bounds(value)
  from = type_cast_single extracted[:from]
  to = type_cast_single extracted[:to]

  if !infinity?(from) && extracted[:exclude_start]
    if from.respond_to?(:succ)
      from = from.succ
      ActiveSupport::Deprecation.warn(<<-MSG.squish)
        Excluding the beginning of a Range is only partialy supported
        through `#succ`. This is not reliable and will be removed in
        the future.
      MSG
    else
      raise ArgumentError, "The Ruby Range object does not support excluding the beginning of a Range. (unsupported value: '#{value}')"
    end
  end
  ::Range.new(from, to, extracted[:exclude_end])
end

#type_cast_for_database(value) ⇒ Object


42
43
44
45
46
47
48
49
50
# File 'activerecord/lib/active_record/connection_adapters/postgresql/oid/range.rb', line 42

def type_cast_for_database(value)
  if value.is_a?(::Range)
    from = type_cast_single_for_database(value.begin)
    to = type_cast_single_for_database(value.end)
    "[#{from},#{to}#{value.exclude_end? ? ')' : ']'}"
  else
    super
  end
end

#type_cast_for_schema(value) ⇒ Object


15
16
17
# File 'activerecord/lib/active_record/connection_adapters/postgresql/oid/range.rb', line 15

def type_cast_for_schema(value)
  value.inspect.gsub('Infinity', '::Float::INFINITY')
end