lib/mongoid_geospatial/fields/point.rb in mongoid_geospatial-2.0.0 vs lib/mongoid_geospatial/fields/point.rb in mongoid_geospatial-2.2.0

- old
+ new

@@ -1,62 +1,47 @@ module Mongoid module Geospatial class Point + attr_accessor :x, :y + def initialize(x, y) + @x, @y = x, y + end + + # Object -> Database def mongoize [x, y] end + alias :to_a :mongoize + alias :to_xy :mongoize + def [](args) + mongoize[args] + end + class << self + # Database -> Object def demongoize(object) - return unless object && !object.empty? - RGeo::Geographic.spherical_factory.point *object - #["x"], object["y"] + # return unless object && !object.empty? + Point.new(object[0], object[1]) end def mongoize(object) - #return new.mongoize if object.respond_to?(:x) case object when Point then object.mongoize - when Hash then [object[:x], object[:y]] + when Array then object.to_xy + when Hash then object.to_xy else object end end - # Converts the object that was supplied to a criteria and converts it + # Converts the object that was supplied to a criteria # into a database friendly form. def evolve(object) object.respond_to?(:x) ? object.mongoize : object end end - -# - self.spacial_fields ||= [] -# - self.spacial_fields << field.name.to_sym if self.spacial_fields.kind_of? Array -# - -# - define_method "distance_from_#{field.name}" do |*args| -# - self.distance_from(field.name, *args) -# - end -# - -# - define_method field.name do -# - output = self[field.name] || [nil,nil] -# - output = {lng_meth => output[0], lat_meth => output[1]} unless options[:return_array] -# - return options[:class].new(output) if options[:class] -# - output -# - end -# - -# - define_method "#{field.name}=" do |arg| -# - if arg.kind_of?(Hash) && arg[lng_meth] && arg[lat_meth] -# - arg = [arg[lng_meth].to_f, arg[lat_meth].to_f] -# - elsif arg.respond_to?(:to_xy) -# - arg = arg.to_xy -# - end -# - self[field.name]=arg -# - arg = [nil,nil] if arg.nil? -# - return arg[0..1] if options[:return_array] -# - h = {lng_meth => arg[0], lat_meth => arg[1]} -# - return h if options[:class].blank? -# - options[:class].new(h) end end end