123456789_123456789_123456789_123456789_123456789_

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

Do not use. This class is for internal use only.
Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
Instance Chain:
Inherits: ActiveModel::Type::Value
Defined in: activerecord/lib/active_record/connection_adapters/postgresql/oid/range.rb

Constant Summary

  • BOUNDS =

    Matches the comma-separated lower and upper bounds of a range's textual representation when a bound is double-quoted. A quoted bound can itself contain a comma, so a naive split on the first comma would corrupt such values. Within a double-quoted bound, literal " and \ are escaped (as "" / " and \ respectively), so those escapes are skipped while scanning for the separating comma.

    An unquoted bound never contains a comma or a double-quote (PostgreSQL quotes the whole bound when it would), so the alternation matches an unquoted run with [^,"] rather than [^,]. Excluding " keeps the two alternatives mutually exclusive, which removes any ambiguity over how a "-prefixed bound is consumed. The /m flag lets the captured upper bound (and quoted bounds) span newlines.

    # File 'activerecord/lib/active_record/connection_adapters/postgresql/oid/range.rb', line 109
    /\A((?:"(?:[^"\\]|""|\\.)*"|[^,"])*),(.*)\z/m
  • INFINITE_FLOAT_RANGE =
    # File 'activerecord/lib/active_record/connection_adapters/postgresql/oid/range.rb', line 122
    (-::Float::INFINITY)..(::Float::INFINITY)

Class Method Summary

::ActiveModel::Type::Value - Inherited

.new

Initializes a type with three basic configuration settings: precision, limit, and scale.

Instance Attribute Summary

::ActiveModel::Type::Value - Inherited

#limit, #precision, #scale,
#binary?

These predicates are not documented, as I need to look further into their use, and see if they can be removed entirely.

#mutable?, #serialized?

Instance Method Summary

::ActiveModel::Type::Value - Inherited

#==, #as_json, #assert_valid_value,
#cast

::ActiveRecord::Type casts a value from user input (e.g. from a setter).

#changed?

Determines whether a value has changed for dirty checking.

#changed_in_place?

Determines whether the mutable value has been modified since it was read.

#deserialize

Converts a value from database input to the appropriate ruby type.

#eql?
#hash,
#serializable?

Returns true if this type can convert value to a type that is usable by the database.

#serialize

Casts a value from the ruby type to a type that the database knows how to understand.

#type

Returns the unique type name as a ::Symbol.

#cast_value

Convenience method for types which do not need separate type casting behavior for user and database inputs.

#force_equality?, #map,
#type_cast_for_schema

::ActiveRecord::Type casts a value for schema dumping.

#value_constructed_by_mass_assignment?

::ActiveModel::Type::SerializeCastValue - Included

Constructor Details

.new(subtype, type = :range) ⇒ Range

[ GitHub ]

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

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

Instance Attribute Details

#subtype (readonly)

[ GitHub ]

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

attr_reader :subtype, :type

#type (readonly)

[ GitHub ]

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

attr_reader :subtype, :type

#user_input_in_time_zone (readonly)

[ GitHub ]

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

delegate :user_input_in_time_zone, to: :subtype

Instance Method Details

#==(other)

[ GitHub ]

  
# File 'activerecord/lib/active_record/connection_adapters/postgresql/oid/range.rb', line 47

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

#bound_for_schema(bound) (private)

[ GitHub ]

  
# File 'activerecord/lib/active_record/connection_adapters/postgresql/oid/range.rb', line 64

def bound_for_schema(bound)
  case bound
  when nil
    "nil"
  when ::Float::INFINITY
    "::Float::INFINITY"
  when -::Float::INFINITY
    "-::Float::INFINITY"
  else
    @subtype.type_cast_for_schema(bound)
  end
end

#cast_value(value)

[ GitHub ]

  
# File 'activerecord/lib/active_record/connection_adapters/postgresql/oid/range.rb', line 23

def cast_value(value)
  return if ["empty", ""].include? value
  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(*sanitize_bounds(from, to), extracted[:exclude_end])
end

#extract_bounds(value) (private)

[ GitHub ]

  
# File 'activerecord/lib/active_record/connection_adapters/postgresql/oid/range.rb', line 85

