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
Wrapper to GeoRuby’s 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, y) ⇒ 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
delegate :distance, to: :to_geo.
- #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, y) ⇒ Point
Returns a new instance of Point.
9 10 11 |
# File 'lib/mongoid/geospatial/fields/point.rb', line 9 def initialize(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
117 118 119 |
# File 'lib/mongoid/geospatial/fields/point.rb', line 117 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.
136 137 138 |
# File 'lib/mongoid/geospatial/fields/point.rb', line 136 def evolve(object) object.respond_to?(:x) ? object.mongoize : object end |
.from_array(ary) ⇒ Object
Also makes life easier:
-
-> nil
90 91 92 93 |
# File 'lib/mongoid/geospatial/fields/point.rb', line 90 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.
96 97 98 99 |
# File 'lib/mongoid/geospatial/fields/point.rb', line 96 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
109 110 111 112 113 114 |
# File 'lib/mongoid/geospatial/fields/point.rb', line 109 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(hsh.keys[0]) hsh.values[0].to_f end |
.from_hash_y(hsh) ⇒ Object
101 102 103 104 105 106 107 |
# File 'lib/mongoid/geospatial/fields/point.rb', line 101 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
83 84 85 86 |
# File 'lib/mongoid/geospatial/fields/point.rb', line 83 def from_string(str) return nil if str.empty? str.split(/,|\s/).reject(&:empty?).map(&:to_f) end |
.mongoize(object) ⇒ Object
121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/mongoid/geospatial/fields/point.rb', line 121 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
22 23 24 |
# File 'lib/mongoid/geospatial/fields/point.rb', line 22 def [](args) mongoize[args] end |
#each {|x| ... } ⇒ Object
26 27 28 29 |
# File 'lib/mongoid/geospatial/fields/point.rb', line 26 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.
15 16 17 18 |
# File 'lib/mongoid/geospatial/fields/point.rb', line 15 def mongoize return nil unless x && y [x, y] end |
#radius(r = 1) ⇒ Object
40 41 42 |
# File 'lib/mongoid/geospatial/fields/point.rb', line 40 def radius(r = 1) [mongoize, r] end |
#radius_sphere(r = 1, unit = :km) ⇒ Object
44 45 46 |
# File 'lib/mongoid/geospatial/fields/point.rb', line 44 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.
19 20 21 22 |
# File 'lib/mongoid/geospatial/fields/point.rb', line 19 def mongoize return nil unless x && y [x, y] end |
#to_geo ⇒ Object
delegate :distance, to: :to_geo
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
35 36 37 |
# File 'lib/mongoid/geospatial/fields/point.rb', line 35 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
31 32 33 |
# File 'lib/mongoid/geospatial/fields/point.rb', line 31 def to_s "#{x}, #{y}" end |
#to_xy ⇒ 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 |
#valid? ⇒ Boolean
48 49 50 |
# File 'lib/mongoid/geospatial/fields/point.rb', line 48 def valid? x && y && x.is_a?(Numeric) && y.is_a?(Numeric) end |