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

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

Overview

:nodoc:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(subtype, type = :range) ⇒ Range

Returns a new instance of Range.



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

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

Instance Attribute Details

#subtypeObject (readonly)

Returns the value of attribute subtype.



6
7
8
# File 'lib/active_record/connection_adapters/postgresql/oid/range.rb', line 6

def subtype
  @subtype
end

#typeObject (readonly)

Returns the value of attribute type.



6
7
8
# File 'lib/active_record/connection_adapters/postgresql/oid/range.rb', line 6

def type
  @type
end

Instance Method Details

#==(other) ⇒ Object



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

def ==(other)
  other.is_a?(Range) &&
    other.subtype == subtype &&
    other.type == type
end

#cast_value(value) ⇒ Object



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

def cast_value(value)
  return if value == "empty"
  return value unless value.is_a?(::String)

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

  if !infinity?(from) && extracted[:exclude_start]
    raise ArgumentError, "The Ruby Range object does not support excluding the beginning of a Range. (unsupported value: '#{value}')"
  end
  ::Range.new(from, to, extracted[:exclude_end])
end

#map(value) ⇒ Object

:nodoc:



48
49
50
51
52
# File 'lib/active_record/connection_adapters/postgresql/oid/range.rb', line 48

def map(value) # :nodoc:
  new_begin = yield(value.begin)
  new_end = yield(value.end)
  ::Range.new(new_begin, new_end, value.exclude_end?)
end

#serialize(value) ⇒ Object



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

def serialize(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



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

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