Sha256: efeca72a4dc25b7f341587b60b3e5a2918482c99390775ae6635dc493ef06719

Contents?: true

Size: 1.58 KB

Versions: 18

Compression:

Stored size: 1.58 KB

Contents

# frozen_string_literal: true

module ActiveRecord
  module ConnectionAdapters
    module CipherStashPG
      Point = Struct.new(:x, :y)
      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)
            if quacks_like_a_point?(value)
              "(#{number_for_point(value.x)},#{number_for_point(value.y)})"
            elsif ::Array === value
              serialize(build_point(*value))
            else
              super
            end
          end

          def type_cast_for_schema(value)
            if quacks_like_a_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)
              CipherStashPG::Point.new(Float(x), Float(y))
            end

            def quacks_like_a_point?(value)
              value.respond_to(:x) && value.respond_to(:y)
            end
        end
      end
    end
  end
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
activerecord-cipherstash-pg-adapter-0.8.5 lib/active_record/connection_adapters/7.0/cipherstash_pg/oid/point.rb
activerecord-cipherstash-pg-adapter-0.8.4 lib/active_record/connection_adapters/7.0/cipherstash_pg/oid/point.rb
activerecord-cipherstash-pg-adapter-0.8.3 lib/active_record/connection_adapters/7.0/cipherstash_pg/oid/point.rb
activerecord-cipherstash-pg-adapter-0.8.2 lib/active_record/connection_adapters/7.0/cipherstash_pg/oid/point.rb
activerecord-cipherstash-pg-adapter-0.8.1 lib/active_record/connection_adapters/7.0/cipherstash_pg/oid/point.rb
activerecord-cipherstash-pg-adapter-0.8.0 lib/active_record/connection_adapters/7.0/cipherstash_pg/oid/point.rb
activerecord-cipherstash-pg-adapter-0.7.19 lib/active_record/connection_adapters/7.0/cipherstash_pg/oid/point.rb
activerecord-cipherstash-pg-adapter-0.7.18 lib/active_record/connection_adapters/7.0/cipherstash_pg/oid/point.rb
activerecord-cipherstash-pg-adapter-0.7.17 lib/active_record/connection_adapters/7.0/cipherstash_pg/oid/point.rb
activerecord-cipherstash-pg-adapter-0.7.16 lib/active_record/connection_adapters/7.0/cipherstash_pg/oid/point.rb
activerecord-cipherstash-pg-adapter-0.7.15 lib/active_record/connection_adapters/7.0/cipherstash_pg/oid/point.rb
activerecord-cipherstash-pg-adapter-0.7.14 lib/active_record/connection_adapters/7.0/cipherstash_pg/oid/point.rb
activerecord-cipherstash-pg-adapter-0.7.12 lib/active_record/connection_adapters/7.0/cipherstash_pg/oid/point.rb
activerecord-cipherstash-pg-adapter-0.7.11 lib/active_record/connection_adapters/7.0/cipherstash_pg/oid/point.rb
activerecord-cipherstash-pg-adapter-0.7.10 lib/active_record/connection_adapters/7.0/cipherstash_pg/oid/point.rb
activerecord-cipherstash-pg-adapter-0.7.9 lib/active_record/connection_adapters/7.0/cipherstash_pg/oid/point.rb
activerecord-cipherstash-pg-adapter-0.7.8 lib/active_record/connection_adapters/7.0/cipherstash_pg/oid/point.rb
activerecord-cipherstash-pg-adapter-0.7.7 lib/active_record/connection_adapters/7.0/cipherstash_pg/oid/point.rb