Class: Loc::LocationCollection

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/loc/location_collection.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#locationsObject (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

#distanceObject

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

Returns:

  • (Boolean)


55
56
57
# File 'lib/loc/location_collection.rb', line 55

def eql?(other)
  to_a.eql?(other.to_a)
end

#hashObject



59
60
61
# File 'lib/loc/location_collection.rb', line 59

def hash
  to_a.hash
end

#inspectObject



67
68
69
# File 'lib/loc/location_collection.rb', line 67

def inspect
  "<#{self.class} #{{ locations: @locations }}>"
end

#popObject



47
48
49
# File 'lib/loc/location_collection.rb', line 47

def pop
  locations.pop
end

#shiftObject



43
44
45
# File 'lib/loc/location_collection.rb', line 43

def shift
  locations.shift
end

#sizeObject



31
32
33
# File 'lib/loc/location_collection.rb', line 31

def size
  locations.size
end

#to_arrayObject Also known as: to_a



63
64
65
# File 'lib/loc/location_collection.rb', line 63

def to_array
  locations.map(&:to_a)
end