Class: Mongoid::Geospatial::Point
- Inherits:
-
Object
- Object
- Mongoid::Geospatial::Point
- 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
-
#x ⇒ Object
readonly
Returns the value of attribute x.
-
#y ⇒ Object
readonly
Returns the value of attribute y.
Class Method Summary collapse
-
.demongoize(object) ⇒ Object
Database -> Object.
-
.evolve(object) ⇒ Object
Converts the object that was supplied to a criteria into a database friendly form.
- .mongoize(object) ⇒ Object
Instance Method Summary collapse
- #[](args) ⇒ Object
- #each {|x| ... } ⇒ Object
- #geo_distance(other) ⇒ Object
-
#initialize(x = nil, y = nil) ⇒ Point
constructor
A new instance of Point.
-
#mongoize ⇒ Object
Object -> Database.
- #radius(r = 1) ⇒ Object
- #radius_sphere(r = 1, unit = :km) ⇒ Object
- #rgeo_distance(other) ⇒ Object
-
#to_a ⇒ Object
Object -> Database.
- #to_geo ⇒ Object
- #to_hsh(xl = :x, yl = :y) ⇒ Object (also: #to_hash)
- #to_rgeo ⇒ Object
- #to_s ⇒ Object
-
#to_xy ⇒ Object
Object -> Database.
Constructor Details
#initialize(x = nil, y = nil) ⇒ Point
Returns a new instance of Point.
7 8 9 10 11 |
# File 'lib/mongoid_geospatial/fields/point.rb', line 7 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 |
Instance Attribute Details
#x ⇒ Object (readonly)
Returns the value of attribute x.
5 6 7 |
# File 'lib/mongoid_geospatial/fields/point.rb', line 5 def x @x end |
#y ⇒ Object (readonly)
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
72 73 74 |
# File 'lib/mongoid_geospatial/fields/point.rb', line 72 def demongoize(object) Point.new(*object) if object end |
.evolve(object) ⇒ Object
Converts the object that was supplied to a criteria into a database friendly form.
87 88 89 |
# File 'lib/mongoid_geospatial/fields/point.rb', line 87 def evolve(object) object.respond_to?(:x) ? object.mongoize : object end |
.mongoize(object) ⇒ Object
76 77 78 79 80 81 82 83 |
# File 'lib/mongoid_geospatial/fields/point.rb', line 76 def mongoize(object) case object when Point then object.mongoize when Array then Geospatial.from_array(object) when Hash then Geospatial.from_hash(object) else object.mongoize end end |
Instance Method Details
#[](args) ⇒ Object
20 21 22 |
# File 'lib/mongoid_geospatial/fields/point.rb', line 20 def [](args) mongoize[args] end |
#each {|x| ... } ⇒ Object
24 25 26 27 |
# File 'lib/mongoid_geospatial/fields/point.rb', line 24 def each yield x yield y end |
#geo_distance(other) ⇒ Object
14 15 16 |
# File 'lib/mongoid_geospatial/wrappers/georuby.rb', line 14 def geo_distance other to_geo.spherical_distance(other.to_geo) end |
#mongoize ⇒ Object
Object -> Database
14 15 16 |
# File 'lib/mongoid_geospatial/fields/point.rb', line 14 def mongoize [x, y] end |
#radius(r = 1) ⇒ Object
38 39 40 |
# File 'lib/mongoid_geospatial/fields/point.rb', line 38 def radius r = 1 [mongoize, r] end |
#radius_sphere(r = 1, unit = :km) ⇒ Object
42 43 44 |
# File 'lib/mongoid_geospatial/fields/point.rb', line 42 def radius_sphere r = 1, unit = :km radius r.to_f/Mongoid::Geospatial.earth_radius[unit] end |
#rgeo_distance(other) ⇒ Object
12 13 14 |
# File 'lib/mongoid_geospatial/wrappers/rgeo.rb', line 12 def rgeo_distance other to_rgeo.distance other.to_rgeo end |
#to_a ⇒ Object
Object -> Database
17 18 19 |
# File 'lib/mongoid_geospatial/fields/point.rb', line 17 def mongoize [x, y] end |
#to_geo ⇒ Object
10 11 12 |
# File 'lib/mongoid_geospatial/wrappers/georuby.rb', line 10 def to_geo GeoRuby::SimpleFeatures::Point.xy(x, y) end |
#to_hsh(xl = :x, yl = :y) ⇒ Object Also known as: to_hash
33 34 35 |
# File 'lib/mongoid_geospatial/fields/point.rb', line 33 def to_hsh xl = :x, yl = :y {xl => x, yl => y} end |
#to_rgeo ⇒ Object
8 9 10 |
# File 'lib/mongoid_geospatial/wrappers/rgeo.rb', line 8 def to_rgeo RGeo::Geographic.spherical_factory.point x, y end |
#to_s ⇒ Object
29 30 31 |
# File 'lib/mongoid_geospatial/fields/point.rb', line 29 def to_s "#{x}, #{y}" end |
#to_xy ⇒ Object
Object -> Database
18 19 20 |
# File 'lib/mongoid_geospatial/fields/point.rb', line 18 def mongoize [x, y] end |