I have a Module named Coordinated that provides distance related methods to any model that implements the latitude and longitude methods.
module Coordinated
...
def crow_flies_to(place)
raise ArgumentError, "place does not implement latitude and longitude" unless is_coordinated?(place)
Math.sqrt((latitude - place.latitude).abs**2 + (longitude - place.longitude).abs**2)
end
def is_coordinated?(place)
place.respond_to?(:latitude) && place.respond_to?(:longitude)
end
...
end
I’d like to test that classes into which Coordinated is included implement the required methods when the class is loaded. How do I do that?
You can use the
includedhook that runs when a module is included:This has the drawback that the inclusion must happen after the methods are defined, since they must already exist when the hook runs.