Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

make sure we're not confused by modules

  • Loading branch information...
commit 48c0bea04e15c7cd96fc98b0a45c54d3b77458eb 1 parent dee030c
@seamusabshere authored
Showing with 82 additions and 0 deletions.
  1. +27 −0 README.rdoc
  2. +21 −0 test/helper.rb
  3. +34 −0 test/test_cache_method.rb
View
27 README.rdoc
@@ -88,6 +88,33 @@ Note: this is NOT the same thing as <tt>#to_hash</tt>! That returns a <b><tt>Has
If you don't want to modify #hash, use #method_cache_hash instead.
+== Module methods
+
+You can put <tt>#cache_method</tt> right into your module declarations:
+
+ module MyModule
+ def my_module_method(args)
+ # [...]
+ end
+ cache_method :my_module_method
+ end
+
+ class Tiger
+ extend MyModule
+ end
+
+ class Lion
+ extend MyModule
+ end
+
+Rest assured that <tt>Tiger.my_module_method</tt> and <tt>Lion.my_module_method</tt> will be cached correctly and separately. This, on the other hand, won't work:
+
+ class Tiger
+ extend MyModule
+ # wrong - will raise NameError Exception: undefined method `my_module_method' for class `Tiger'
+ # cache_method :my_module_method
+ end
+
== Rationale
* It should be easy to cache a method using memcached, dalli (if you're on heroku), redis, etc. (that's why I made the {cache gem}[https://rubygems.org/gems/cache])
View
21 test/helper.rb
@@ -52,6 +52,22 @@ def method_cache_hash
end
end
+module Say
+ def say_count
+ @say_count ||= 0
+ end
+
+ def say_count=(x)
+ @say_count = x
+ end
+
+ def say(msg)
+ self.say_count += 1
+ msg
+ end
+ cache_method :say
+end
+
class CopyCat2
class << self
attr_writer :echo_count
@@ -137,3 +153,8 @@ class << self
cache_method :get_latest_entries
end
end
+
+CopyCat1.extend Say
+CopyCat2.extend Say
+
+CopyCat1.send :include, Say
View
34 test/test_cache_method.rb
@@ -6,6 +6,8 @@ class TestCacheMethod < Test::Unit::TestCase
def setup
Blog2.request_count = 0
CopyCat2.echo_count = 0
+ CopyCat1.say_count = 0
+ CopyCat2.say_count = 0
my_cache = Memcached.new '127.0.0.1:11211', :binary => true
my_cache.flush
CacheMethod.config.storage = my_cache
@@ -233,4 +235,36 @@ def test_method_cache_hash
a.echo 'hi'
end
end
+
+ def test_method_added_by_extension
+ assert_equal 'hi', CopyCat2.say('hi')
+ assert_equal 1, CopyCat2.say_count
+
+ assert_equal 'hi', CopyCat2.say('hi')
+ assert_equal 1, CopyCat2.say_count
+ end
+
+ def test_method_added_by_inclusion
+ a = CopyCat1.new 'sayer'
+
+ assert_equal 'hi', a.say('hi')
+ assert_equal 1, a.say_count
+
+ assert_equal 'hi', a.say('hi')
+ assert_equal 1, a.say_count
+ end
+
+ def test_not_confused_by_module
+ assert_equal 'hi', CopyCat2.say('hi')
+ assert_equal 1, CopyCat2.say_count
+
+ assert_equal 'hi', CopyCat2.say('hi')
+ assert_equal 1, CopyCat2.say_count
+
+ assert_equal 'hi', CopyCat1.say('hi')
+ assert_equal 1, CopyCat1.say_count
+
+ assert_equal 'hi', CopyCat1.say('hi')
+ assert_equal 1, CopyCat1.say_count
+ end
end
Please sign in to comment.
Something went wrong with that request. Please try again.