Module: Geocoder::Store::MongoBase

Included in:
MongoMapper, Mongoid
Defined in:
lib/geocoder/stores/mongo_base.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included_by_model(base) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/geocoder/stores/mongo_base.rb', line 4

def self.included_by_model(base)
  base.class_eval do

    scope :geocoded, lambda {
      where(geocoder_options[:coordinates].ne => nil)
    }

    scope :not_geocoded, lambda {
      where(geocoder_options[:coordinates] => nil)
    }

    scope :near, lambda{ |location, *args|
      warn "DEPRECATION WARNING: The .near method will be removed for MongoDB-backed models in Geocoder 1.3.0. Please use MongoDB's built-in query language instead."
      coords  = Geocoder::Calculations.extract_coordinates(location)

      # no results if no lat/lon given
      return where(:id => false) unless coords.is_a?(Array)

      radius  = args.size > 0 ? args.shift : 20
      options = args.size > 0 ? args.shift : {}
      options[:units] ||= geocoder_options[:units]

      # Use BSON::OrderedHash if Ruby's hashes are unordered.
      # Conditions must be in order required by indexes (see mongo gem).
      version = RUBY_VERSION.split('.').map { |i| i.to_i }
      empty = version[0] < 2 && version[1] < 9 ? BSON::OrderedHash.new : {}

      conds = empty.clone
      field = geocoder_options[:coordinates]
      conds[field] = empty.clone
      conds[field]["$nearSphere"]  = coords.reverse

      if radius
        conds[field]["$maxDistance"] = \
          Geocoder::Calculations.distance_to_radians(radius, options[:units])
      end

      if obj = options[:exclude]
        conds[:_id.ne] = obj.id
      end
      where(conds)
    }
  end
end

Instance Method Details

#geocodeObject

Look up coordinates and assign to latitude and longitude attributes (or other as specified in geocoded_by). Returns coordinates (array).



75
76
77
78
79
80
81
82
83
84
# File 'lib/geocoder/stores/mongo_base.rb', line 75

def geocode
  do_lookup(false) do |o,rs|
    if r = rs.first
      unless r.coordinates.nil?
        o.__send__ "#{self.class.geocoder_options[:coordinates]}=", r.coordinates.reverse
      end
      r.coordinates
    end
  end
end

#nearbys(radius = 20, options = {}) ⇒ Object

Get nearby geocoded objects. Takes the same options hash as the near class method (scope). Returns nil if the object is not geocoded.



64
65
66
67
68
69
# File 'lib/geocoder/stores/mongo_base.rb', line 64

def nearbys(radius = 20, options = {})
  warn "DEPRECATION WARNING: The #nearbys method will be removed for MongoDB-backed models in Geocoder 1.3.0. Please use MongoDB's built-in query language instead."
  return nil unless geocoded?
  options.merge!(:exclude => self) unless send(self.class.primary_key).nil?
  self.class.near(self, radius, options)
end

#reverse_geocodeObject

Look up address and assign to address attribute (or other as specified in reverse_geocoded_by). Returns address (string).



90
91
92
93
94
95
96
97
98
99
# File 'lib/geocoder/stores/mongo_base.rb', line 90

def reverse_geocode
  do_lookup(true) do |o,rs|
    if r = rs.first
      unless r.address.nil?
        o.__send__ "#{self.class.geocoder_options[:fetched_address]}=", r.address
      end
      r.address
    end
  end
end

#to_coordinatesObject

Coordinates [lat,lon] of the object. This method always returns coordinates in lat,lon order, even though internally they are stored in the opposite order.



54
55
56
57
# File 'lib/geocoder/stores/mongo_base.rb', line 54

def to_coordinates
  coords = send(self.class.geocoder_options[:coordinates])
  coords.is_a?(Array) ? coords.reverse : []
end