Sha256: 4427ef86f5f3be01ef15b217adbc519a45f8cae71ea6d0841d0219a21d9ba9a9

Contents?: true

Size: 1.46 KB

Versions: 17

Compression:

Stored size: 1.46 KB

Contents

# frozen_string_literal: true

module ActiveRecord
  Point = Struct.new(:x, :y)

  module ConnectionAdapters
    module PostgreSQL
      module OID # :nodoc:
        class Point < Type::Value # :nodoc:
          include ActiveModel::Type::Helpers::Mutable

          def type
            :point
          end

          def cast(value)
            case value
            when ::String
              return if value.blank?

              if value[0] == "(" && value[-1] == ")"
                value = value[1...-1]
              end
              x, y = value.split(",")
              build_point(x, y)
            when ::Array
              build_point(*value)
            else
              value
            end
          end

          def serialize(value)
            case value
            when ActiveRecord::Point
              "(#{number_for_point(value.x)},#{number_for_point(value.y)})"
            when ::Array
              serialize(build_point(*value))
            else
              super
            end
          end

          def type_cast_for_schema(value)
            if ActiveRecord::Point === value
              [value.x, value.y]
            else
              super
            end
          end

          private

            def number_for_point(number)
              number.to_s.gsub(/\.0$/, "")
            end

            def build_point(x, y)
              ActiveRecord::Point.new(Float(x), Float(y))
            end
        end
      end
    end
  end
end

Version data entries

17 entries across 17 versions & 4 rubygems

Version Path
activerecord-6.0.2.2 lib/active_record/connection_adapters/postgresql/oid/point.rb
argon-1.3.1 vendor/bundle/ruby/2.7.0/gems/activerecord-6.0.2.1/lib/active_record/connection_adapters/postgresql/oid/point.rb
symbolic_enum-1.1.5 vendor/bundle/ruby/2.7.0/gems/activerecord-6.0.2.1/lib/active_record/connection_adapters/postgresql/oid/point.rb
activerecord-6.0.2.1 lib/active_record/connection_adapters/postgresql/oid/point.rb
activerecord-6.0.2 lib/active_record/connection_adapters/postgresql/oid/point.rb
activerecord-6.0.2.rc2 lib/active_record/connection_adapters/postgresql/oid/point.rb
activerecord-6.0.2.rc1 lib/active_record/connection_adapters/postgresql/oid/point.rb
activerecord-6.0.1 lib/active_record/connection_adapters/postgresql/oid/point.rb
chatops-rpc-0.0.2 fixtures/chatops-controller-example/vendor/bundle/ruby/2.5.0/gems/activerecord-6.0.0/lib/active_record/connection_adapters/postgresql/oid/point.rb
activerecord-6.0.1.rc1 lib/active_record/connection_adapters/postgresql/oid/point.rb
chatops-rpc-0.0.1 fixtures/chatops-controller-example/vendor/bundle/ruby/2.5.0/gems/activerecord-6.0.0/lib/active_record/connection_adapters/postgresql/oid/point.rb
activerecord-6.0.0 lib/active_record/connection_adapters/postgresql/oid/point.rb
activerecord-6.0.0.rc2 lib/active_record/connection_adapters/postgresql/oid/point.rb
activerecord-6.0.0.rc1 lib/active_record/connection_adapters/postgresql/oid/point.rb
activerecord-6.0.0.beta3 lib/active_record/connection_adapters/postgresql/oid/point.rb
activerecord-6.0.0.beta2 lib/active_record/connection_adapters/postgresql/oid/point.rb
activerecord-6.0.0.beta1 lib/active_record/connection_adapters/postgresql/oid/point.rb