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.
-
#geocoder ⇒ Object
Returns the value of attribute geocoder.
-
#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.
-
#premise ⇒ Object
Returns the value of attribute premise.
-
#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
-
#coordinates ⇒ Object
Returns an Array with latitude and longitude.
-
#distance_to(destination, options = {}) ⇒ Object
Calculate the distance to another location.
- #eql?(other) ⇒ Boolean
- #hash ⇒ Object
-
#initialize(attrs = {}) ⇒ Location
constructor
A new instance of Location.
- #normalized_country ⇒ Object
- #normalized_locality ⇒ Object
- #normalized_premise ⇒ Object
- #normalized_region ⇒ Object
- #normalized_street ⇒ Object
- #postal_code_base ⇒ Object
- #to_s(options = {}) ⇒ Object
Constructor Details
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 |
#geocoder ⇒ Object
Returns the value of attribute geocoder.
5 6 7 |
# File 'lib/graticule/location.rb', line 5 def geocoder @geocoder 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 |
#premise ⇒ Object
Returns the value of attribute premise.
5 6 7 |
# File 'lib/graticule/location.rb', line 5 def premise @premise 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
62 63 64 |
# File 'lib/graticule/location.rb', line 62 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?
82 83 84 |
# File 'lib/graticule/location.rb', line 82 def antipode Location.new :latitude => -latitude, :longitude => longitude + (longitude >= 0 ? -180 : 180) end |
#attributes ⇒ Object
17 18 19 20 21 22 |
# File 'lib/graticule/location.rb', line 17 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 |
#coordinates ⇒ Object
Returns an Array with latitude and longitude.
58 59 60 |
# File 'lib/graticule/location.rb', line 58 def coordinates [latitude, longitude] end |
#distance_to(destination, options = {}) ⇒ Object
Calculate the distance to another location. See the various Distance formulas for more information
76 77 78 79 |
# File 'lib/graticule/location.rb', line 76 def distance_to(destination, = {}) = {:formula => :haversine, :units => :miles}.merge() "Graticule::Distance::#{[:formula].to_s.titleize}".constantize.distance(self, destination, [:units]) end |
#eql?(other) ⇒ Boolean
66 67 68 |
# File 'lib/graticule/location.rb', line 66 def eql?(other) self == other end |
#hash ⇒ Object
70 71 72 |
# File 'lib/graticule/location.rb', line 70 def hash attributes.to_s.hash end |
#normalized_country ⇒ Object
24 25 26 |
# File 'lib/graticule/location.rb', line 24 def normalized_country normalize_string(country) end |
#normalized_locality ⇒ Object
32 33 34 |
# File 'lib/graticule/location.rb', line 32 def normalized_locality normalize_string(normalize_abbreviations(locality)) end |
#normalized_premise ⇒ Object
40 41 42 |
# File 'lib/graticule/location.rb', line 40 def normalized_premise normalize_string(normalize_abbreviations(premise)) end |
#normalized_region ⇒ Object
28 29 30 |
# File 'lib/graticule/location.rb', line 28 def normalized_region normalize_string(region) end |
#normalized_street ⇒ Object
36 37 38 |
# File 'lib/graticule/location.rb', line 36 def normalized_street normalize_string(normalize_abbreviations(street)) end |
#postal_code_base ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/graticule/location.rb', line 44 def postal_code_base base = case country when "CA" postal_code.to_s[0..2] else postal_code.to_s[0..4] end base.strip! base end |
#to_s(options = {}) ⇒ Object
87 88 89 90 91 92 93 94 95 |
# File 'lib/graticule/location.rb', line 87 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 |