Module: GeoKit::ActsAsMappable::InstanceMethods::SingletonMethods

Defined in:
lib/geo_kit/acts_as_mappable.rb

Overview

Class singleton methods to mix into ActiveRecord.

Instance Method Summary collapse

Instance Method Details

#count(*args) ⇒ Object

Extends the existing count method by:

  • If a mappable instance exists in the options and the distance column exists in the conditions, substitutes the distance sql for the distance column – this saves having to write the gory SQL.



116
117
118
119
# File 'lib/geo_kit/acts_as_mappable.rb', line 116

def count(*args)
  prepare_for_find_or_count(:count, args)
  super(*args)
end

#count_beyond(distance, options = {}) ⇒ Object Also known as: count_outside

Counts beyond a distance radius.



166
167
168
169
# File 'lib/geo_kit/acts_as_mappable.rb', line 166

def count_beyond(distance, options={})
  options[:beyond] = distance
  count(options)
end

#count_by_range(range, options = {}) ⇒ Object

Counts according to a range. Accepts inclusive or exclusive ranges.



173
174
175
176
# File 'lib/geo_kit/acts_as_mappable.rb', line 173

def count_by_range(range, options={})
  options[:range] = range
  count(options)
end

#count_within(distance, options = {}) ⇒ Object Also known as: count_inside

counts within a distance radius.



159
160
161
162
# File 'lib/geo_kit/acts_as_mappable.rb', line 159

def count_within(distance, options={})
  options[:within] = distance
  count(options)
end

#count_within_bounds(bounds, options = {}) ⇒ Object

Finds within rectangular bounds (sw,ne).



179
180
181
182
# File 'lib/geo_kit/acts_as_mappable.rb', line 179

def count_within_bounds(bounds, options={})
  options[:bounds] = bounds
  count(options)
end

#distance_sql(origin, units = default_units, formula = default_formula) ⇒ Object

Returns the distance calculation to be used as a display column or a condition. This is provide for anyone wanting access to the raw SQL.



186
187
188
189
190
191
192
193
194
# File 'lib/geo_kit/acts_as_mappable.rb', line 186

def distance_sql(origin, units=default_units, formula=default_formula)
  case formula
  when :sphere
    sql = sphere_distance_sql(origin, units)
  when :flat
    sql = flat_distance_sql(origin, units)
  end
  sql
end

#find(*args) ⇒ Object

Extends the existing find method in potentially two ways:

  • If a mappable instance exists in the options, adds a distance column.

  • If a mappable instance exists in the options and the distance column exists in the conditions, substitutes the distance sql for the distance column – this saves having to write the gory SQL.



107
108
109
110
# File 'lib/geo_kit/acts_as_mappable.rb', line 107

def find(*args)
  prepare_for_find_or_count(:find, args)
  super(*args)
end

#find_beyond(distance, options = {}) ⇒ Object Also known as: find_outside

Finds beyond a distance radius.



129
130
131
132
# File 'lib/geo_kit/acts_as_mappable.rb', line 129

def find_beyond(distance, options={})
  options[:beyond] = distance
  find(:all, options)
end

#find_by_range(range, options = {}) ⇒ Object

Finds according to a range. Accepts inclusive or exclusive ranges.



136
137
138
139
# File 'lib/geo_kit/acts_as_mappable.rb', line 136

def find_by_range(range, options={})
  options[:range] = range
  find(:all, options)
end

#find_closest(options = {}) ⇒ Object Also known as: find_nearest

Finds the closest to the origin.



142
143
144
# File 'lib/geo_kit/acts_as_mappable.rb', line 142

def find_closest(options={})
  find(:nearest, options)
end

#find_farthest(options = {}) ⇒ Object

Finds the farthest from the origin.



148
149
150
# File 'lib/geo_kit/acts_as_mappable.rb', line 148

def find_farthest(options={})
  find(:farthest, options)
end

#find_within(distance, options = {}) ⇒ Object Also known as: find_inside

Finds within a distance radius.



122
123
124
125
# File 'lib/geo_kit/acts_as_mappable.rb', line 122

def find_within(distance, options={})
  options[:within] = distance
  find(:all, options)
end

#find_within_bounds(bounds, options = {}) ⇒ Object

Finds within rectangular bounds (sw,ne).



153
154
155
156
# File 'lib/geo_kit/acts_as_mappable.rb', line 153

def find_within_bounds(bounds, options={})
  options[:bounds] = bounds
  find(:all, options)
end