Extend Array with a sort_by_distance method.

Public Instance Methods

sort_by_distance_from(origin, opts={}) click to toggle source

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.

     # File /Users/andre/projects/rails/geokit_dev/vendor/plugins/geokit/lib/geokit-rails/acts_as_mappable.rb, line 448
448:   def sort_by_distance_from(origin, opts={})
449:     distance_attribute_name = opts.delete(:distance_attribute_name) || 'distance'
450:     self.each do |e|
451:       e.class.send(:attr_accessor, distance_attribute_name) if !e.respond_to?("#{distance_attribute_name}=")
452:       e.send("#{distance_attribute_name}=", e.distance_to(origin,opts))
453:     end
454:     self.sort!{|a,b|a.send(distance_attribute_name) <=> b.send(distance_attribute_name)}
455:   end

Disabled; run with --debug to generate this.


Generated with the Darkfish Rdoc Generator 1.1.6.