forked from mongoid/mongoid-geospatial
-
Notifications
You must be signed in to change notification settings - Fork 7
/
point.rb
47 lines (39 loc) · 959 Bytes
/
point.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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