Class: Hillpace::Route
- Inherits:
-
Object
- Object
- Hillpace::Route
- Defined in:
- lib/hillpace/route.rb
Overview
Represents a geographic route in the Earth, made out of consecutive segments.
Instance Attribute Summary collapse
-
#segments ⇒ Object
readonly
Returns the value of attribute segments.
Instance Method Summary collapse
-
#climb ⇒ Number
Measures the elevation difference from the start to the end of the route, in meters.
-
#distance_meters ⇒ Number
Measures the distance of the route, in meters.
-
#duration ⇒ Number
Measures the duration of the route, in seconds.
-
#incline ⇒ Number
Measures the climb of the route relative to its distance.
-
#initialize(segments) ⇒ Route
constructor
Initializes a Route object.
-
#pace ⇒ Pace
Measures the pace of the route.
-
#split(split_distances) ⇒ Route
Splits the segments in the route in the distances indicated.
-
#total_downhills ⇒ Number
Measures the sum of downhills between segments of the route.
-
#total_uphills ⇒ Number
Measures the sum of uphills between segments of the route.
Constructor Details
#initialize(segments) ⇒ Route
Initializes a Route object.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/hillpace/route.rb', line 11 def initialize(segments) raise 'Invalid segment array to initialize Route' unless segments.respond_to?('each') && segments.all? {|segment| segment.is_a? Segment} unless segments.empty? last_track_point = segments.first.track_points.first segments.each do |segment| raise 'Segments must be consecutive' unless last_track_point == segment.track_points.first last_track_point = segment.track_points.last end end @segments = segments end |
Instance Attribute Details
#segments ⇒ Object (readonly)
Returns the value of attribute segments.
4 5 6 |
# File 'lib/hillpace/route.rb', line 4 def segments @segments end |
Instance Method Details
#climb ⇒ Number
Measures the elevation difference from the start to the end of the route, in meters.
34 35 36 |
# File 'lib/hillpace/route.rb', line 34 def climb segments.inject(0) {|result, segment| result + segment.climb} end |
#distance_meters ⇒ Number
Measures the distance of the route, in meters.
28 29 30 |
# File 'lib/hillpace/route.rb', line 28 def distance_meters segments.inject(0) {|result, segment| result + segment.distance_meters} end |
#duration ⇒ Number
Measures the duration of the route, in seconds.
58 59 60 |
# File 'lib/hillpace/route.rb', line 58 def duration segments.inject(0) {|result, segment| result + segment.duration} end |
#incline ⇒ Number
Measures the climb of the route relative to its distance.
40 41 42 |
# File 'lib/hillpace/route.rb', line 40 def incline self.climb / self.distance_meters end |
#pace ⇒ Pace
Measures the pace of the route.
64 65 66 |
# File 'lib/hillpace/route.rb', line 64 def pace Pace.from_meters_per_second(distance_meters / duration) end |
#split(split_distances) ⇒ Route
Splits the segments in the route in the distances indicated.
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/hillpace/route.rb', line 71 def split(split_distances) split_distances_enumerator = split_distances.sort.each next_split_distance = split_distances_enumerator.next accumulated_distance = 0 result = [] segments.lazy.each do |segment| while next_split_distance < accumulated_distance + segment.distance_meters subsegments = segment.split (next_split_distance - accumulated_distance) result << subsegments.first accumulated_distance += subsegments.first.distance_meters if subsegments.length > 1 segment = subsegments.last next_split_distance = split_distances_enumerator.next rescue Float::INFINITY end end result << segment.clone accumulated_distance += segment.distance_meters end Route.new result end |
#total_downhills ⇒ Number
Measures the sum of downhills between segments of the route.
52 53 54 |
# File 'lib/hillpace/route.rb', line 52 def total_downhills segments.inject(0) {|result, segment| result + segment.total_downhills} end |
#total_uphills ⇒ Number
Measures the sum of uphills between segments of the route.
46 47 48 |
# File 'lib/hillpace/route.rb', line 46 def total_uphills segments.inject(0) {|result, segment| result + segment.total_uphills} end |