Class: Loc::LocationCollection
- Inherits:
-
Object
- Object
- Loc::LocationCollection
- Includes:
- Enumerable
- Defined in:
- lib/loc/location_collection.rb
Instance Attribute Summary collapse
-
#locations ⇒ Object
readonly
Returns the value of attribute locations.
Class Method Summary collapse
- .[](*args) ⇒ Object
- .from_array(a) ⇒ Object (also: from_a)
Instance Method Summary collapse
- #==(other) ⇒ Object
- #[](*args) ⇒ Object
-
#distance ⇒ Object
Give the distance in meters between ordered location points using the ‘Haversine’ formula.
- #each(&block) ⇒ Object
- #eql?(other) ⇒ Boolean
- #hash ⇒ Object
-
#initialize(locations = []) ⇒ LocationCollection
constructor
A new instance of LocationCollection.
- #inspect ⇒ Object
- #pop ⇒ Object
- #shift ⇒ Object
- #size ⇒ Object
- #to_array ⇒ Object (also: #to_a)
Constructor Details
#initialize(locations = []) ⇒ LocationCollection
Returns a new instance of LocationCollection.
6 7 8 |
# File 'lib/loc/location_collection.rb', line 6 def initialize(locations = []) @locations = locations.map { |o| to_location(o) } end |
Instance Attribute Details
#locations ⇒ Object (readonly)
Returns the value of attribute locations.
4 5 6 |
# File 'lib/loc/location_collection.rb', line 4 def locations @locations end |
Class Method Details
.[](*args) ⇒ Object
15 16 17 |
# File 'lib/loc/location_collection.rb', line 15 def [](*args) new(args) end |
.from_array(a) ⇒ Object Also known as: from_a
11 12 13 |
# File 'lib/loc/location_collection.rb', line 11 def from_array(a) new(a) end |
Instance Method Details
#==(other) ⇒ Object
51 52 53 |
# File 'lib/loc/location_collection.rb', line 51 def ==(other) to_a == other.to_a end |
#[](*args) ⇒ Object
35 36 37 |
# File 'lib/loc/location_collection.rb', line 35 def [](*args) locations[*args] end |
#distance ⇒ Object
Give the distance in meters between ordered location points using the ‘Haversine’ formula
24 25 26 27 28 29 |
# File 'lib/loc/location_collection.rb', line 24 def distance return nil unless @locations.size > 1 locations.each_cons(2).reduce(0) do |acc, (loc1, loc2)| acc + loc1.distance_to(loc2) end end |
#each(&block) ⇒ Object
39 40 41 |
# File 'lib/loc/location_collection.rb', line 39 def each(&block) locations.each(&block) end |
#eql?(other) ⇒ Boolean
55 56 57 |
# File 'lib/loc/location_collection.rb', line 55 def eql?(other) to_a.eql?(other.to_a) end |
#hash ⇒ Object
59 60 61 |
# File 'lib/loc/location_collection.rb', line 59 def hash to_a.hash end |
#inspect ⇒ Object
67 68 69 |
# File 'lib/loc/location_collection.rb', line 67 def inspect "<#{self.class} #{{ locations: @locations }}>" end |
#pop ⇒ Object
47 48 49 |
# File 'lib/loc/location_collection.rb', line 47 def pop locations.pop end |
#shift ⇒ Object
43 44 45 |
# File 'lib/loc/location_collection.rb', line 43 def shift locations.shift end |
#size ⇒ Object
31 32 33 |
# File 'lib/loc/location_collection.rb', line 31 def size locations.size end |
#to_array ⇒ Object Also known as: to_a
63 64 65 |
# File 'lib/loc/location_collection.rb', line 63 def to_array locations.map(&:to_a) end |