class Mongoid::Geospatial::Point
Attributes
Public Class Methods
Database -> Object Get it back
# File lib/mongoid/geospatial/fields/point.rb, line 132 def demongoize(obj) obj && new(*obj) end
Converts the object that was supplied to a criteria into a database friendly form.
# File lib/mongoid/geospatial/fields/point.rb, line 155 def evolve(obj) case obj when Point then obj.mongoize else obj end end
Object -> Database Send it to MongoDB
# File lib/mongoid/geospatial/fields/point.rb, line 139 def mongoize(obj) case obj when Point then obj.mongoize when String then from_string(obj) when Array then from_array(obj) when Hash then from_hash(obj) when NilClass then nil else return obj.to_xy if obj.respond_to?(:to_xy) raise 'Invalid Point' end end
# File lib/mongoid/geospatial/fields/point.rb, line 9 def initialize(x, y, z = nil) @x = x @y = y @z = z end
Private Class Methods
Sanitize a `Point` from an `Array`
Also makes life easier:
-
-> []
- 1,2
-
-> [1.0, 2.0]
@return (Array)
# File lib/mongoid/geospatial/fields/point.rb, line 189 def from_array(array) return nil if array.empty? array.flatten[0..1].map(&:to_f) end
Sanitize a `Point` from a `Hash`
Uses Mongoid::Geospatial.lat_symbols & lng_symbols
Also makes life easier: {x: 1.0, y: 2.0} -> [1.0, 2.0] {lat: 1.0, lon: 2.0} -> [1.0, 2.0] {lat: 1.0, long: 2.0} -> [1.0, 2.0]
Throws error if hash has less than 2 items.
@return (Array)
# File lib/mongoid/geospatial/fields/point.rb, line 209 def from_hash(hsh) raise 'Hash must have at least 2 items' if hsh.size < 2 [from_hash_x(hsh), from_hash_y(hsh)] end
# File lib/mongoid/geospatial/fields/point.rb, line 222 def from_hash_x(hsh) v = (Mongoid::Geospatial::Config::Point.x & hsh.keys).first return hsh[v].to_f if !v.nil? && hsh[v] raise "Hash must contain #{Mongoid::Geospatial::Config::Point.x.inspect}" end
# File lib/mongoid/geospatial/fields/point.rb, line 215 def from_hash_y(hsh) v = (Mongoid::Geospatial::Config::Point.y & hsh.keys).first return hsh[v].to_f if !v.nil? && hsh[v] raise "Hash must contain #{Mongoid::Geospatial::Config::Point.y.inspect}" end
Sanitize a `Point` from a `String`
Makes life easier: “” -> [] “1, 2” -> [1.0, 2.0] “1.1 2.2” -> [1.1, 2.2]
@return (Array)
# File lib/mongoid/geospatial/fields/point.rb, line 174 def from_string(str) return nil if str.empty? str.split(/,|\s/).reject(&:empty?).map(&:to_f) end
Public Instance Methods
# File lib/mongoid/geospatial/fields/point.rb, line 27 def [](args) mongoize[args] end
# File lib/mongoid/geospatial/fields/point.rb, line 31 def each yield x yield y end
Object -> Database Let's store NilClass if we are invalid.
@return (Array)
# File lib/mongoid/geospatial/fields/point.rb, line 19 def mongoize return nil unless x && y [x, y] end
Helper for [self, radius]
@return [Array] with [self, radius]
# File lib/mongoid/geospatial/fields/point.rb, line 51 def radius(r = 1) [mongoize, r] end
Point
representation as a Hash
@return [Hash] with { xl => x, yl => y }
# File lib/mongoid/geospatial/fields/point.rb, line 41 def to_hsh(xl = :x, yl = :y) { xl => x, yl => y } end