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

Inherits:
Type
  • Object
show all
Defined in:
lib/arjdbc/postgresql/base/oid.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Type

#infinity, #type

Constructor Details

#initialize(subtype) ⇒ Range

Returns a new instance of Range.



157
158
159
# File 'lib/arjdbc/postgresql/base/oid.rb', line 157

def initialize(subtype)
  @subtype = subtype
end

Instance Attribute Details

#subtypeObject (readonly)

Returns the value of attribute subtype.



154
155
156
# File 'lib/arjdbc/postgresql/base/oid.rb', line 154

def subtype
  @subtype
end

Instance Method Details

#extract_bounds(value) ⇒ Object



161
162
163
164
165
166
167
168
169
# File 'lib/arjdbc/postgresql/base/oid.rb', line 161

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

#infinity?(value) ⇒ Boolean

Returns:



171
172
173
# File 'lib/arjdbc/postgresql/base/oid.rb', line 171

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

#simplified_type(sql_type) ⇒ Object



155
# File 'lib/arjdbc/postgresql/base/oid.rb', line 155

def simplified_type(sql_type); sql_type.to_sym end

#type_cast(value) ⇒ Object



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/arjdbc/postgresql/base/oid.rb', line 179

def type_cast(value)
  return if value.nil? || 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 "Excluding the beginning of a Range is only partialy supported through `#succ`.\nThis is not reliable and will be removed in the future.\n      MESSAGE\n    else\n      raise ArgumentError, \"The Ruby Range object does not support excluding the beginning of a Range. (unsupported value: '\#{value}')\"\n    end\n  end\n  ::Range.new(from, to, extracted[:exclude_end])\nend\n"

#type_cast_single(value) ⇒ Object



175
176
177
# File 'lib/arjdbc/postgresql/base/oid.rb', line 175

def type_cast_single(value)
  infinity?(value) ? value : @subtype.type_cast(value)
end