diff options
| -rw-r--r-- | lib/stop_areas_to_way_costs_converter.rb | 21 | ||||
| -rw-r--r-- | lib/way_cost.rb | 7 | ||||
| -rw-r--r-- | spec/lib/stop_areas_to_way_costs_converter_spec.rb | 70 | 
3 files changed, 95 insertions, 3 deletions
| diff --git a/lib/stop_areas_to_way_costs_converter.rb b/lib/stop_areas_to_way_costs_converter.rb new file mode 100644 index 000000000..c6a00d31b --- /dev/null +++ b/lib/stop_areas_to_way_costs_converter.rb @@ -0,0 +1,21 @@ +class StopAreasToWayCostsConverter +  def initialize(stop_areas) +    @stop_areas = stop_areas +  end + +  def convert +    @stop_areas.each_cons(2).map do |stop_area_pair| +      WayCost.new( +        departure: Geokit::LatLng.new( +          stop_area_pair[0].latitude, +          stop_area_pair[0].longitude +        ), +        arrival: Geokit::LatLng.new( +          stop_area_pair[1].latitude, +          stop_area_pair[1].longitude +        ), +        id: "#{stop_area_pair[0].id}-#{stop_area_pair[1].id}" +      ) +    end +  end +end diff --git a/lib/way_cost.rb b/lib/way_cost.rb index 5de64177f..9f860308c 100644 --- a/lib/way_cost.rb +++ b/lib/way_cost.rb @@ -1,5 +1,5 @@  class WayCost -  attr_reader :departure, :arrival +  attr_reader :departure, :arrival, :id    attr_accessor :distance, :time    def initialize( @@ -7,7 +7,7 @@ class WayCost      arrival:,      distance: nil,      time: nil, -    id: nil  # TODO: calculate ID automatically +    id: nil    )      @departure = departure      @arrival = arrival @@ -21,6 +21,7 @@ class WayCost        @departure == other.departure &&        @arrival == other.arrival &&        @distance == other.distance && -      @time == other.time +      @time == other.time && +      @id == other.id    end  end diff --git a/spec/lib/stop_areas_to_way_costs_converter_spec.rb b/spec/lib/stop_areas_to_way_costs_converter_spec.rb new file mode 100644 index 000000000..7e09ede2e --- /dev/null +++ b/spec/lib/stop_areas_to_way_costs_converter_spec.rb @@ -0,0 +1,70 @@ +RSpec.describe StopAreasToWayCostsConverter do +  describe "#convert" do +    it "converts a StopArea collection to WayCosts" do +      route = create(:route_common) +      coordinates = [ +        [1.5, 1.8], +        [2.2, 2.1], +        [3.0, 3.6], +        [4.9, 4.3] +      ] + +      stop_areas = coordinates.map do |latlng| +        stop_area = create( +          :stop_area, +          area_type: 'zdep', +          latitude: latlng[0], +          longitude: latlng[1] +        ) + +        create( +          :stop_point, +          route: route, +          stop_area: stop_area +        ) + +        stop_area +      end + +      way_costs = [ +        WayCost.new( +          departure: Geokit::LatLng.new( +            stop_areas[0].latitude, +            stop_areas[0].longitude +          ), +          arrival: Geokit::LatLng.new( +            stop_areas[1].latitude, +            stop_areas[1].longitude +          ), +          id: "#{stop_areas[0].id}-#{stop_areas[1].id}" +        ), +        WayCost.new( +          departure: Geokit::LatLng.new( +            stop_areas[1].latitude, +            stop_areas[1].longitude +          ), +          arrival: Geokit::LatLng.new( +            stop_areas[2].latitude, +            stop_areas[2].longitude +          ), +          id: "#{stop_areas[1].id}-#{stop_areas[2].id}" +        ), +        WayCost.new( +          departure: Geokit::LatLng.new( +            stop_areas[2].latitude, +            stop_areas[2].longitude +          ), +          arrival: Geokit::LatLng.new( +            stop_areas[3].latitude, +            stop_areas[3].longitude +          ), +          id: "#{stop_areas[2].id}-#{stop_areas[3].id}" +        ) +      ] + +      expect( +        StopAreasToWayCostsConverter.new(stop_areas).convert +      ).to eq(way_costs) +    end +  end +end | 