def extract_bounds(value)
  from, to = split_bounds(value[1..-2])
  {
    from:          (from == "" || from == "-infinity") ? infinity(negative: true) : unquote(from),
    to:            (to == "" || to == "infinity") ? infinity : unquote(to),
    exclude_start: value.start_with?("("),
    exclude_end:   value.end_with?(")")
  }
end

#force_equality?(value) ⇒ Boolean

[ GitHub ]

  
# File 'activerecord/lib/active_record/connection_adapters/postgresql/oid/range.rb', line 59

def force_equality?(value)
  value.is_a?(::Range)
end

#infinity(negative: false) (private)

[ GitHub ]

  
# File 'activerecord/lib/active_record/connection_adapters/postgresql/oid/range.rb', line 150

def infinity(negative: false)
  if subtype.respond_to?(:infinity)
    subtype.infinity(negative: negative)
  elsif negative
    -::Float::INFINITY
  else
    ::Float::INFINITY
  end
end

#infinity?(value) ⇒ Boolean (private)

[ GitHub ]

  
# File 'activerecord/lib/active_record/connection_adapters/postgresql/oid/range.rb', line 160

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

#map(value)

[ GitHub ]

  
# File 'activerecord/lib/active_record/connection_adapters/postgresql/oid/range.rb', line 53

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

#sanitize_bounds(from, to) (private)

[ GitHub ]

  
# File 'activerecord/lib/active_record/connection_adapters/postgresql/oid/range.rb', line 124

def sanitize_bounds(from, to)
  [
    (from == -::Float::INFINITY && !INFINITE_FLOAT_RANGE.cover?(to)) ? nil : from,
    (to == ::Float::INFINITY && !INFINITE_FLOAT_RANGE.cover?(from)) ? nil : to
  ]
end

#serialize(value)

[ GitHub ]

  
# File 'activerecord/lib/active_record/connection_adapters/postgresql/oid/range.rb', line 37

def serialize(value)
  if value.is_a?(::Range)
    from = type_cast_single_for_database(value.begin)
    to = type_cast_single_for_database(value.end)
    ::Range.new(from, to, value.exclude_end?)
  else
    super
  end
end

#split_bounds(value) (private)

[ GitHub ]

  
# File 'activerecord/lib/active_record/connection_adapters/postgresql/oid/range.rb', line 111

def split_bounds(value)
  # Fast path: an unquoted representation (every built-in range
  # type -- int/num/date/timestamp) has no embedded comma, so a
  # plain split is correct and avoids the regexp. Only quoted
  # bounds (custom text/varchar/money/... ranges) need the
  # comma-skipping scan.
  return value.split(",", 2) unless value.include?('"')

  (match = BOUNDS.match(value)) ? [match[1], match[2]] : [value, nil]
end

#type_cast_for_schema(value)

[ GitHub ]

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

def type_cast_for_schema(value)
  from = bound_for_schema(value.begin)
  to   = bound_for_schema(value.end)
  op   = value.exclude_end? ? "..." : ".."
  "#{from}#{op}#{to}"
end

#type_cast_single(value) (private)

[ GitHub ]

  
# File 'activerecord/lib/active_record/connection_adapters/postgresql/oid/range.rb', line 77

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

#type_cast_single_for_database(value) (private)

[ GitHub ]

  
# File 'activerecord/lib/active_record/connection_adapters/postgresql/oid/range.rb', line 81

def type_cast_single_for_database(value)
  infinity?(value) ? value : @subtype.serialize(@subtype.cast(value))
end

#unquote(value) (private)

When formatting the bound values of range types, ::ActiveRecord::ConnectionAdapters::PostgreSQL quotes the bound value using double-quotes in certain conditions. Within a double-quoted string, literal " and \ characters are themselves escaped. In input, ::ActiveRecord::ConnectionAdapters::PostgreSQL accepts multiple escape styles for " (either " or "") but in output always uses "". See:

[ GitHub ]

  
# File 'activerecord/lib/active_record/connection_adapters/postgresql/oid/range.rb', line 139

def unquote(value)
  if value && value.start_with?('"') && value.end_with?('"')
    unquoted_value = value[1..-2]
    unquoted_value.gsub!('""', '"')
    unquoted_value.gsub!("\\\\", "\\")
    unquoted_value
  else
    value
  end
end