Sha256: 159aa96f057b774ec9883009d6a354747e196d201d71eb1ef3aca8b62bb2cfb2

Contents?: true

Size: 1.53 KB

Versions: 2

Compression:

Stored size: 1.53 KB

Contents

# frozen_string_literal: true

module ActiveRecordMysqlSpatial
  module ActiveRecord
    module MySQL
      class Point < Base
        attr_reader :x,
                    :y

        def type
          :point
        end

        def serialize(value)
          if value.is_a?(Point)
            value
          else
            return unless valid_hash?(value, type: :point)

            cast_value(value)
          end
        end

        def to_sql
          return nil if @x.nil? || @y.nil?

          "Point(#{to_coordinate_sql})"
        end

        def to_coordinate_sql
          return nil if @x.nil? || @y.nil?

          "#{@x} #{@y}"
        end

        alias to_coordinates_sql to_coordinate_sql

        def ==(other)
          x == other.x && y == other.y
        end

        private

        def cast_value(value)
          @raw = value
          coordinate, create_raw = extract_coordinates(value, type: :point)

          @raw = Geometry.from_coordinates(coordinate, type: :point).as_binary if create_raw

          @x, @y = if coordinate.is_a?(Array) && coordinate.present?
                     [coordinate[0], coordinate[1]]
                   elsif value.is_a?(Array)
                     [value[0], value[1]]
                   elsif value.is_a?(Hash)
                     [value[:x] || value['x'], value[:y] || value['y']]
                   else
                     [nil, nil]
                   end

          self
        rescue StandardError => e
          @error = e.message

          self
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
active_record_mysql_spatial-0.2.0 lib/active_record_mysql_spatial/active_record/mysql/point.rb
active_record_mysql_spatial-0.1.0 lib/active_record_mysql_spatial/active_record/mysql/point.rb