Class: TimePieces::TrafficCop

Inherits:
Object
  • Object
show all
Defined in:
lib/time_pieces/traffic_cop.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(time_durations) ⇒ TrafficCop

Returns a new instance of TrafficCop.



4
5
6
# File 'lib/time_pieces/traffic_cop.rb', line 4

def initialize(time_durations)
  @time_durations = time_durations
end

Instance Attribute Details

#time_durationsObject

Returns the value of attribute time_durations.



3
4
5
# File 'lib/time_pieces/traffic_cop.rb', line 3

def time_durations
  @time_durations
end

Instance Method Details

#add_time_durations(time_durations) ⇒ Object



7
8
9
# File 'lib/time_pieces/traffic_cop.rb', line 7

def add_time_durations(time_durations)
  @time_durations += time_durations
end

#add_time_set(time_set) ⇒ Object



10
11
12
# File 'lib/time_pieces/traffic_cop.rb', line 10

def add_time_set(time_set)
  @time_durations += time_set.time_durations
end

#assign_lane_counts!Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/time_pieces/traffic_cop.rb', line 45

def assign_lane_counts!
  lanes = assign_lane_numbers!
  lanes.reverse.each_with_index do |lane, lane_number_reversed|
    real_lane = lanes.count - lane_number_reversed
    other_lane_recs = []
    lanes.each_with_index do |oth_lane, oth_lane_index|
      break if oth_lane_index >= real_lane
      other_lane_recs += oth_lane
    end
    lane.each do |rec|
      rec.lane_count ||= real_lane
      other_lane_recs.each do |oth_rec|
        if rec.overlaps?(oth_rec)
          oth_rec.lane_count = rec.lane_count if (oth_rec.lane_count.nil?) || (oth_rec.lane_count < rec.lane_count)
        end
      end
    end
  end

end

#assign_lane_numbers!Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/time_pieces/traffic_cop.rb', line 18

def assign_lane_numbers!
  lanes = [[], [], [], [], [], [], [], [], [], [], [], [], [], []]
  time_durations.sort_by(&:start_at_seconds).each do |td|
    lanes.each_with_index do |lane, lane_number|
      if lane.count > 0
        if lane.last.overlaps?(td)
          next
        else
          lane << td
          break
        end
      else
        lane << td
        break
      end

    end
  end
  lanes.each_with_index do |lane, lane_number|
    lane.each do |td|
      td.lane_number = lane_number + 1
      puts td.inspect
      puts td.lane_number
    end
  end
  return lanes
end

#calculate!Object



13
14
15
16
17
# File 'lib/time_pieces/traffic_cop.rb', line 13

def calculate!
  #This handles lane numbers also
  assign_lane_counts!
  
end