Class: Mongoid::Geospatial::Point

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/mongoid_geospatial/fields/point.rb,
lib/mongoid_geospatial/wrappers/rgeo.rb,
lib/mongoid_geospatial/wrappers/georuby.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(x = nil, y = nil) ⇒ Point

Returns a new instance of Point.



7
8
9
# File 'lib/mongoid_geospatial/fields/point.rb', line 7

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

Instance Attribute Details

#xObject

Returns the value of attribute x.



5
6
7
# File 'lib/mongoid_geospatial/fields/point.rb', line 5

def x
  @x
end

#yObject

Returns the value of attribute y.



5
6
7
# File 'lib/mongoid_geospatial/fields/point.rb', line 5

def y
  @y
end

Class Method Details

.demongoize(object) ⇒ Object

Database -> Object



66
67
68
69
# File 'lib/mongoid_geospatial/fields/point.rb', line 66

def demongoize(object)
  return unless object
  Point.new(*object)
end

.evolve(object) ⇒ Object

Converts the object that was supplied to a criteria into a database friendly form.



82
83
84
# File 'lib/mongoid_geospatial/fields/point.rb', line 82

def evolve(object)
  object.respond_to?(:x) ? object.mongoize : object
end

.mongoize(obj) ⇒ Object



71
72
73
74
75
76
77
78
# File 'lib/mongoid_geospatial/fields/point.rb', line 71

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

Instance Method Details

#[](args) ⇒ Object



18
19
20
# File 'lib/mongoid_geospatial/fields/point.rb', line 18

def [](args)
  mongoize[args]
end

#distance(other) ⇒ Object



12
13
14
# File 'lib/mongoid_geospatial/wrappers/rgeo.rb', line 12

def distance other
  to_geo.distance other.to_geo
end

#each {|x| ... } ⇒ Object

Yields:

  • (x)


22
23
24
25
# File 'lib/mongoid_geospatial/fields/point.rb', line 22

def each
  yield x
  yield y
end

#mongoizeObject

Object -> Database



12
13
14
# File 'lib/mongoid_geospatial/fields/point.rb', line 12

def mongoize
  [x, y]
end

#radius(r = 1) ⇒ Object



32
33
34
# File 'lib/mongoid_geospatial/fields/point.rb', line 32

def radius r = 1
  [mongoize, r]
end

#radius_sphere(r = 1, unit = :km) ⇒ Object



36
37
38
# File 'lib/mongoid_geospatial/fields/point.rb', line 36

def radius_sphere r = 1, unit = :km
  radius r.to_f/Mongoid::Geospatial.earth_radius[unit]
end

#to_aObject

Object -> Database



15
16
17
# File 'lib/mongoid_geospatial/fields/point.rb', line 15

def mongoize
  [x, y]
end

#to_geoObject



8
9
10
# File 'lib/mongoid_geospatial/wrappers/rgeo.rb', line 8

def to_geo
  RGeo::Geographic.spherical_factory.point x, y
end

#to_hsh(xl = :x, yl = :y) ⇒ Object Also known as: to_hash



27
28
29
# File 'lib/mongoid_geospatial/fields/point.rb', line 27

def to_hsh xl = :x, yl = :y
  {xl => x, yl => y}
end

#to_xyObject

Object -> Database



16
17
18
# File 'lib/mongoid_geospatial/fields/point.rb', line 16

def mongoize
  [x, y]
end