# 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