point.rb 957 字节
Newer Older
1 2 3 4
module ActiveRecord
  module ConnectionAdapters
    module PostgreSQL
      module OID # :nodoc:
5
        class Point < Type::Value # :nodoc:
6
          include Type::Helpers::Mutable
7

8 9 10 11
          def type
            :point
          end

S
Sean Griffin 已提交
12
          def cast(value)
13 14
            case value
            when ::String
15 16 17
              if value[0] == '(' && value[-1] == ')'
                value = value[1...-1]
              end
S
Sean Griffin 已提交
18
              cast(value.split(','))
19 20
            when ::Array
              value.map { |v| Float(v) }
21 22 23 24
            else
              value
            end
          end
25

26
          def serialize(value)
27
            if value.is_a?(::Array)
28
              "(#{number_for_point(value[0])},#{number_for_point(value[1])})"
29 30 31 32
            else
              super
            end
          end
33 34 35 36 37 38

          private

          def number_for_point(number)
            number.to_s.gsub(/\.0$/, '')
          end
39 40 41 42 43
        end
      end
    end
  end
end