Skip to content

Instantly share code, notes, and snippets.

@joshuacronemeyer
Created April 9, 2014 16:29
Show Gist options
  • Save joshuacronemeyer/10289176 to your computer and use it in GitHub Desktop.
Save joshuacronemeyer/10289176 to your computer and use it in GitHub Desktop.
Add geocoder methods to a poro
# This code creates an object that is sortable
# by it's distance to a particular event
# the event and friend code isn't included
# but both objects have latitude and longitude accessors
# you need the geocoder gem in your gemfile.
class FriendEvent
require 'geocoder/stores/base'
include Comparable
include Geocoder::Store::Base
attr_reader :event, :friend
def initialize(event, friend)
@event = event
@friend = friend
end
def latitude
@friend.lat
end
def longitude
@friend.long
end
def proximity
return Float::INFINITY if latitude.nil? || longitude.nil?
self.distance_to(@event.venue) #here is the method that i'm using from geocoder
end
def <=>(anOther)
return 0 if @event.venue.latitude.nil? || @event.venue.longitude.nil?
proximity <=> anOther.proximity
end
def self.geocoder_options
#needed by the geocoder gem methods
#poro's not really supported well...
{ units: :km,
latitude: :latitude,
longitude: :longitude
}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment