Sha256: 49d6c70452b0372ac0dccec8d5abdc1053c2f57537b0b8bdf6bc48cb8183b7b4

Contents?: true

Size: 959 Bytes

Versions: 1

Compression:

Stored size: 959 Bytes

Contents

module Mongoid
  module Geospatial
    class Point
      attr_accessor :x, :y

      def initialize(x, y)
        @x, @y = x, y
      end

      # Object -> Database
      def mongoize
        [x, y]
      end
      alias :to_a  :mongoize
      alias :to_xy :mongoize

      def [](args)
        mongoize[args]
      end

      class << self

        # Database -> Object
        def demongoize(object)
          # return unless object && !object.empty?
          Point.new(object[0], object[1])
        end

        def mongoize(object)
          case object
          when Point then object.mongoize
          when Array then object.to_xy
          when Hash then object.to_xy
          else object
          end
        end

        # Converts the object that was supplied to a criteria
        # into a database friendly form.
        def evolve(object)
          object.respond_to?(:x) ? object.mongoize : object
        end
      end

    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
mongoid_geospatial-2.2.0 lib/mongoid_geospatial/fields/point.rb