diff options
| author | Teddy Wing | 2018-04-16 12:58:22 +0200 |
|---|---|---|
| committer | Teddy Wing | 2018-04-16 16:21:04 +0200 |
| commit | 7a9a08325db222b11d22a7e72458e8d66875c42a (patch) | |
| tree | 9aa8f9dbebd4fbac5f5f17e1643833be6774740d | |
| parent | ae3933586653c00ea92d788380fc81d3586e4d42 (diff) | |
| download | chouette-core-7a9a08325db222b11d22a7e72458e8d66875c42a.tar.bz2 | |
WayCost: Add `#cache_key` method6097-cache-route-WayCosts
Provides a uniform Rails cache key for all `WayCost`s using their `id`.
Since the `id` field is option, raise an error if the `WayCost` doesn't
have one.
Refs #6097
| -rw-r--r-- | lib/way_cost.rb | 8 | ||||
| -rw-r--r-- | spec/lib/way_cost_spec.rb | 22 |
2 files changed, 30 insertions, 0 deletions
diff --git a/lib/way_cost.rb b/lib/way_cost.rb index 9f860308c..53e69ddb3 100644 --- a/lib/way_cost.rb +++ b/lib/way_cost.rb @@ -24,4 +24,12 @@ class WayCost @time == other.time && @id == other.id end + + def cache_key + raise NullIDError if !id + + "way_cost/#{id}" + end + + class NullIDError < RuntimeError; end end diff --git a/spec/lib/way_cost_spec.rb b/spec/lib/way_cost_spec.rb new file mode 100644 index 000000000..1e85c68ab --- /dev/null +++ b/spec/lib/way_cost_spec.rb @@ -0,0 +1,22 @@ +RSpec.describe WayCost do + describe "#cache_key" do + it "returns a cache string including the ID of the WayCost" do + way_cost = WayCost.new( + departure: Geokit::LatLng.new(47.91231, 1.87606), + arrival: Geokit::LatLng.new(52.50867, 13.42879), + id: '2-3' + ) + + expect(way_cost.cache_key).to eq('way_cost/2-3') + end + + it "raises an error if the WayCost doesn't have an ID" do + way_cost = WayCost.new( + departure: Geokit::LatLng.new(47.91231, 1.87606), + arrival: Geokit::LatLng.new(52.50867, 13.42879) + ) + + expect { way_cost.cache_key }.to raise_error(WayCost::NullIDError) + end + end +end |
