Skip to content

Commit 80568f5

Browse files
translate cache to configuration
1 parent 4cd106a commit 80568f5

4 files changed

Lines changed: 49 additions & 3 deletions

File tree

lib/ruber.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
require "forwardable"
55
require "ruber/configuration"
66
require "ruber/authenticator"
7-
require "ruber/cache"
87

98
# a Ruby wrapper for Uber API
109
module Ruber
@@ -18,8 +17,8 @@ class << self
1817
extend Forwardable
1918

2019
def_delegators(
21-
:configuration, :customer_id, :client_id, :client_secret,
22-
:customer_id=, :client_id=, :client_secret=, :cache_key, :cache_key=
20+
:configuration, :customer_id, :client_id, :client_secret, :cache,
21+
:customer_id=, :client_id=, :client_secret=, :cache_key, :cache_key=, :cache=
2322
)
2423
end
2524
end

lib/ruber/configuration.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,24 @@
11
# frozen_string_literal: true
22

3+
require_relative "configuration/null_cache"
4+
35
module Ruber
46
class Configuration
57
attr_accessor :customer_id, :client_id, :client_secret
68
attr_writer :cache_key
79

10+
def cache
11+
@cache ||= NullCache.new
12+
end
13+
14+
def cache=(store)
15+
unless %i[read write clear delete].all? { |method| store.respond_to?(method) }
16+
raise ArgumentError, "cache_store must respond to read, write, clear, and delete"
17+
end
18+
19+
@cache = store
20+
end
21+
822
def cache_key
923
@cache_key || "#{customer_id}_#{client_id}_access_token"
1024
end
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# frozen_string_literal: true
2+
3+
module Ruber
4+
class Configuration
5+
class NullCache
6+
def read(key) = memory_store[key]
7+
def write(key, value, _options = {}) = memory_store[key] = value
8+
def clear = memory_store.clear
9+
def delete(key) = memory_store.delete(key)
10+
def memory_store = @memory_store ||= {}
11+
end
12+
end
13+
end

test/ruber/configuration_test.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,14 @@ def setup
1212
cache_key: "custom_cache_key"
1313
}
1414

15+
@custom_cache = Class.new do
16+
def read(key) = memory_store[key]
17+
def write(key, value, _options = {}) = memory_store[key] = value
18+
def clear = memory_store.clear
19+
def delete(key) = memory_store.delete(key)
20+
def memory_store = @memory_store ||= {}
21+
end.new
22+
1523
Ruber.configuration = nil
1624
end
1725

@@ -66,6 +74,18 @@ def test_cache_key_can_be_set_explicitly
6674
assert_equal @config_values[:cache_key], Ruber.cache_key
6775
end
6876

77+
def test_cache_can_be_set_explicitly
78+
Ruber.cache = @custom_cache
79+
80+
assert_equal @custom_cache, Ruber.cache
81+
end
82+
83+
def test_cache_must_respond_to_read_write_clear_and_delete
84+
assert_raises(ArgumentError) do
85+
Ruber.cache = Object.new
86+
end
87+
end
88+
6989
private
7090

7191
def assert_configuration_values

0 commit comments

Comments
 (0)