Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
First pass at widget cache. Also took out '&blk' parameter to #to_s s…
…ince there were no tests and it made things weird.
- Loading branch information
Showing
5 changed files
with
303 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
module Erector | ||
class Cache | ||
def initialize | ||
@stores = {} | ||
end | ||
|
||
def store_for(klass) | ||
@stores[klass] ||= {} | ||
end | ||
|
||
def []=(*args) | ||
value = args.pop | ||
klass = args.shift | ||
params = args.first || {} | ||
store_for(klass)[params] = value | ||
end | ||
|
||
def [](klass, params = {}) | ||
store_for(klass)[params] | ||
end | ||
|
||
def delete(klass, params = {}) | ||
store_for(klass).delete(params) | ||
end | ||
|
||
def delete_all(klass) | ||
@stores.delete(klass) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
require File.expand_path("#{File.dirname(__FILE__)}/../spec_helper") | ||
|
||
module CacheSpec | ||
describe Erector::Cache do | ||
before do | ||
@cache = Erector::Cache.new | ||
end | ||
|
||
class Johnny < Erector::Widget | ||
end | ||
|
||
class June < Erector::Widget | ||
end | ||
|
||
it 'caches a class with no parameters' do | ||
@cache[Johnny] = "ring of fire" | ||
@cache[Johnny].should == "ring of fire" | ||
end | ||
|
||
it 'caches two classes with no parameters' do | ||
@cache[Johnny] = "ring of fire" | ||
@cache[June] = "wildwood flower" | ||
@cache[Johnny].should == "ring of fire" | ||
@cache[June].should == "wildwood flower" | ||
end | ||
|
||
it "stores different slots for the same class with different parameters" do | ||
@cache[Johnny, {:flames => "higher"}] = "ring of fire" | ||
@cache[Johnny, {:working => "in a coal mine"}] = "my daddy died young" | ||
|
||
@cache[Johnny, {:flames => "higher"}].should == "ring of fire" | ||
@cache[Johnny, {:working => "in a coal mine"}].should == "my daddy died young" | ||
end | ||
|
||
describe 'after storing a widget with one parameter' do | ||
before do | ||
@cache[Johnny, {:flames => "higher"}] = "ring of fire" | ||
end | ||
|
||
it 'doesn\'t get it when passed the class alone' do | ||
@cache[Johnny].should be_nil | ||
end | ||
|
||
it 'doesn\'t get it when passed a different class' do | ||
@cache[June].should be_nil | ||
end | ||
|
||
it 'gets it' do | ||
@cache[Johnny, {:flames => "higher"}].should == "ring of fire" | ||
end | ||
|
||
it 'doesn\'t get it when passed a different parameter key' do | ||
@cache[Johnny, {:working => "coal mine"}].should be_nil | ||
end | ||
|
||
it 'doesn\'t get it when passed a different parameter value' do | ||
@cache[Johnny, {:flames => "lower"}].should be_nil | ||
end | ||
|
||
it 'doesn\'t get it when passed an extra parameter key' do | ||
@cache[Johnny, {:flames => "higher", :working => "coal mine"}].should be_nil | ||
end | ||
end | ||
|
||
describe 'after storing a widget with more than one parameter' do | ||
before do | ||
@cache[Johnny, {:flames => "higher", :working => "coal mine"}] = "ring of fire" | ||
end | ||
|
||
it "gets it" do | ||
@cache[Johnny, {:flames => "higher", :working => "coal mine"}].should == "ring of fire" | ||
end | ||
|
||
it 'doesn\'t get it when passed the class alone' do | ||
@cache[Johnny].should be_nil | ||
end | ||
|
||
it "doesn't get it when passed a partial parameter set" do | ||
@cache[Johnny, {:flames => "higher"}].should be_nil | ||
end | ||
|
||
it 'doesn\'t get it when passed a different class' do | ||
@cache[June].should be_nil | ||
end | ||
|
||
it 'doesn\'t get it when passed different a parameter value' do | ||
@cache[Johnny, {:flames => "lower", :working => "coal mine"}].should be_nil | ||
end | ||
|
||
it 'doesn\'t get it when passed an extra parameter key' do | ||
@cache[Johnny, {:flames => "higher", :working => "coal mine", :hear => "train a' comin'"}].should be_nil | ||
end | ||
end | ||
|
||
describe "expires" do | ||
it 'a class with no parameters' do | ||
@cache[Johnny] = "ring of fire" | ||
@cache.delete(Johnny) | ||
@cache[Johnny].should be_nil | ||
end | ||
|
||
it 'all versions of a class' do | ||
@cache[Johnny] = "i fell in" | ||
@cache[Johnny, {:flames => "higher"}] = "ring of fire" | ||
@cache[Johnny, {:working => "in a coal mine"}] = "my daddy died young" | ||
|
||
@cache.delete_all(Johnny) | ||
|
||
@cache[Johnny].should be_nil | ||
@cache[Johnny, {:flames => "higher"}].should be_nil | ||
@cache[Johnny, {:working => "in a coal mine"}].should be_nil | ||
end | ||
|
||
it '...but not other cached values' do | ||
@cache[Johnny] = "ring of fire" | ||
@cache[Johnny, {:flames => 'higher'}] = "higher fire" | ||
@cache[June] = "wildwood flower" | ||
@cache.delete(Johnny) | ||
@cache[Johnny].should be_nil | ||
@cache[Johnny, {:flames => 'higher'}].should == "higher fire" | ||
@cache[June].should == "wildwood flower" | ||
end | ||
end | ||
|
||
end | ||
end |
Oops, something went wrong.