Class: ActiveRecord::ConnectionAdapters::PostgreSQLAdapter::OID::Range

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Type

#type, #type_cast_for_write

Constructor Details

#initialize(subtype) ⇒ Range

Returns a new instance of Range.



109
110
111
# File 'lib/active_record/connection_adapters/postgresql/oid.rb', line 109

def initialize(subtype)
  @subtype = subtype
end

Instance Attribute Details

#subtypeObject (readonly)

Returns the value of attribute subtype.



108
109
110
# File 'lib/active_record/connection_adapters/postgresql/oid.rb', line 108

def subtype
  @subtype
end

Instance Method Details

#extract_bounds(value) ⇒ Object



113
114
115
116
117
118
119
120
121
# File 'lib/active_record/connection_adapters/postgresql/oid.rb', line 113

def extract_bounds(value)
  from, to = value[1..-2].split(',')
  {
    from:          (value[1] == ',' || from == '-infinity') ? infinity(:negative => true) : from,
    to:            (value[-2] == ',' || to == 'infinity') ? infinity : to,
    exclude_start: (value[0] == '('),
    exclude_end:   (value[-1] == ')')
  }
end

#infinity(options = {}) ⇒ Object



123
124
125
# File 'lib/active_record/connection_adapters/postgresql/oid.rb', line 123

def infinity(options = {})
  ::Float::INFINITY * (options[:negative] ? -1 : 1)
end

#infinity?(value) ⇒ Boolean

Returns:



127
128
129
# File 'lib/active_record/connection_adapters/postgresql/oid.rb', line 127

def infinity?(value)
  value.respond_to?(:infinite?) && value.infinite?
end

#to_integer(value) ⇒ Object



131
132
133
# File 'lib/active_record/connection_adapters/postgresql/oid.rb', line 131

def to_integer(value)
  infinity?(value) ? value : value.to_i
end

#type_cast(value) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/active_record/connection_adapters/postgresql/oid.rb', line 135

def type_cast(value)
  return if value.nil? || value == 'empty'
  return value if value.is_a?(::Range)

  extracted = extract_bounds(value)

  case @subtype
  when :date
    from  = ConnectionAdapters::Column.value_to_date(extracted[:from])
    from -= 1.day if extracted[:exclude_start]
    to    = ConnectionAdapters::Column.value_to_date(extracted[:to])
  when :decimal
    from  = BigDecimal.new(extracted[:from].to_s)
    # FIXME: add exclude start for ::Range, same for timestamp ranges
    to    = BigDecimal.new(extracted[:to].to_s)
  when :time
    from = ConnectionAdapters::Column.string_to_time(extracted[:from])
    to   = ConnectionAdapters::Column.string_to_time(extracted[:to])
  when :integer
    from = to_integer(extracted[:from]) rescue value ? 1 : 0
    from -= 1 if extracted[:exclude_start]
    to   = to_integer(extracted[:to]) rescue value ? 1 : 0
  else
    return value
  end

  ::Range.new(from, to, extracted[:exclude_end])
end