Class: Array

Inherits:
Object
  • Object
show all
Defined in:
lib/geokit-rails3/core_extensions.rb

Overview

Extend Array with a sort_by_distance method.

Instance Method Summary collapse

Instance Method Details

#sort_by_distance_from(origin, opts = {}) ⇒ Object

This method creates a “distance” attribute on each object, calculates the distance from the passed origin, and finally sorts the array by the resulting distance.



6
7
8
9
10
11
12
13
# File 'lib/geokit-rails3/core_extensions.rb', line 6

def sort_by_distance_from(origin, opts={})
  distance_attribute_name = opts.delete(:distance_attribute_name) || 'distance'
  self.each do |e|
    e.class.send(:attr_accessor, distance_attribute_name) if !e.respond_to?("#{distance_attribute_name}=")
    e.send("#{distance_attribute_name}=", e.distance_to(origin,opts))
  end
  self.sort!{|a,b|a.send(distance_attribute_name) <=> b.send(distance_attribute_name)}
end