Sha256: acc8fd8fb3796bf180674d6065d9c5dea27a15f2d10df24cf22104d8aa6f7f9b

Contents?: true

Size: 1.78 KB

Versions: 31

Compression:

Stored size: 1.78 KB

Contents

# frozen_string_literal: true

module Torque
  module PostgreSQL
    module Adapter
      module OID
        class Range < ActiveRecord::ConnectionAdapters::PostgreSQL::OID::Range
          HASH_PICK = %i[from start end to].freeze

          module Comparasion
            def <=>(other)
              return super unless other.acts_like?(:date) || other.acts_like?(:time)
              other = other.to_time if other.acts_like?(:date)
              super other.to_i
            end
          end

          def cast_value(value)
            case value
            when Array
              cast_custom(value[0], value[1])
            when Hash
              pieces = value.with_indifferent_access.values_at(*HASH_PICK)
              cast_custom(pieces[0] || pieces[1], pieces[2] || pieces[3])
            else
              super
            end
          end

          def map(value) # :nodoc:
            return value unless value.respond_to?(:first)
            from = yield(value.first)
            to = yield(value.last)
            cast_custom(from, to)
          end

          private

            def cast_custom(from, to)
              from = custom_cast_single(from, true)
              to = custom_cast_single(to)
              ::Range.new(from, to)
            end

            def custom_cast_single(value, negative = false)
              value.blank? ? custom_infinity(negative) : subtype.deserialize(value)
            end

            def custom_infinity(negative)
              negative ? -::Float::INFINITY : ::Float::INFINITY
            end
        end

        ::ActiveRecord::ConnectionAdapters::PostgreSQL::OID.send(:remove_const, :Range)
        ::ActiveRecord::ConnectionAdapters::PostgreSQL::OID.const_set(:Range, Range)

        ::Float.prepend(Range::Comparasion)
      end
    end
  end
end

Version data entries

31 entries across 31 versions & 1 rubygems

Version Path
torque-postgresql-3.4.1 lib/torque/postgresql/adapter/oid/range.rb
torque-postgresql-2.4.5 lib/torque/postgresql/adapter/oid/range.rb
torque-postgresql-3.4.0 lib/torque/postgresql/adapter/oid/range.rb
torque-postgresql-3.3.3 lib/torque/postgresql/adapter/oid/range.rb
torque-postgresql-2.4.4 lib/torque/postgresql/adapter/oid/range.rb
torque-postgresql-3.3.2 lib/torque/postgresql/adapter/oid/range.rb
torque-postgresql-2.4.3 lib/torque/postgresql/adapter/oid/range.rb
torque-postgresql-3.3.1 lib/torque/postgresql/adapter/oid/range.rb
torque-postgresql-3.3.0 lib/torque/postgresql/adapter/oid/range.rb
torque-postgresql-3.2.2 lib/torque/postgresql/adapter/oid/range.rb
torque-postgresql-2.4.2 lib/torque/postgresql/adapter/oid/range.rb
torque-postgresql-2.4.1 lib/torque/postgresql/adapter/oid/range.rb
torque-postgresql-3.2.1 lib/torque/postgresql/adapter/oid/range.rb
torque-postgresql-2.4.0 lib/torque/postgresql/adapter/oid/range.rb
torque-postgresql-3.2.0 lib/torque/postgresql/adapter/oid/range.rb
torque-postgresql-3.1.0 lib/torque/postgresql/adapter/oid/range.rb
torque-postgresql-2.3.0 lib/torque/postgresql/adapter/oid/range.rb
torque-postgresql-3.0.1 lib/torque/postgresql/adapter/oid/range.rb
torque-postgresql-2.2.4 lib/torque/postgresql/adapter/oid/range.rb
torque-postgresql-3.0.0 lib/torque/postgresql/adapter/oid/range.rb