Sha256: fce8e4d0d356821a0a0738063c8f938ab6e52bf0c990964f761030a3753ea5b4

Contents?: true

Size: 1.47 KB

Versions: 28

Compression:

Stored size: 1.47 KB

Contents

# frozen_string_literal: true

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

  module ConnectionAdapters
    module CipherStashPG
      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.start_with?("(") && value.end_with?(")")
                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.delete_suffix(".0")
            end

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

Version data entries

28 entries across 17 versions & 1 rubygems

Version Path
activerecord-cipherstash-pg-adapter-0.8.5 lib/active_record/connection_adapters/7.1/cipherstash_pg/oid/point.rb
activerecord-cipherstash-pg-adapter-0.8.4 lib/active_record/connection_adapters/7.1/cipherstash_pg/oid/point.rb
activerecord-cipherstash-pg-adapter-0.8.3 lib/active_record/connection_adapters/7.1/cipherstash_pg/oid/point.rb
activerecord-cipherstash-pg-adapter-0.8.2 lib/active_record/connection_adapters/7.1/cipherstash_pg/oid/point.rb
activerecord-cipherstash-pg-adapter-0.7.5 lib/active_record/connection_adapters/6.1/cipherstash_pg/oid/point.rb
activerecord-cipherstash-pg-adapter-0.7.5 lib/active_record/connection_adapters/7.0/cipherstash_pg/oid/point.rb
activerecord-cipherstash-pg-adapter-0.7.4 lib/active_record/connection_adapters/7.0/cipherstash_pg/oid/point.rb
activerecord-cipherstash-pg-adapter-0.7.4 lib/active_record/connection_adapters/6.1/cipherstash_pg/oid/point.rb
activerecord-cipherstash-pg-adapter-0.7.3 lib/active_record/connection_adapters/7.0/cipherstash_pg/oid/point.rb
activerecord-cipherstash-pg-adapter-0.7.3 lib/active_record/connection_adapters/6.1/cipherstash_pg/oid/point.rb
activerecord-cipherstash-pg-adapter-0.7.2 lib/active_record/connection_adapters/7.0/cipherstash_pg/oid/point.rb
activerecord-cipherstash-pg-adapter-0.7.2 lib/active_record/connection_adapters/6.1/cipherstash_pg/oid/point.rb
activerecord-cipherstash-pg-adapter-0.7.1 lib/active_record/connection_adapters/6.1/cipherstash_pg/oid/point.rb
activerecord-cipherstash-pg-adapter-0.7.1 lib/active_record/connection_adapters/7.0/cipherstash_pg/oid/point.rb
activerecord-cipherstash-pg-adapter-0.7.0 lib/active_record/connection_adapters/7.0/cipherstash_pg/oid/point.rb
activerecord-cipherstash-pg-adapter-0.7.0 lib/active_record/connection_adapters/6.1/cipherstash_pg/oid/point.rb
activerecord-cipherstash-pg-adapter-0.6.1 lib/active_record/connection_adapters/7.0/cipherstash_pg/oid/point.rb
activerecord-cipherstash-pg-adapter-0.6.1 lib/active_record/connection_adapters/6.1/cipherstash_pg/oid/point.rb
activerecord-cipherstash-pg-adapter-0.6.0 lib/active_record/connection_adapters/6.1/cipherstash_pg/oid/point.rb
activerecord-cipherstash-pg-adapter-0.6.0 lib/active_record/connection_adapters/7.0/cipherstash_pg/oid/point.rb