Sha256: 86a7386d44107d3796b6705ff5e247ac0fa0f9ba9a199cacd0f116776e88adc8

Contents?: true

Size: 1.75 KB

Versions: 13

Compression:

Stored size: 1.75 KB

Contents

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

13 entries across 13 versions & 1 rubygems

Version Path
torque-postgresql-1.1.8 lib/torque/postgresql/adapter/oid/range.rb
torque-postgresql-1.1.7 lib/torque/postgresql/adapter/oid/range.rb
torque-postgresql-2.0.3 lib/torque/postgresql/adapter/oid/range.rb
torque-postgresql-2.0.2 lib/torque/postgresql/adapter/oid/range.rb
torque-postgresql-1.1.6 lib/torque/postgresql/adapter/oid/range.rb
torque-postgresql-1.1.5 lib/torque/postgresql/adapter/oid/range.rb
torque-postgresql-2.0.1 lib/torque/postgresql/adapter/oid/range.rb
torque-postgresql-2.0.0 lib/torque/postgresql/adapter/oid/range.rb
torque-postgresql-1.1.4 lib/torque/postgresql/adapter/oid/range.rb
torque-postgresql-1.1.3 lib/torque/postgresql/adapter/oid/range.rb
torque-postgresql-1.1.2 lib/torque/postgresql/adapter/oid/range.rb
torque-postgresql-1.1.1 lib/torque/postgresql/adapter/oid/range.rb
torque-postgresql-1.1.0 lib/torque/postgresql/adapter/oid/range.rb