looksy 0.0.8 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,7 +1,7 @@
1
1
  require "looksy/version"
2
2
  require "looksy/dynamic_find_match"
3
3
  require "looksy/repository"
4
- require "looksy/null_cache"
4
+ require "looksy/cache"
5
5
  require "looksy/cacheable"
6
6
 
7
7
  module Looksy
@@ -0,0 +1,7 @@
1
+ require "looksy/cache/null"
2
+ require "looksy/cache/memory"
3
+
4
+ module Looksy
5
+ module Cache
6
+ end
7
+ end
@@ -0,0 +1,25 @@
1
+ module Looksy
2
+ module Cache
3
+ class Memory
4
+ def initialize
5
+ @data = {}
6
+ end
7
+
8
+ def fetch(key, options = {}, &block)
9
+ unless @data[key]
10
+ @data[key] = block.call if block_given?
11
+ end
12
+
13
+ @data[key]
14
+ end
15
+
16
+ def has_key?(key)
17
+ true unless @data[key].nil?
18
+ end
19
+
20
+ def clear
21
+ @data = {}
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,9 @@
1
+ module Looksy
2
+ module Cache
3
+ class Null
4
+ def fetch(key, options = {}, &block)
5
+ block.call if block_given?
6
+ end
7
+ end
8
+ end
9
+ end
@@ -6,7 +6,7 @@ module Looksy
6
6
 
7
7
  module ClassMethods
8
8
  def cache_store
9
- @cache_store ||= defined?(Rails) ? Rails.cache : Looksy::NullCache.new
9
+ @cache_store ||= defined?(Rails) ? Rails.cache : Looksy::Cache::Memory.new
10
10
  end
11
11
 
12
12
  def cache_store=(store)
@@ -1,3 +1,3 @@
1
1
  module Looksy
2
- VERSION = "0.0.8"
2
+ VERSION = "0.1.0"
3
3
  end
@@ -0,0 +1,72 @@
1
+ require "spec_helper"
2
+
3
+ describe Looksy::Cache::Memory do
4
+ let(:cache) { Looksy::Cache::Memory.new }
5
+
6
+ after { cache.clear }
7
+
8
+ describe '#fetch' do
9
+ context 'when passing block' do
10
+ context 'when key exists' do
11
+ before { cache.fetch('block_with_key') { 'result' } }
12
+
13
+ it 'returns the correct data' do
14
+ cache.fetch('block_with_key').should eql('result')
15
+ end
16
+ end
17
+
18
+ context 'when key does not exist' do
19
+ it 'stores the data' do
20
+ cache.fetch('block_without_key') { 'result' }
21
+ cache.should have_key('block_without_key')
22
+ end
23
+
24
+ it 'returns the correct data' do
25
+ cache.fetch('block_without_key') { 'result' }
26
+ cache.fetch('block_without_key').should eql('result')
27
+ end
28
+ end
29
+ end
30
+
31
+ context 'when not passing block' do
32
+ context 'when key exists' do
33
+ before { cache.fetch('key_without_block') { 'result' } }
34
+
35
+ it 'returns the correct data' do
36
+ cache.fetch('key_without_block').should eql('result')
37
+ end
38
+ end
39
+
40
+ context 'when key does not exist' do
41
+ it 'returns nil' do
42
+ cache.fetch('no_key_without_block').should be_nil
43
+ end
44
+ end
45
+ end
46
+ end
47
+
48
+ describe '#clear' do
49
+ before { cache.fetch('clear_data') { 'result'} }
50
+
51
+ it 'clears the stored data' do
52
+ cache.clear
53
+ cache.should_not have_key('clear_data')
54
+ end
55
+ end
56
+
57
+ describe '#has_key?' do
58
+ context 'when has key' do
59
+ before { cache.fetch('has_key') { 'result' } }
60
+
61
+ it 'returns true' do
62
+ cache.should have_key('has_key')
63
+ end
64
+ end
65
+
66
+ context 'when does not have key' do
67
+ it 'returns false' do
68
+ cache.should_not have_key('dont_have_key')
69
+ end
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,19 @@
1
+ require "spec_helper"
2
+
3
+ describe Looksy::Cache::Null do
4
+ let(:cache) { Looksy::Cache::Null.new }
5
+
6
+ describe '#fetch' do
7
+ context 'when passing block' do
8
+ it 'returns the result of the block' do
9
+ cache.fetch('key') { 'result' }.should eql('result')
10
+ end
11
+ end
12
+
13
+ context 'when not passing block' do
14
+ it 'returns nil' do
15
+ cache.fetch('key').should be_nil
16
+ end
17
+ end
18
+ end
19
+ end
@@ -14,7 +14,7 @@ describe Looksy::Cacheable do
14
14
 
15
15
  context 'when not using Rails' do
16
16
  it 'returns the default cache store' do
17
- klass.cache_store.should be_a(Looksy::NullCache)
17
+ klass.cache_store.should be_a(Looksy::Cache::Memory)
18
18
  end
19
19
  end
20
20
  end
@@ -3,7 +3,7 @@ require "support/record"
3
3
 
4
4
  describe Looksy::Repository do
5
5
  let(:klass) { Record }
6
- let(:cache) { Looksy::NullCache.new }
6
+ let(:cache) { Looksy::Cache::Memory.new }
7
7
  let(:lookup) { Looksy::Repository.new(klass, cache) }
8
8
 
9
9
  describe '#all' do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: looksy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.8
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,12 +9,12 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-07-17 00:00:00.000000000 -07:00
12
+ date: 2013-07-30 00:00:00.000000000 -07:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rspec
17
- requirement: &53734640 !ruby/object:Gem::Requirement
17
+ requirement: &65060540 !ruby/object:Gem::Requirement
18
18
  none: false
19
19
  requirements:
20
20
  - - ! '>='
@@ -22,7 +22,7 @@ dependencies:
22
22
  version: '0'
23
23
  type: :development
24
24
  prerelease: false
25
- version_requirements: *53734640
25
+ version_requirements: *65060540
26
26
  description: Add a caching layer to your ActiveRecord models that represent look up
27
27
  tables
28
28
  email:
@@ -38,12 +38,16 @@ files:
38
38
  - README.md
39
39
  - Rakefile
40
40
  - lib/looksy.rb
41
+ - lib/looksy/cache.rb
42
+ - lib/looksy/cache/memory.rb
43
+ - lib/looksy/cache/null.rb
41
44
  - lib/looksy/cacheable.rb
42
45
  - lib/looksy/dynamic_find_match.rb
43
- - lib/looksy/null_cache.rb
44
46
  - lib/looksy/repository.rb
45
47
  - lib/looksy/version.rb
46
48
  - looksy.gemspec
49
+ - spec/lib/cache/memory_spec.rb
50
+ - spec/lib/cache/null_spec.rb
47
51
  - spec/lib/cacheable_spec.rb
48
52
  - spec/lib/dynamic_find_match_spec.rb
49
53
  - spec/lib/repository_spec.rb
@@ -75,6 +79,8 @@ signing_key:
75
79
  specification_version: 3
76
80
  summary: Caching layer for look up tables
77
81
  test_files:
82
+ - spec/lib/cache/memory_spec.rb
83
+ - spec/lib/cache/null_spec.rb
78
84
  - spec/lib/cacheable_spec.rb
79
85
  - spec/lib/dynamic_find_match_spec.rb
80
86
  - spec/lib/repository_spec.rb
@@ -1,7 +0,0 @@
1
- module Looksy
2
- class NullCache
3
- def fetch(key, options = {}, &block)
4
- block.call
5
- end
6
- end
7
- end