diff options
| author | Teddy Wing | 2017-05-24 10:49:49 +0200 | 
|---|---|---|
| committer | Robert | 2017-05-29 08:49:52 +0200 | 
| commit | 31c4acfa6a3f9893e99805c5fc1d03fd3392d778 (patch) | |
| tree | 7f0238ff3c48d65cd5d467a83ef16546e3c49943 | |
| parent | c8452bfcedf158abf5c10a0ca8d1449650710eca (diff) | |
| download | chouette-core-31c4acfa6a3f9893e99805c5fc1d03fd3392d778.tar.bz2 | |
Create `TimeDuration` module for `#exceeds_gap?` method
Put the `exceeds_gap?` method in a new module called `TimeDuration`.
This will allow us to use it from both the
`Chouette::VehicleJourneyAtStop` class and from the
`IncreasingTimeOrderValidator`. Doing this also allows us to test it
independently.
Haven't yet rewritten the increasing times validator to take advantage
of this new method, but adding it now to ensure we don't get an error
calling a non-existent method in `VehicleJourneyAtStop`.
I changed the signature of the method to take a duration as its first
argument. This allows us to set arbitrary durations to compare against
and surfaces the duration when reading code where the  method is called.
Not sure if that's the best approach here, but it seems to make sense
for now.
Refs #870
| -rw-r--r-- | app/models/chouette/vehicle_journey_at_stop.rb | 2 | ||||
| -rw-r--r-- | lib/time_duration.rb | 5 | 
2 files changed, 6 insertions, 1 deletions
| diff --git a/app/models/chouette/vehicle_journey_at_stop.rb b/app/models/chouette/vehicle_journey_at_stop.rb index cd515be7f..ee35b065b 100644 --- a/app/models/chouette/vehicle_journey_at_stop.rb +++ b/app/models/chouette/vehicle_journey_at_stop.rb @@ -16,7 +16,7 @@ module Chouette        # security against nil values        return unless arrival_time && departure_time -      if exceeds_gap?( arrival_time, departure_time) +      if TimeDuration.exceeds_gap?(4.hours, arrival_time, departure_time)          errors.add(:arrival_time,I18n.t("activerecord.errors.models.vehicle_journey_at_stop.arrival_must_be_before_departure"))        end      end diff --git a/lib/time_duration.rb b/lib/time_duration.rb new file mode 100644 index 000000000..231fff7b8 --- /dev/null +++ b/lib/time_duration.rb @@ -0,0 +1,5 @@ +module TimeDuration +  def self.exceeds_gap?(duration, earlier, later) +    (4 * 3600) < ((later - earlier) % (3600 * 24)) +  end +end | 
