From adab844784abf15fe79803b5cfa9eeaf7955e418 Mon Sep 17 00:00:00 2001 From: hakanensari Date: Tue, 1 Apr 2014 11:21:14 +0100 Subject: [PATCH] Support older dalli client versions --- lib/rack/attack/store_proxy.rb | 10 ++++++++++ spec/integration/rack_attack_cache_spec.rb | 7 +++++++ 2 files changed, 17 insertions(+) diff --git a/lib/rack/attack/store_proxy.rb b/lib/rack/attack/store_proxy.rb index abd9089..8bb04d9 100644 --- a/lib/rack/attack/store_proxy.rb +++ b/lib/rack/attack/store_proxy.rb @@ -58,6 +58,7 @@ module Rack class DalliProxy < SimpleDelegator def initialize(client) super(client) + stub_with_method_on_older_clients end def read(key) @@ -81,6 +82,15 @@ module Rack rescue Dalli::DalliError end + private + + # So we support Dalli < 2.7.0 + def stub_with_method_on_older_clients + unless __getobj__.respond_to?(:with) + def __getobj__.with; yield self; end + end + end + end end end diff --git a/spec/integration/rack_attack_cache_spec.rb b/spec/integration/rack_attack_cache_spec.rb index adc755a..af82197 100644 --- a/spec/integration/rack_attack_cache_spec.rb +++ b/spec/integration/rack_attack_cache_spec.rb @@ -141,4 +141,11 @@ describe Rack::Attack::Cache do end end + describe "given an older Dalli::Client" do + it "should stub #with" do + proxy = Rack::Attack::StoreProxy::DalliProxy.new(Class.new) + proxy.must_respond_to :with + end + end + end