gris 0.3.8 → 0.3.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e9fe08cd20a13729cd7233e2f0bf1d3e9449073a
4
- data.tar.gz: 0f9b45250044716c25debc2e7b6e49b81ebd71b3
3
+ metadata.gz: 4ee4df75cb4ac7a293b7f2796ab5e53b3cbb6219
4
+ data.tar.gz: 798bec39179c86bf2dbef41d368d6047c80081e2
5
5
  SHA512:
6
- metadata.gz: f1670b79cc1848f4606dc952e13bb03027625b588679fadb628d6f424f0e335f9a0ea09b5619782467a88352e583df72e63c83729aea69c9355021f689d89d6d
7
- data.tar.gz: 61cd72f59c6e5b3169ad5868139d8199114b53dcadb5bf7532c6412d289930ce2717ea1a468b0393b8d589468f8f08039e43f3cb61d189c36b3c280081bb8421
6
+ metadata.gz: 3f37e39acf05cc99b2ae5ca09d933adbe69a36d741897cf30cb523e2f3c940bebde14f4e14ff2571a361ba3dddead63d131c5ee568cefc12e55c07e9bb8abab9
7
+ data.tar.gz: 5c94ff4ce3a9c260c140b3948fdab806dbd484c0176c5f954c0a86e70b834033182a64709219325a74d26c934c25e0150813a40dbbd6f3cf6b791d43c71ad92f
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- gris (0.3.8)
4
+ gris (0.3.9)
5
5
  activesupport (~> 4.2, >= 4.2.0)
6
6
  chronic (~> 0.10.0)
7
7
  dalli (~> 2.7)
@@ -84,7 +84,7 @@ GEM
84
84
  rack-accept
85
85
  rack-mount
86
86
  virtus (>= 1.0.0)
87
- grape-entity (0.4.5)
87
+ grape-entity (0.4.7)
88
88
  activesupport
89
89
  multi_json (>= 1.3.2)
90
90
  grape-roar (0.3.0)
data/README.md CHANGED
@@ -57,13 +57,21 @@ Commands:
57
57
  You can use caching by including this module in your ActiveRecord models,
58
58
 
59
59
  class OfferEvent < ActiveRecord::Base
60
- include Gris::CacheKey
60
+ include Gris::Caching
61
61
  end
62
62
 
63
63
  and then cache inside GET requests like this
64
64
 
65
65
  offer_event = OfferEvent.cached_find(id)
66
66
 
67
+ To expire the cache for an object, call
68
+
69
+ OfferEvent.expire_cache_for(id)
70
+
71
+ or, if you have an instance, use
72
+
73
+ offer_event.expire_cache
74
+
67
75
  ### The name
68
76
 
