fat_cache 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/lib/fat_cache.rb +16 -5
- data/spec/fat_cache_spec.rb +29 -3
- metadata +2 -2
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.5
|
data/lib/fat_cache.rb
CHANGED
@@ -45,7 +45,7 @@ class FatCache
|
|
45
45
|
return indexed_fatcache[key][by][using]
|
46
46
|
end
|
47
47
|
|
48
|
-
def index(key, on)
|
48
|
+
def index(key, on, &block)
|
49
49
|
# must have cache data to work with
|
50
50
|
ensure_cached(key)
|
51
51
|
|
@@ -56,10 +56,21 @@ class FatCache
|
|
56
56
|
indexed_fatcache[key] = {} unless indexed_fatcache.has_key?(key)
|
57
57
|
|
58
58
|
raw_data = get(key)
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
59
|
+
|
60
|
+
if block
|
61
|
+
# make the cache available to the passed block, and ensure that
|
62
|
+
# the returned value is wrapped in an array (to keep these keys in line
|
63
|
+
# with the method sending strategy
|
64
|
+
wrapped_block = lambda { |item| [*block.call(self, item)] }
|
65
|
+
|
66
|
+
# pass each element of the raw data set into the block, which will
|
67
|
+
# compute the key for us
|
68
|
+
indexed_fatcache[key][on] = raw_data.group_by(&wrapped_block)
|
69
|
+
else
|
70
|
+
# call each method specified in the `on` array once on each element in
|
71
|
+
# the raw dataset, and use the results of those calls to key this index
|
72
|
+
indexed_fatcache[key][on] = raw_data.group_by { |x| on.map { |b| x.send(b) } }
|
73
|
+
end
|
63
74
|
end
|
64
75
|
|
65
76
|
def get_index(key, on)
|
data/spec/fat_cache_spec.rb
CHANGED
@@ -83,7 +83,7 @@ describe FatCache do
|
|
83
83
|
end
|
84
84
|
end
|
85
85
|
|
86
|
-
describe 'index(key, on)' do
|
86
|
+
describe 'index(key, on) { optional_block }' do
|
87
87
|
it 'raises an error if there is no raw data to index for specified key' do
|
88
88
|
lambda {
|
89
89
|
FatCache.index(:wishbone, :whats_the_story?)
|
@@ -99,19 +99,45 @@ describe FatCache do
|
|
99
99
|
|
100
100
|
describe 'given a dataset which responds to methods' do
|
101
101
|
before do
|
102
|
-
fruit = [
|
102
|
+
@fruit = [
|
103
103
|
stub(:mango, :grams_of_awesome => 3),
|
104
104
|
stub(:banana, :grams_of_awesome => 10),
|
105
105
|
stub(:apple, :grams_of_awesome => 3)
|
106
106
|
]
|
107
|
-
FatCache.store(:fruit, fruit)
|
107
|
+
FatCache.store(:fruit, @fruit)
|
108
108
|
end
|
109
|
+
|
109
110
|
it 'calls each method specified in `on` array and uses the results as the index key' do
|
110
111
|
FatCache.index(:fruit, [:grams_of_awesome])
|
111
112
|
index = FatCache.get_index(:fruit, :grams_of_awesome)
|
112
113
|
index.keys.should =~ [[3],[10]]
|
113
114
|
end
|
115
|
+
|
116
|
+
describe 'when the optional block is specified' do
|
117
|
+
it 'does not send the symbols specified for `on` to the dataset members' do
|
118
|
+
FatCache.get(:fruit).each { |f| f.should_not_receive(:grams_of_awesome) }
|
119
|
+
FatCache.index(:fruit, :grams_of_awesome) { :dont_care }
|
120
|
+
end
|
121
|
+
|
122
|
+
it 'indexes the dataset based on the return value of the block' do
|
123
|
+
FatCache.index(:fruit, :grams_of_awesome) { :so_many }
|
124
|
+
results = FatCache.lookup(:fruit, :by => :grams_of_awesome, :using => :so_many)
|
125
|
+
results.should =~ @fruit
|
126
|
+
end
|
127
|
+
|
128
|
+
it 'passes itself into the block as the first argument' do
|
129
|
+
FatCache.index(:fruit, :grams_of_awesome) { |cache, _|
|
130
|
+
cache.should == FatCache
|
131
|
+
}
|
132
|
+
end
|
133
|
+
|
134
|
+
it 'passes each item into the block as the second argument' do
|
135
|
+
FatCache.get(:fruit).each { |f| f.should_receive(:seen) }
|
136
|
+
FatCache.index(:fruit, :grams_of_awesome) { |_, item| item.seen }
|
137
|
+
end
|
138
|
+
end
|
114
139
|
end
|
140
|
+
|
115
141
|
end
|
116
142
|
|
117
143
|
describe 'invalidate(key)' do
|
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.
|
4
|
+
version: 0.0.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- phinze
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2010-03-
|
12
|
+
date: 2010-03-03 00:00:00 -06:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|