infopark_component_cache 3.0.0 → 3.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f2561cd7d70ca3cf1f7b21a07bdb092456118933
4
- data.tar.gz: 1b4c46f017f400aad1b7526ccbfacce200345dc2
3
+ metadata.gz: 97f5a401ded8bb31c5e7539f39bdb7686fbbbb69
4
+ data.tar.gz: 99d9b252e0e9b37b03c17aac61753a354d4d3b27
5
5
  SHA512:
6
- metadata.gz: d1f5995924da3bad924c94b7cbe788d3e6e7008e6feddad7a47752c38cb5e9cf683b53e663afbbb3b59df6aebcde8a3f981b8e71ca36c78e1b1ba0ad71450124
7
- data.tar.gz: ccb9eff5d683ba9c5a914a6964c7ff852facc6851a28c7ab56230e1278b82dd29768e6c1e573d74202ccc42dce4e52f1cd24facbb45c2786ef2806f6b0cf6a98
6
+ metadata.gz: f4b0472ea662483d07b9d21b45c2be93e056a7cb76bf09f2b7b56423b0a4d00b93469c6a6babecfc79b1d2485aa38356765fbe46222c93518f2d9eefd249b38b
7
+ data.tar.gz: 5b7b9f4e4d36c41e35b87ef8382d72ed7ecec73d39de1f7cb01155444131848d19d5093c3dd6d12adf34512139e9d02053748529d3f7a3da0c1ca74bb5e2158e
data/README.md CHANGED
@@ -16,6 +16,14 @@ Or install it yourself as:
16
16
 
17
17
  $ gem install infopark_component_cache
18
18
 
19
+ ## Tests
20
+
21
+ To run the test suite just execute:
22
+
23
+ $ bundle exec rspec spec/
24
+
25
+ No setup neccessary.
26
+
19
27
  ## Usage
20
28
 