69
77
  Gris is named for [Juan Gris](https://www.artsy.net/artist/juan-gris) (but also works in the wine-based word context of Grape and Napa). Cleverness!
@@ -0,0 +1,29 @@
1
+ module Gris
2
+ module Caching
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ def expire_cache
7
+ self.class.expire_cache_for(id)
8
+ end
9
+ end
10
+
11
+ class_methods do
12
+ def cached_find(id)
13
+ Gris.cache.fetch(cache_key_for(id)) do
14
+ find(id)
15
+ end
16
+ end
17
+
18
+ def expire_cache_for(id)
19
+ Gris.cache.delete(cache_key_for(id))
20
+ end
21
+
22
+ private
23
+
24
+ def cache_key_for(id)
25
+ "#{model_name.cache_key}/#{id}"
26
+ end
27
+ end
28
+ end
29
+ end
data/lib/gris/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  module Gris
2
- VERSION = '0.3.8'
2
+ VERSION = '0.3.9'
3
3
 
4
4
  class Version
5
5
  class << self
data/lib/gris.rb CHANGED
@@ -12,7 +12,7 @@ require 'hashie-forbidden_attributes'
12
12
 
13
13
  # require internal files
14
14
  require 'gris/application'
15
- require 'gris/cache_key'
15
+ require 'gris/caching'
16
16
  require 'gris/deprecations'
17
17
  require 'gris/grape_extensions/crud_helpers'
18
18
  require 'gris/grape_extensions/error_helpers'
data/spec/cache_spec.rb CHANGED
@@ -1,14 +1,14 @@
1
1
  require 'spec_helper'
2
- require 'gris/identity'
3
2
 
4
3
  describe 'Gris.cache' do
5
- before(:each) do
6
- @cache = Gris.cache
4
+ let(:cache) { Gris.cache }
5
+
6
+ it 'returns nil by default' do
7
+ expect(cache.read('x')).to be_nil
7
8
  end
8
9
 
9
- it 'caches things' do
10
- expect(@cache.read('x')).to be_nil
11
- @cache.write('x', 'abc')
12
- expect(@cache.read('x')).to eq('abc')
10
+ it 'permits writing to and retrieving from cache' do
11
+ cache.write('x', 'abc')
12
+ expect(cache.read('x')).to eq('abc')
13
13
  end
14
14
  end
@@ -0,0 +1,25 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'Gris::Caching' do
4
+ let(:id) { SecureRandom.random_number(500) }
5
+
6
+ it 'uses the cache for cached_find' do
7
+ foo = CachingHelper.cached_find(id)
8
+ expect(foo.id).to eq(id)
9
+ allow(Gris.cache).to receive(:fetch).and_return('cached-result')
10
+ expect(CachingHelper.cached_find(id)).to eq('cached-result')
11
+ end
12
+
13
+ it 'expires cache for a given id' do
14
+ allow(Gris.cache).to receive(:delete).with("cache_key_helper/#{id}")
15
+ CachingHelper.expire_cache_for(id)
16
+ expect(Gris.cache).to have_received(:delete).with("cache_key_helper/#{id}")
17
+ end
18
+
19
+ it 'expires cache for a given instance' do
20
+ allow(Gris.cache).to receive(:delete).with("cache_key_helper/#{id}")
21
+ instance = CachingHelper.new(id: id)
22
+ instance.expire_cache
23
+ expect(Gris.cache).to have_received(:delete).with("cache_key_helper/#{id}")
24
+ end
25
+ end
@@ -1,8 +1,8 @@
1
1
  require 'active_model'
2
2
 
3
- class CacheKeyHelper
3
+ class CachingHelper
4
4
  include ActiveModel::Model
5
- include Gris::CacheKey
5
+ include Gris::Caching
6
6
 
7
7
  attr_accessor :id
8
8
 
@@ -12,11 +12,11 @@ class CacheKeyHelper
12
12
 
13
13
  class << self
14
14
  def find(id)
15
- CacheKeyHelper.new(id: id)
15
+ CachingHelper.new(id: id)
16
16
  end
17
17
 
18
18
  def model_name
19
- CacheKeyHelper
19
+ CachingHelper
20
20
  end
21
21
 
22
22
  def cache_key
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gris
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.8
4
+ version: 0.3.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dylan Fareed
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-07-31 00:00:00.000000000 Z
11
+ date: 2015-08-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: thor
@@ -329,7 +329,7 @@ files:
329
329
  - lib/gris.rb
330
330
  - lib/gris/application.rb
331
331
  - lib/gris/application/configuration.rb
332
- - lib/gris/cache_key.rb
332
+ - lib/gris/caching.rb
333
333
  - lib/gris/cli.rb
334
334
  - lib/gris/deprecations.rb
335
335
  - lib/gris/deprecations/gris_setup.rb
@@ -384,8 +384,8 @@ files:
384
384
  - lib/gris/version.rb
385
385
  - lib/tasks/db.rake
386
386
  - lib/tasks/routes.rake
387
- - spec/cache_key_spec.rb
388
387
  - spec/cache_spec.rb
388
+ - spec/caching_spec.rb
389
389
  - spec/generators/api_generator_spec.rb
390
390
  - spec/generators/migration_generator_spec.rb
391
391
  - spec/generators/scaffold_generator_spec.rb
@@ -396,7 +396,7 @@ files:
396
396
  - spec/identity_spec.rb
397
397
  - spec/integration/token_authentication_spec.rb
398
398
  - spec/spec_helper.rb
399
- - spec/support/cache_key_helper.rb
399
+ - spec/support/caching_helper.rb
400
400
  - spec/support/integration_setup.rb
401
401
  - spec/support/spec_api_auth_helper.rb
402
402
  - spec/support/spec_api_error_helper.rb
@@ -428,8 +428,8 @@ signing_key:
428
428
  specification_version: 4
429
429
  summary: A simple api microservice generator framework.
430
430
  test_files:
431
- - spec/cache_key_spec.rb
432
431
  - spec/cache_spec.rb
432
+ - spec/caching_spec.rb
433
433
  - spec/generators/api_generator_spec.rb
434
434
  - spec/generators/migration_generator_spec.rb
435
435
  - spec/generators/scaffold_generator_spec.rb
@@ -440,7 +440,7 @@ test_files:
440
440
  - spec/identity_spec.rb
441
441
  - spec/integration/token_authentication_spec.rb
442
442
  - spec/spec_helper.rb
443
- - spec/support/cache_key_helper.rb
443
+ - spec/support/caching_helper.rb
444
444
  - spec/support/integration_setup.rb
445
445
  - spec/support/spec_api_auth_helper.rb
446
446
  - spec/support/spec_api_error_helper.rb
@@ -1,31 +0,0 @@
1
- module Gris
2
- module CacheKey
3
- extend ActiveSupport::Concern
4
-
5
- included do
6
- def cache_key
7
- case
8
- when new_record?
9
- fail NotImplementedError
10
- when timestamp = try(:updated_at)
11
- timestamp = timestamp.utc.to_s(:number)
12
- "#{self.class.model_name.cache_key}/#{id}-#{timestamp}"
13
- else
14
- "#{self.class.model_name.cache_key}/#{id}"
15
- end
16
- end
17
- end
18
-
19
- class_methods do
20
- def cache_key_for(id)
21
- "#{model_name.cache_key}/#{id}"
22
- end
23
-
24
- def cached_find(id)
25
- Gris.cache.fetch(cache_key_for(id)) do
26
- find(id)
27
- end
28
- end
29
- end
30
- end
31
- end
@@ -1,26 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe 'Gris::CacheKey' do
4
- it 'raises an error when cache_key is called on a blank object' do
5
- @foo = CacheKeyHelper.new
6
- expect { @foo.cache_key }.to raise_error(NotImplementedError)
7
- end
8
-
9
- it 'returns a value for cache_key' do
10
- @foo = CacheKeyHelper.new(id: SecureRandom.random_number(500))
11
- expect(@foo.cache_key).to eq("cache_key_helper/#{@foo.id}")
12
- end
13
-
14
- it 'returns a value for cache_key_for' do
15
- id = SecureRandom.random_number(500)
16
- expect(CacheKeyHelper.cache_key_for(id)).to eq("cache_key_helper/#{id}")
17
- end
18
-
19
- it 'uses the cache for cached_find' do
20
- id = SecureRandom.random_number(500)
21
- foo = CacheKeyHelper.cached_find(id)
22
- expect(foo.id).to eq(id)
23
- allow(Gris.cache).to receive(:fetch).and_return('cached-result')
24
- expect(CacheKeyHelper.cached_find(id)).to eq('cached-result')
25
- end
26
- end