Skip to content

Instantly share code, notes, and snippets.

@kopylovvlad
Created May 14, 2017 09:03
Show Gist options
  • Save kopylovvlad/9120a5b580bf44dda39c506c1ca7e5fa to your computer and use it in GitHub Desktop.
Save kopylovvlad/9120a5b580bf44dda39c506c1ca7e5fa to your computer and use it in GitHub Desktop.
def GeoCoordinate(*args)
case args.first
when String
GeoCoordinate.new(*args[0].to_i, *args[1].to_i)
when Array
GeoCoordinate.new(*args[0][0], *args[1][0])
when Hash
GeoCoordinate.new(*args[0][:latitude], *args[0][:longitude])
when ->(arg) { %w(Float Fixnum).include?(arg.class.to_s) }
GeoCoordinate.new(*args)
when ->(arg) { arg.respond_to?(:to_geo_coordinate) }
args.first.to_geo_coordinate
when ->(arg) { arg.is_a?(Latitude) }
GeoCoordinate.new(*args[0].to_i, *args[1].to_i)
else
raise TypeError, "Cannot convert #{args.inspect} to GeoCoordinate"
end
end
GeoCoordinate(55.45, 37.37)
GeoCoordinate('55.45', '37.37')
GeoCoordinate(['55.45'], ['37.37'])
GeoCoordinate(MagicCoordinate.new)
GeoCoordinate(Latitude.new('55.45'), Longitude.new('37.37'))
GeoCoordinate(latitude: '55.45', longitude: '37.37')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment