Class: Graticule::Location
- Inherits:
-
Object
- Object
- Graticule::Location
- Defined in:
- lib/graticule/location.rb
Overview
A geographic location
Instance Attribute Summary collapse
-
#country ⇒ Object
Returns the value of attribute country.
-
#latitude ⇒ Object
Returns the value of attribute latitude.
-
#locality ⇒ Object
(also: #city)
Returns the value of attribute locality.
-
#longitude ⇒ Object
Returns the value of attribute longitude.
-
#postal_code ⇒ Object
(also: #zip)
Returns the value of attribute postal_code.
-
#precision ⇒ Object
Returns the value of attribute precision.
-
#region ⇒ Object
(also: #state)
Returns the value of attribute region.
-
#street ⇒ Object
Returns the value of attribute street.
-
#warning ⇒ Object
Returns the value of attribute warning.
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#antipode ⇒ Object
(also: #antipodal_location)
Where would I be if I dug through the center of the earth?.
- #attributes ⇒ Object
- #blank? ⇒ Boolean
-
#coordinates ⇒ Object
Returns an Array with latitude and longitude.
-
#distance_to(destination, options = {}) ⇒ Object
Calculate the distance to another location.
-
#initialize(attrs = {}) ⇒ Location
constructor
if latitude is string both latitude and longitude are converted to float.
- #to_s(options = {}) ⇒ Object
Constructor Details
#initialize(attrs = {}) ⇒ Location
if latitude is string both latitude and longitude are converted to float
11 12 13 14 15 16 17 |
# File 'lib/graticule/location.rb', line 11 def initialize(attrs = {}) attrs = parse_strings(attrs) if attrs[:latitude].is_a? String attrs.each do |key,value| instance_variable_set "@#{key}", value end self.precision ||= :unknown end |
Instance Attribute Details
#country ⇒ Object
Returns the value of attribute country.
5 6 7 |
# File 'lib/graticule/location.rb', line 5 def country @country end |
#latitude ⇒ Object
Returns the value of attribute latitude.
5 6 7 |
# File 'lib/graticule/location.rb', line 5 def latitude @latitude end |
#locality ⇒ Object Also known as: city
Returns the value of attribute locality.
5 6 7 |
# File 'lib/graticule/location.rb', line 5 def locality @locality end |
#longitude ⇒ Object
Returns the value of attribute longitude.
5 6 7 |
# File 'lib/graticule/location.rb', line 5 def longitude @longitude end |
#postal_code ⇒ Object Also known as: zip
Returns the value of attribute postal_code.
5 6 7 |
# File 'lib/graticule/location.rb', line 5 def postal_code @postal_code end |
#precision ⇒ Object
Returns the value of attribute precision.
5 6 7 |
# File 'lib/graticule/location.rb', line 5 def precision @precision end |
#region ⇒ Object Also known as: state
Returns the value of attribute region.
5 6 7 |
# File 'lib/graticule/location.rb', line 5 def region @region end |
#street ⇒ Object
Returns the value of attribute street.
5 6 7 |
# File 'lib/graticule/location.rb', line 5 def street @street end |
#warning ⇒ Object
Returns the value of attribute warning.
5 6 7 |
# File 'lib/graticule/location.rb', line 5 def warning @warning end |
Instance Method Details
#==(other) ⇒ Object
35 36 37 |
# File 'lib/graticule/location.rb', line 35 def ==(other) other.respond_to?(:attributes) ? attributes == other.attributes : false end |
#antipode ⇒ Object Also known as: antipodal_location
Where would I be if I dug through the center of the earth?
47 48 49 |
# File 'lib/graticule/location.rb', line 47 def antipode Location.new :latitude => -latitude, :longitude => longitude + (longitude >= 0 ? -180 : 180) end |
#attributes ⇒ Object
19 20 21 22 23 24 |
# File 'lib/graticule/location.rb', line 19 def attributes [:latitude, :longitude, :street, :locality, :region, :postal_code, :country, :precision].inject({}) do |result,attr| result[attr] = self.send(attr) unless self.send(attr).blank? result end end |
#blank? ⇒ Boolean
26 27 28 |
# File 'lib/graticule/location.rb', line 26 def blank? attributes.except(:precision).empty? end |
#coordinates ⇒ Object
Returns an Array with latitude and longitude.
31 32 33 |
# File 'lib/graticule/location.rb', line 31 def coordinates [latitude, longitude] end |
#distance_to(destination, options = {}) ⇒ Object
Calculate the distance to another location. See the various Distance formulas for more information
41 42 43 44 |
# File 'lib/graticule/location.rb', line 41 def distance_to(destination, = {}) = {:formula => :haversine, :units => :miles}.merge() "Graticule::Distance::#{[:formula].to_s.titleize}".constantize.distance(self, destination, [:units]) end |
#to_s(options = {}) ⇒ Object
52 53 54 55 56 57 58 59 60 |
# File 'lib/graticule/location.rb', line 52 def to_s( = {}) = {:coordinates => false, :country => true}.merge() result = "" result << "#{street}\n" if street result << [locality, [region, postal_code].compact.join(" ")].compact.join(", ") result << " #{country}" if [:country] && country result << "\nlatitude: #{latitude}, longitude: #{longitude}" if [:coordinates] && [latitude, longitude].any? result end |