lib/mongoid_geospatial/fields/point.rb in mongoid_geospatial-2.8.3 vs lib/mongoid_geospatial/fields/point.rb in mongoid_geospatial-3.0.0
- old
+ new
@@ -1,13 +1,15 @@
module Mongoid
module Geospatial
class Point
include Enumerable
- attr_accessor :x, :y
+ attr_reader :x, :y
- def initialize(x=nil, y=nil)
- @x, @y = x, y
+ def initialize(x = nil, y = nil)
+ return unless x
+ ll = y ? [x, y] : x.split(/,|\s/).reject(&:empty?)
+ @x, @y = ll.map(&:to_f)
end
# Object -> Database
def mongoize
[x, y]
@@ -22,10 +24,14 @@
def each
yield x
yield y
end
+ def to_s
+ "#{x}, #{y}"
+ end
+
def to_hsh xl = :x, yl = :y
{xl => x, yl => y}
end
alias :to_hash :to_hsh
@@ -62,19 +68,18 @@
class << self
# Database -> Object
def demongoize(object)
- return unless object
- Point.new(*object)
+ Point.new(*object) if object
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
+ when Array then Geospatial.from_array(object)
+ when Hash then Geospatial.from_hash(object)
+ else object.mongoize
end
end
# Converts the object that was supplied to a criteria
# into a database friendly form.