21
29
  Set up Rails catching (see [http://guides.rubyonrails.org/caching_with_rails.html](http://guides.rubyonrails.org/caching_with_rails.html)) and
@@ -21,7 +21,7 @@ Gem::Specification.new do |gem|
21
21
 
22
22
  gem.add_dependency 'infopark_fiona_connector'
23
23
 
24
- gem.add_development_dependency 'rspec-rails'
24
+ gem.add_development_dependency 'rspec-rails', '~> 3.5'
25
25
  gem.add_development_dependency 'sqlite3'
26
26
  gem.add_development_dependency 'byebug'
27
27
  end
@@ -5,12 +5,16 @@ module InfoparkComponentCache
5
5
  # and some parameters (hash). It should be used to point
6
6
  # to some data stored in a particular context
7
7
  class Component
8
- attr_reader :obj, :name, :params
8
+ attr_reader :obj, :component, :params
9
9
 
10
10
  def initialize(obj, component, params={})
11
11
  @obj, @component, @params = obj, component, params
12
12
  end
13
13
 
14
+ def name
15
+ self.component
16
+ end
17
+
14
18
  def cache_key(meta_prefix=nil)
15
19
  if meta_prefix
16
20
  meta_prefix + "_" + KeyGenerator.generate_key(identity_hash)
@@ -23,4 +27,4 @@ module InfoparkComponentCache
23
27
  @params.merge({:obj_name=>@obj.name, :obj_id=>@obj.id, :obj_component=>@component})
24
28
  end
25
29
  end
26
- end
30
+ end
@@ -54,6 +54,7 @@ module InfoparkComponentCache
54
54
  #
55
55
  # @return true if cache is valid and in consistent state
56
56
  def expired?
57
+ return true if !cache.enabled?
57
58
  return !guards.all?(&:consistent?)
58
59
 
59
60
  rescue => exception
@@ -73,8 +74,10 @@ module InfoparkComponentCache
73
74
  if expired?
74
75
  value = yield
75
76
  begin
76
- cache.write(component.cache_key, value)
77
- ensure_consistency!
77
+ if cache.enabled?
78
+ cache.write(component.cache_key, value)
79
+ ensure_consistency!
80
+ end
78
81
  return value
79
82
 
80
83
  rescue => exception
@@ -1,3 +1,3 @@
1
1
  module InfoparkComponentCache
2
- VERSION = "3.0.0"
2
+ VERSION = "3.1.0"
3
3
  end
@@ -1,2 +1,3 @@
1
1
  Dummy::Application.configure do
2
+ config.cache_store = :memory_store
2
3
  end
@@ -0,0 +1,116 @@
1
+ require 'spec_helper'
2
+
3
+ describe InfoparkComponentCache::ComponentCache do
4
+ let(:obj) { double(name: 'spec_obj', id: 2001) }
5
+ let(:name) { 'spec_cached_component' }
6
+ let(:params) { {some: 'additional', params: 'supplied'} }
7
+ let(:guard) { Class.new(Struct.new(:component)) }
8
+
9
+ subject { described_class.new(obj, name, params, [guard]) }
10
+
11
+ context 'with caching disabled' do
12
+ before { allow(Rails.application.config.action_controller).to receive(:perform_caching).and_return(false) }
13
+
14
+ describe '#fetch' do
15
+ let(:value) { 'very_hard_computation_required_for_this_string' }
16
+ let(:specific_guard) { subject.guards.first }
17
+
18
+ it 'returns the passed the block value' do
19
+ expect(subject.fetch { value }).to eq(value)
20
+ end
21
+
22
+ it 'never calls any methods on the guard' do
23
+ expect(specific_guard).not_to receive(:consistent?)
24
+ expect(specific_guard).not_to receive(:guard!)
25
+
26
+ subject.fetch { value }
27
+ end
28
+ end
29
+ end
30
+
31
+ context 'with caching enabled' do
32
+ before { allow(Rails.application.config.action_controller).to receive(:perform_caching).and_return(true) }
33
+
34
+ let(:guard) { Class.new(InfoparkComponentCache::ConsistencyGuard) {
35
+ def consistent?
36
+ cache.exist?(:guard_called)
37
+ end
38
+
39
+ def guard!
40
+ cache.write(:guard_called, 1)
41
+ end
42
+ } }
43
+
44
+ describe '#fetch' do
45
+ let(:value) { 'very_hard_computation_required_for_this_string' }
46
+ let(:specific_guard) { subject.guards.first }
47
+ let(:computer) { double }
48
+
49
+ it 'returns the passed the block value' do
50
+ expect(subject.fetch { value }).to eq(value)
51
+ end
52
+
53
+ it 'calls the required methods on the guard' do
54
+ expect(specific_guard).to receive(:consistent?).exactly(3).times
55
+ expect(specific_guard).to receive(:guard!).exactly(3).times
56
+
57
+ 3.times { subject.fetch { value } }
58
+ end
59
+
60
+ it 'only evalues the block once' do
61
+ expect(computer).to receive(:compute).and_return(value).once
62
+
63
+ 3.times { expect(subject.fetch { computer.compute }).to eq(value) }
64
+ end
65
+ end
66
+ end
67
+
68
+ describe '#compontent' do
69
+ subject { described_class.new(obj, name, params).component }
70
+
71
+ it 'stores the passed obj' do
72
+ expect(subject.obj).to eq(obj)
73
+ end
74
+
75
+ it 'stores the passed name' do
76
+ expect(subject.name).to eq(name)
77
+ end
78
+
79
+ it 'stores the passed params' do
80
+ expect(subject.params).to eq(params)
81
+ end
82
+ end
83
+
84
+ describe '#guards' do
85
+ let(:guard_class1) { Struct.new(:component) }
86
+ let(:guard_class2) { Struct.new(:compontent, :extra) }
87
+ let(:guard_params) { {guard: guard_class2, something: 'more'} }
88
+
89
+ subject { described_class.new(obj, name, params, [guard_class1, guard_params]).guards }
90
+
91
+ it 'contains an array of passed guards' do
92
+ expect(subject).to match_array([
93
+ an_instance_of(guard_class1),
94
+ an_instance_of(guard_class2)
95
+ ])
96
+ end
97
+
98
+ it 'preserves the extra params' do
99
+ expect(subject.last.extra).to eq(guard_params)
100
+ end
101
+
102
+ context 'with no guards specified in the constructors' do
103
+ subject { described_class.new(obj, name, params).guards }
104
+
105
+ it 'contains standard guards' do
106
+ expect(subject).to match_array([
107
+ an_instance_of(InfoparkComponentCache::Guards::ValuePresent),
108
+ an_instance_of(InfoparkComponentCache::Guards::LastChanged),
109
+ an_instance_of(InfoparkComponentCache::Guards::ObjCount),
110
+ an_instance_of(InfoparkComponentCache::Guards::ValidFrom),
111
+ an_instance_of(InfoparkComponentCache::Guards::ValidUntil)
112
+ ])
113
+ end
114
+ end
115
+ end
116
+ end
@@ -22,4 +22,7 @@ RSpec.configure do |config|
22
22
  config.mock_with :rspec do |c|
23
23
  c.syntax = [:should, :expect]
24
24
  end
25
+ config.before :each do
26
+ Rails.cache.clear
27
+ end
25
28
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: infopark_component_cache
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.0
4
+ version: 3.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tomasz Przedmojski
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-05-24 00:00:00.000000000 Z
11
+ date: 2017-02-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -42,16 +42,16 @@ dependencies:
42
42
  name: rspec-rails
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - ">="
45
+ - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: '0'
47
+ version: '3.5'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - ">="
52
+ - - "~>"
53
53
  - !ruby/object:Gem::Version
54
- version: '0'
54
+ version: '3.5'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: sqlite3
57
57
  requirement: !ruby/object:Gem::Requirement
@@ -137,6 +137,7 @@ files:
137
137
  - spec/dummy/db/development.sqlite3
138
138
  - spec/dummy/db/schema.rb
139
139
  - spec/dummy/log/.gitkeep
140
+ - spec/lib/compontent_cache_spec.rb
140
141
  - spec/lib/delayed_guard_spec.rb
141
142
  - spec/lib/guards/always_consistent_spec.rb
142
143
  - spec/lib/guards/last_changed_spec.rb
@@ -187,6 +188,7 @@ test_files:
187
188
  - spec/dummy/db/development.sqlite3
188
189
  - spec/dummy/db/schema.rb
189
190
  - spec/dummy/log/.gitkeep
191
+ - spec/lib/compontent_cache_spec.rb
190
192
  - spec/lib/delayed_guard_spec.rb
191
193
  - spec/lib/guards/always_consistent_spec.rb
192
194
  - spec/lib/guards/last_changed_spec.rb