fat_cache 0.0.7 → 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/VERSION +1 -1
  2. data/lib/fat_cache.rb +16 -0
  3. data/spec/fat_cache_spec.rb +41 -0
  4. metadata +1 -1
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.7
1
+ 0.0.8
data/lib/fat_cache.rb CHANGED
@@ -2,6 +2,8 @@ require 'rubygems'
2
2
  require 'ruby-debug'
3
3
 
4
4
  class FatCache
5
+ class CacheMiss < RuntimeError; end
6
+ class AmbiguousHit < RuntimeError; end
5
7
 
6
8
  class << self
7
9
  @initted = false
@@ -40,6 +42,20 @@ class FatCache
40
42
  indexed_fatcache[key][by][using]
41
43
  end
42
44
 
45
+ def one(key, spec={})
46
+ result = lookup(key, :by => spec.keys, :using => spec.values)
47
+ if result && result.length > 1
48
+ raise AmbiguousHit, "expected one record for #{key} with #{spec.inspect}, got #{result.inspect}"
49
+ end
50
+ result.first if result # makes us return nil if result is nil
51
+ end
52
+
53
+ def one!(key, spec={})
54
+ result = one(key, spec)
55
+ raise CacheMiss, "count not find find #{key} with #{spec.inspect}" unless result
56
+ result
57
+ end
58
+
43
59
  def index(key, on, &block)
44
60
  on = [*on] # ensure we're dealing with an array, we're such a friendly API!
45
61
 
@@ -105,6 +105,47 @@ describe FatCache do
105
105
  end
106
106
  end
107
107
 
108
+ describe 'one(key, spec)' do
109
+
110
+ describe 'one', :shared => true do
111
+ it 'returns one record found in the dataset specified by key, with a spec matching the spec hash' do
112
+ FatCache.set(:numbers) { [0,1,2,3,4] }
113
+ FatCache.one(:numbers, :zero? => true).should == 0
114
+ end
115
+
116
+ it 'raises an error if more than one record matches spec' do
117
+ FatCache.set(:numbers) { [0,1,2,3,4] }
118
+ lambda {
119
+ FatCache.one(:numbers, :odd? => false)
120
+ }.should raise_error(FatCache::AmbiguousHit)
121
+ end
122
+
123
+ it 'raises an error if no cache exists for key' do
124
+ lambda {
125
+ FatCache.one(:no_way, :not => 'here')
126
+ }.should raise_error(/no_way/)
127
+ end
128
+ end
129
+
130
+ it_should_behave_like 'one'
131
+
132
+ it 'returns nil if no record found in index' do
133
+ FatCache.set(:numbers) { [1,2,3,4] }
134
+ FatCache.one(:numbers, :zero? => true).should be_nil
135
+ end
136
+ end
137
+
138
+ describe 'one!(key, spec)' do
139
+ it_should_behave_like 'one'
140
+
141
+ it 'raises an error if no record found in index' do
142
+ FatCache.set(:numbers) { [1,2,3,4] }
143
+ lambda {
144
+ FatCache.one!(:numbers, :zero? => true)
145
+ }.should raise_error(FatCache::CacheMiss)
146
+ end
147
+ end
148
+
108
149
  describe 'index(key, on) { optional_block }' do
109
150
  it 'raises an error if there is no raw data to index for specified key' do
110
151
  lambda {
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fat_cache
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 0.0.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - phinze