From 1b7808e8c1b127bc8f788aedbe5c5dec59701660 Mon Sep 17 00:00:00 2001 From: Andrew Rove Date: Wed, 15 Nov 2017 13:22:40 -0600 Subject: [PATCH 1/2] Let cache handle marshalling of results. Also multi_fetch should use the splat operator. http://api.rubyonrails.org/classes/ActiveSupport/Cache/Store.html#method-i-fetch_multi --- lib/jsonapi/renderer/cached_resources_processor.rb | 12 ++---------- spec/caching_spec.rb | 4 ++-- 2 files changed, 4 insertions(+), 12 deletions(-) diff --git a/lib/jsonapi/renderer/cached_resources_processor.rb b/lib/jsonapi/renderer/cached_resources_processor.rb index 080ee99..609d2c3 100644 --- a/lib/jsonapi/renderer/cached_resources_processor.rb +++ b/lib/jsonapi/renderer/cached_resources_processor.rb @@ -4,12 +4,6 @@ module JSONAPI class Renderer # @private class CachedResourcesProcessor < ResourcesProcessor - class JSONString < String - def to_json(*) - self - end - end - def initialize(cache) @cache = cache end @@ -17,11 +11,9 @@ def initialize(cache) def process_resources [@primary, @included].each do |resources| cache_hash = cache_key_map(resources) - processed_resources = @cache.fetch_multi(cache_hash.keys) do |key| + processed_resources = @cache.fetch_multi(*cache_hash.keys) do |key| res, include, fields = cache_hash[key] - json = res.as_jsonapi(include: include, fields: fields).to_json - - JSONString.new(json) + json = res.as_jsonapi(include: include, fields: fields) end resources.replace(processed_resources.values) diff --git a/spec/caching_spec.rb b/spec/caching_spec.rb index f153ba7..9de4c94 100644 --- a/spec/caching_spec.rb +++ b/spec/caching_spec.rb @@ -5,7 +5,7 @@ def initialize @cache = {} end - def fetch_multi(keys) + def fetch_multi(*keys) keys.each_with_object({}) do |k, h| @cache[k] = yield(k) unless @cache.key?(k) h[k] = @cache[k] @@ -96,6 +96,6 @@ def fetch_multi(keys) } expect(JSON.parse(actual.to_json)).to eq(JSON.parse(expected.to_json)) - expect(actual[:data]).to be_a(JSONAPI::Renderer::CachedResourcesProcessor::JSONString) + expect(actual[:data]).to be_a(Hash) end end From 187ddb222699f68c7035d49cb4f0b357a6043e54 Mon Sep 17 00:00:00 2001 From: Andrew Rove Date: Wed, 15 Nov 2017 13:25:18 -0600 Subject: [PATCH 2/2] Code Climate feedback. --- lib/jsonapi/renderer/cached_resources_processor.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/jsonapi/renderer/cached_resources_processor.rb b/lib/jsonapi/renderer/cached_resources_processor.rb index 609d2c3..2e77f7d 100644 --- a/lib/jsonapi/renderer/cached_resources_processor.rb +++ b/lib/jsonapi/renderer/cached_resources_processor.rb @@ -13,7 +13,7 @@ def process_resources cache_hash = cache_key_map(resources) processed_resources = @cache.fetch_multi(*cache_hash.keys) do |key| res, include, fields = cache_hash[key] - json = res.as_jsonapi(include: include, fields: fields) + res.as_jsonapi(include: include, fields: fields) end resources.replace(processed_resources.values)