forked from ragalie/lisbn
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Made method caching less obtrusive and correct if the object changes
- Loading branch information
Mike Ragalie
committed
May 12, 2012
1 parent
b554ede
commit 9c86cf3
Showing
5 changed files
with
72 additions
and
27 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
require "active_support/core_ext/hash" | ||
require "lisbn/cache_method" | ||
require "lisbn/lisbn" |
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,15 @@ | ||
class Lisbn < String | ||
module CacheMethod | ||
def cache_method(*methods) | ||
methods.map(&:to_s).each do |method| | ||
alias_method method + "_without_cache", method | ||
define_method method do |*args, &blk| | ||
@cache ||= {} | ||
@cache[[method, self]] ||= send(method + "_without_cache", *args, &blk) | ||
end | ||
end | ||
end | ||
end | ||
|
||
extend CacheMethod | ||
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,27 @@ | ||
require 'spec_helper' | ||
|
||
class String | ||
extend Lisbn::CacheMethod | ||
|
||
def with_excitement! | ||
self + "!" | ||
end | ||
|
||
cache_method :with_excitement! | ||
end | ||
|
||
describe "cache_method" do | ||
subject { String.new("awesomeness") } | ||
|
||
it "evaluates the method the first time but not subsequent times" do | ||
subject.should_receive(:+).with("!").once.and_return("you got stubbed!") | ||
subject.with_excitement! | ||
subject.with_excitement!.should == "you got stubbed!" | ||
end | ||
|
||
it "reevaluates the method if the object's hash changes" do | ||
subject.with_excitement!.should == "awesomeness!" | ||
subject.replace("more awesomeness") | ||
subject.with_excitement!.should == "more awesomeness!" | ||
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