Sha256: 6aa40557bfc43d00fd9dac605d770edd14f32b1d83492c3bc7009e42143772fa

Contents?: true

Size: 1005 Bytes

Versions: 2

Compression:

Stored size: 1005 Bytes

Contents

# This class allows you to configure how Geocoder2 should treat errors that occur when
# the cache is not available.
# Configure it like this
# config/initializers/geocoder2.rb
# Geocoder2.configure(
#  :cache => Geocoder2::CacheBypass.new(Redis.new)
# )
#
# Depending on the value of @bypass this will either
# raise the exception (true) or swallow it and pretend the cache did not return a hit (false)
#
class Geocoder2::CacheBypass
  def initialize(target, bypass = true)
    @target = target
    @bypass = bypass
  end


  def [](key)
    with_bypass { @target[key] }
  end

  def []=(key, value)
    with_bypass(value) { @target[key] = value }
  end

  def keys
    with_bypass([]) { @target.keys }
  end

  def del(key)
    with_bypass { @target.del(key) }
  end

  private

  def with_bypass(return_value_if_exception = nil, &block)
    begin
      yield
    rescue
      if @bypass
        return_value_if_exception
      else
        raise # reraise original exception
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
geocoder2-0.1.1 examples/cache_bypass.rb
geocoder2-0.1.0 examples/cache_bypass.rb