lib/mongoid/geospatial/fields/point.rb in mongoid-geospatial-5.0.0 vs lib/mongoid/geospatial/fields/point.rb in mongoid-geospatial-5.1.0

- old
+ new

@@ -16,14 +16,15 @@ # Let's store NilClass if we are invalid. # # @return (Array) def mongoize return nil unless x && y + [x, y] end - alias_method :to_a, :mongoize - alias_method :to_xy, :mongoize + alias to_a mongoize + alias to_xy mongoize def [](args) mongoize[args] end @@ -38,11 +39,11 @@ # @return [Hash] with { xl => x, yl => y } # def to_hsh(xl = :x, yl = :y) { xl => x, yl => y } end - alias_method :to_hash, :to_hsh + alias to_hash to_hsh # # Helper for [self, radius] # # @return [Array] with [self, radius] @@ -142,11 +143,12 @@ 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) - fail 'Invalid Point' + + raise 'Invalid Point' end end # Converts the object that was supplied to a criteria # into a database friendly form. @@ -169,10 +171,11 @@ # # @return (Array) # def from_string(str) return nil if str.empty? + str.split(/,|\s/).reject(&:empty?).map(&:to_f) end # # Sanitize a `Point` from an `Array` @@ -183,10 +186,11 @@ # # @return (Array) # def from_array(array) return nil if array.empty? + array.flatten[0..1].map(&:to_f) end # # Sanitize a `Point` from a `Hash` @@ -201,23 +205,26 @@ # Throws error if hash has less than 2 items. # # @return (Array) # def from_hash(hsh) - fail 'Hash must have at least 2 items' if hsh.size < 2 + raise 'Hash must have at least 2 items' if hsh.size < 2 + [from_hash_x(hsh), from_hash_y(hsh)] end def from_hash_y(hsh) - v = (Mongoid::Geospatial.lat_symbols & hsh.keys).first + v = (Mongoid::Geospatial::Config::Point.y & hsh.keys).first return hsh[v].to_f if !v.nil? && hsh[v] - fail "Hash must contain #{Mongoid::Geospatial.lat_symbols.inspect}" + + raise "Hash must contain #{Mongoid::Geospatial::Config::Point.y.inspect}" end def from_hash_x(hsh) - v = (Mongoid::Geospatial.lng_symbols & hsh.keys).first + v = (Mongoid::Geospatial::Config::Point.x & hsh.keys).first return hsh[v].to_f if !v.nil? && hsh[v] - fail "Hash must contain #{Mongoid::Geospatial.lng_symbols.inspect}" + + raise "Hash must contain #{Mongoid::Geospatial::Config::Point.x.inspect}" end end # << self end # Point end # Geospatial end # Mongoid