diff options
| author | Teddy Wing | 2017-05-30 23:30:48 +0200 |
|---|---|---|
| committer | Teddy Wing | 2017-05-31 17:57:36 +0200 |
| commit | 52531e2910b3db6ac2a7c5fedbc14b0be54782dc (patch) | |
| tree | e5148ef532ff5dd3b2e227861f3e50381e8d5d18 | |
| parent | d22ee707c026a71dac64da792cb22f46a1a1dbde (diff) | |
| download | chouette-core-52531e2910b3db6ac2a7c5fedbc14b0be54782dc.tar.bz2 | |
VehicleJourneyAtStopsDayOffset#calculate!: Adjust loop
Didn't like my code from before so wanted to go back over it. Ultimately
this is pretty much the same though. Still not super happy, but this
seems to hopefully be a bit better.
Get rid of my first stab at the problem (commented code), as this wasn't
even relevant when I first committed it.
Another idea I was thinking of could be this instead of the `<`
comparisons:
stop.arrival_time.earlier_than(prior_stop.departure_time)
That seems more readable than the standard comparison operator, but
wouldn't look good in terms of implementation. No monkeypatching, but
we'd have to create a new class that has an `#earlier_than` method. Not
ideal. Instead of getting fancy, just left the comparison conditions
pretty much the same as before.
The only change here to the comparisons is to make them a bit more
consistent in terms of ordering and placement.
Instead of using `#each_with_index` and a rotating "previous" variable,
use the `#inject` method to do the loop and give us the previous at-stop
to compare against. A little worried that this is less readable than the
old code, but it feels a touch nicer.
Not ideal perhaps, but good enough I think.
Refs #3596
| -rw-r--r-- | app/models/chouette/vehicle_journey_at_stops_day_offset.rb | 44 |
1 files changed, 9 insertions, 35 deletions
diff --git a/app/models/chouette/vehicle_journey_at_stops_day_offset.rb b/app/models/chouette/vehicle_journey_at_stops_day_offset.rb index 71eb46b19..9f577dded 100644 --- a/app/models/chouette/vehicle_journey_at_stops_day_offset.rb +++ b/app/models/chouette/vehicle_journey_at_stops_day_offset.rb @@ -5,52 +5,26 @@ module Chouette end def calculate! - start_date = @at_stops.first.arrival_time.to_date - offset = 0 - arrival_offset = 0 departure_offset = 0 - # @at_stops.each do |at_stop| - # if at_stop.arrival_time.to_date != start_date - # offset = at_stop.arrival_time.to_date - start_date - # end - # - # if at_stop.departure_time.to_date != start_date - # offset = at_stop.departure_time.to_date - start_date - # end - # - # at_stop.arrival_day_offset = offset - # at_stop.departure_day_offset = offset - # end - - # Check that the current time is later than the previous time. - # If earlier, it's a new day offset - # Otherwise, we keep the current day offset - - # TODO: Don't like this - previous_at_stop = @at_stops.first - - @at_stops.each_with_index do |at_stop, index| - next if index == 0 + @at_stops.inject(nil) do |prior_stop, stop| + next stop if prior_stop.nil? - if previous_at_stop.departure_time > at_stop.arrival_time || - at_stop.arrival_time < previous_at_stop.arrival_time + if stop.arrival_time < prior_stop.departure_time || + stop.arrival_time < prior_stop.arrival_time arrival_offset += 1 end - # Departure time is earlier than arrival time - # or - # Departure time is earlier than previous departure time - if at_stop.departure_time < at_stop.arrival_time || - at_stop.departure_time < previous_at_stop.departure_time + if stop.departure_time < stop.arrival_time || + stop.departure_time < prior_stop.departure_time departure_offset += 1 end - at_stop.arrival_day_offset = arrival_offset - at_stop.departure_day_offset = departure_offset + stop.arrival_day_offset = arrival_offset + stop.departure_day_offset = departure_offset - previous_at_stop = at_stop + stop end end |
