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

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 44

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

#cast_value(value)

[ GitHub ]

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

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 69

def extract_bounds(value)
  from, to = value[1..-2].split(",", 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 56

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 107

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 117

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 50

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 81

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 34

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

#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)
  value.inspect.gsub("Infinity", "::Float::INFINITY")
end

#type_cast_single(value) (private)

[ GitHub ]

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

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 65

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 96

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