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
Overview
Point
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.
-
.from_array(ary) ⇒ Object
Also makes life easier: [] -> nil.
-
.from_hash(hsh) ⇒ Object
Throw error on wrong hash, just for a change.
- .from_hash_x(hsh) ⇒ Object
- .from_hash_y(hsh) ⇒ Object
-
.from_string(str) ⇒ Object
Makes life easier: “” -> nil.
- .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 Let’s store NilClass if we are invalid.
- #radius(r = 1) ⇒ Object
- #radius_sphere(r = 1, unit = :km) ⇒ Object
- #rgeo_distance(other) ⇒ Object
-
#to_a ⇒ Object
Object -> Database Let’s store NilClass if we are invalid.
- #to_geo ⇒ Object
- #to_hsh(xl = :x, yl = :y) ⇒ Object (also: #to_hash)
- #to_rgeo ⇒ Object
- #to_s ⇒ Object
-
#to_xy ⇒ Object
Object -> Database Let’s store NilClass if we are invalid.
- #valid? ⇒ Boolean
Constructor Details
#initialize(x = nil, y = nil) ⇒ Point
Returns a new instance of Point.
9 10 11 12 |
# File 'lib/mongoid_geospatial/fields/point.rb', line 9 def initialize(x = nil, y = nil) return unless x && y @x, @y = x, y end |
Instance Attribute Details
#x ⇒ Object (readonly)
Returns the value of attribute x.
7 8 9 |
# File 'lib/mongoid_geospatial/fields/point.rb', line 7 def x @x end |
#y ⇒ Object (readonly)
Returns the value of attribute y.
7 8 9 |
# File 'lib/mongoid_geospatial/fields/point.rb', line 7 def y @y end |
Class Method Details
.demongoize(object) ⇒ Object
Database -> Object
119 120 121 |
# File 'lib/mongoid_geospatial/fields/point.rb', line 119 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.
138 139 140 |
# File 'lib/mongoid_geospatial/fields/point.rb', line 138 def evolve(object) object.respond_to?(:x) ? object.mongoize : object end |
.from_array(ary) ⇒ Object
Also makes life easier:
-
-> nil
92 93 94 95 |
# File 'lib/mongoid_geospatial/fields/point.rb', line 92 def from_array(ary) return nil if ary.empty? ary[0..1].map(&:to_f) end |
.from_hash(hsh) ⇒ Object
Throw error on wrong hash, just for a change.
98 99 100 101 |
# File 'lib/mongoid_geospatial/fields/point.rb', line 98 def from_hash(hsh) fail "Hash must have at least 2 items" if hsh.size < 2 [from_hash_x(hsh), from_hash_y(hsh)] end |
.from_hash_x(hsh) ⇒ Object
111 112 113 114 115 116 |
# File 'lib/mongoid_geospatial/fields/point.rb', line 111 def from_hash_x(hsh) v = (Mongoid::Geospatial.lng_symbols & hsh.keys).first return hsh[v].to_f if !v.nil? && hsh[v] fail "Hash cannot contain #{Mongoid::Geospatial.lat_symbols.inspect} as the first item if there is no #{Mongoid::Geospatial.lng_symbols.inspect}" if Mongoid::Geospatial.lat_symbols.index(keys[0]) values[0].to_f end |
.from_hash_y(hsh) ⇒ Object
103 104 105 106 107 108 109 |
# File 'lib/mongoid_geospatial/fields/point.rb', line 103 def from_hash_y(hsh) v = (Mongoid::Geospatial.lat_symbols & hsh.keys).first return hsh[v].to_f if !v.nil? && hsh[v] fail "Hash must contain #{Mongoid::Geospatial.lat_symbols.inspect} if Ruby version is less than 1.9" if RUBY_VERSION.to_f < 1.9 fail "Hash cannot contain #{Mongoid::Geospatial.lng_symbols.inspect} as the second item if there is no #{Mongoid::Geospatial.lat_symbols.inspect}" if Mongoid::Geospatial.lng_symbols.index(hsh.keys[1]) hsh.values[1].to_f end |
.from_string(str) ⇒ Object
Makes life easier: “” -> nil
85 86 87 88 |
# File 'lib/mongoid_geospatial/fields/point.rb', line 85 def from_string(str) return nil if str.empty? str.split(/,|\s/).reject(&:empty?).map(&:to_f) end |
.mongoize(object) ⇒ Object
123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/mongoid_geospatial/fields/point.rb', line 123 def mongoize(object) case object when Point then object.mongoize when String then from_string(object) when Array then from_array(object) when Hash then from_hash(object) when NilClass then nil else return object.to_xy if object.respond_to?(:to_xy) fail 'Invalid Point' end end |
Instance Method Details
#[](args) ⇒ Object
23 24 25 |
# File 'lib/mongoid_geospatial/fields/point.rb', line 23 def [](args) mongoize[args] end |
#each {|x| ... } ⇒ Object
27 28 29 30 |
# File 'lib/mongoid_geospatial/fields/point.rb', line 27 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 Let’s store NilClass if we are invalid.
16 17 18 19 |
# File 'lib/mongoid_geospatial/fields/point.rb', line 16 def mongoize return nil unless x && y [x, y] end |
#radius(r = 1) ⇒ Object
41 42 43 |
# File 'lib/mongoid_geospatial/fields/point.rb', line 41 def radius(r = 1) [mongoize, r] end |
#radius_sphere(r = 1, unit = :km) ⇒ Object
45 46 47 |
# File 'lib/mongoid_geospatial/fields/point.rb', line 45 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 Let’s store NilClass if we are invalid.
20 21 22 23 |
# File 'lib/mongoid_geospatial/fields/point.rb', line 20 def mongoize return nil unless x && y [x, y] end |
#to_geo ⇒ Object
9 10 11 12 |
# File 'lib/mongoid_geospatial/wrappers/georuby.rb', line 9 def to_geo return unless valid? GeoRuby::SimpleFeatures::Point.xy(x, y) end |
#to_hsh(xl = :x, yl = :y) ⇒ Object Also known as: to_hash
36 37 38 |
# File 'lib/mongoid_geospatial/fields/point.rb', line 36 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
32 33 34 |
# File 'lib/mongoid_geospatial/fields/point.rb', line 32 def to_s "#{x}, #{y}" end |
#to_xy ⇒ Object
Object -> Database Let’s store NilClass if we are invalid.
21 22 23 24 |
# File 'lib/mongoid_geospatial/fields/point.rb', line 21 def mongoize return nil unless x && y [x, y] end |
#valid? ⇒ Boolean
49 50 51 |
# File 'lib/mongoid_geospatial/fields/point.rb', line 49 def valid? x && y && x.is_a?(Numeric) && y.is_a?(Numeric) end |