From 05f81829b3546bd95bc92b222e0e2bcb19d96771 Mon Sep 17 00:00:00 2001 From: Graham Wall Date: Tue, 8 Aug 2017 17:14:03 +0100 Subject: [PATCH 1/3] Remove explicit check for Moneta::Transformer to allow use of Moneta::Expires --- lib/parse/client.rb | 2 +- lib/parse/client/caching.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/parse/client.rb b/lib/parse/client.rb index 8eac58e..7dc3d08 100644 --- a/lib/parse/client.rb +++ b/lib/parse/client.rb @@ -277,7 +277,7 @@ def initialize(opts = {}) end end - unless opts[:cache].is_a?(Moneta::Transformer) + unless [:key?, :[], :delete, :store].all? { |method| opts[:cache].respond_to?(method) } raise ArgumentError, "Parse::Client option :cache needs to be a type of Moneta::Transformer store." end self.cache = opts[:cache] diff --git a/lib/parse/client/caching.rb b/lib/parse/client/caching.rb index b680627..fad6d20 100644 --- a/lib/parse/client/caching.rb +++ b/lib/parse/client/caching.rb @@ -81,7 +81,7 @@ def initialize(adapter, store, opts = {}) @opts.merge!(opts) if opts.is_a?(Hash) @expires = @opts[:expires] - unless @store.is_a?(Moneta::Transformer) + unless [:key?, :[], :delete, :store].all? { |method| @store.respond_to?(method) } raise ArgumentError, "Caching store object must a Moneta key/value store (Moneta::Transformer)." end From d010066d3b69c5ab0c0afba7a30914c141ebe6c0 Mon Sep 17 00:00:00 2001 From: Graham Wall Date: Wed, 9 Aug 2017 09:35:10 +0100 Subject: [PATCH 2/3] Comments for Moneta::Expires cache change --- lib/parse/client.rb | 11 ++++++----- lib/parse/client/caching.rb | 6 +++--- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/lib/parse/client.rb b/lib/parse/client.rb index 7dc3d08..1232953 100644 --- a/lib/parse/client.rb +++ b/lib/parse/client.rb @@ -122,7 +122,7 @@ class ResponseError < Parse::Error; end; # @!attribute cache # The underlying cache store for caching API requests. - # @return [Moneta::Transformer] + # @return [Object] # @!attribute [r] application_id # The Parse application identifier to be sent in every API request. # @return [String] @@ -206,8 +206,9 @@ def setup(opts = {}) # the logging performed by {Parse::Middleware::BodyBuilder}. # @option opts [Object] :adapter The connection adapter. By default it uses # the `Faraday.default_adapter` which is Net/HTTP. - # @option opts [Moneta::Transformer] :cache A caching adapter of type - # {https://github.com/minad/moneta Moneta::Transformer} that will be used + # @option opts [Object] :cache A caching adapter of type + # {https://github.com/minad/moneta Moneta::Transformer} or + # {https://github.com/minad/moneta Moneta::Expires} that will be used # by the caching middleware {Parse::Middleware::Caching}. # Caching queries and object fetches can help improve the performance of # your application, even if it is for a few seconds. Only successful GET @@ -223,7 +224,7 @@ def setup(opts = {}) # @option opts [Hash] :faraday You may pass a hash of options that will be # passed to the Faraday constructor. # @raise Parse::Error::ConnectionError if the client was not properly configured with required keys or url. - # @raise ArgumentError if the cache instance passed to the :cache option is not of Moneta::Transformer. + # @raise ArgumentError if the cache instance passed to the :cache option is not of Moneta::Transformer or Moneta::Expires # @see Parse::Middleware::BodyBuilder # @see Parse::Middleware::Caching # @see Parse::Middleware::Authentication @@ -278,7 +279,7 @@ def initialize(opts = {}) end unless [:key?, :[], :delete, :store].all? { |method| opts[:cache].respond_to?(method) } - raise ArgumentError, "Parse::Client option :cache needs to be a type of Moneta::Transformer store." + raise ArgumentError, "Parse::Client option :cache needs to be a type of Moneta store" end self.cache = opts[:cache] conn.use Parse::Middleware::Caching, self.cache, {expires: opts[:expires].to_i } diff --git a/lib/parse/client/caching.rb b/lib/parse/client/caching.rb index fad6d20..52e2b83 100644 --- a/lib/parse/client/caching.rb +++ b/lib/parse/client/caching.rb @@ -59,7 +59,7 @@ def caching? # @!attribute [rw] store # The internal moneta cache store instance. - # @return [Moneta::Transformer] + # @return [Object] attr_accessor :store # @!attribute [rw] expires @@ -73,7 +73,7 @@ def caching? # @param store [Moneta] An instance of the Moneta cache store to use. # @param opts [Hash] additional options. # @option opts [Integer] :expires the default expiration for a cache entry. - # @raise ArgumentError, if `store` is not a Moneta::Transformer instance. + # @raise ArgumentError, if `store` is not a Moneta::Transformer or Moneta::Expires instance. def initialize(adapter, store, opts = {}) super(adapter) @store = store @@ -82,7 +82,7 @@ def initialize(adapter, store, opts = {}) @expires = @opts[:expires] unless [:key?, :[], :delete, :store].all? { |method| @store.respond_to?(method) } - raise ArgumentError, "Caching store object must a Moneta key/value store (Moneta::Transformer)." + raise ArgumentError, "Caching store object must a Moneta key/value store." end end From c9daab9099830e1f62c3fb287fa7e412fc850a43 Mon Sep 17 00:00:00 2001 From: Graham Wall Date: Fri, 11 Aug 2017 16:57:14 +0100 Subject: [PATCH 3/3] Add some basic tests of cache type supplied on init --- test/lib/parse/cache_test.rb | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 test/lib/parse/cache_test.rb diff --git a/test/lib/parse/cache_test.rb b/test/lib/parse/cache_test.rb new file mode 100644 index 0000000..0b0a9e6 --- /dev/null +++ b/test/lib/parse/cache_test.rb @@ -0,0 +1,32 @@ +require_relative '../../test_helper' + +class TestCache < Minitest::Test + def setup + @init_object = { + server_url: 'http://b.com/parse', + app_id: 'abc', + api_key: 'def' + } + end + + def test_no_cache_ok + assert Parse.setup(@init_object) + end + + def test_moneta_transformer_accepted + init = @init_object.merge(cache: Moneta.new(:LRUHash)) + assert init[:cache].is_a?(Moneta::Transformer) + assert Parse.setup(init) + end + + def test_moneta_expire_accepted + init = @init_object.merge(cache: Moneta.new(:LRUHash, expires: 13)) + assert init[:cache].is_a?(Moneta::Expires) + assert Parse.setup(init) + end + + def test_bad_cache_type_rejected + init = @init_object.merge(cache: 'hamster') + assert_raises(ArgumentError) { Parse.setup(init) } + end +end