lib/hungry/geolocation.rb in hungry-0.0.1 vs lib/hungry/geolocation.rb in hungry-0.1.0

- old
+ new

@@ -25,12 +25,12 @@ elsif input.respond_to?(:match) # Example: "50.8469397,5.6927505" # # input is a String, so we can use a regular expression to extract # latitude and longitude: - if match = input.match(/^(?<latitude>[0-9\.]+),\s?(?<longitude>[0-9\.]+)$/) - coordinates = [match[:latitude], match[:longitude]] + if match = input.match(/^([0-9\.]+),\s?([0-9\.]+)$/) + coordinates = [match[1], match[2]] end elsif input.respond_to?(:keys) # Example: { latitude: 50.8469397, longitude: 5.6927505 } # @@ -56,8 +56,28 @@ self.longitude = longitude.to_f end def to_s [latitude, longitude].join(',') + end + + def [](key) + to_h.send(:[], key) + end + + def []=(key, value) + case key.to_sym + when :latitude + self.latitude = value.to_f + when :longitude + self.longitude = value.to_f + end + end + + def to_h + { + latitude: latitude, + longitude: longitude + } end end end