Permalink
Browse files

Permitting the use of #method_missing.

  • Loading branch information...
1 parent de3409f commit 140e53ce2225233cb248eddc6ea9e79704f88432 @pvande pvande committed Mar 10, 2011
Showing with 24 additions and 1 deletion.
  1. +1 −1 lib/mustache/context.rb
  2. +19 −0 test/fixtures/method_missing.rb
  3. +4 −0 test/mustache_test.rb
View
@@ -128,7 +128,7 @@ def find(obj, key, default = nil)
elsif hash && obj.has_key?(key.to_s)
obj[key.to_s]
elsif !hash && obj.respond_to?(key)
- meth = obj.method(key)
+ meth = obj.method(key) rescue proc { obj.send(key) }
if meth.arity == 1
meth.to_proc
else
@@ -0,0 +1,19 @@
+$LOAD_PATH.unshift File.dirname(__FILE__) + '/../lib'
+require 'mustache'
+
+class MethodMissing < Mustache
+ self.template = '[ {{#list}}{{.}} {{/list}}]'
+
+ def method_missing(name, *args, &block)
+ return (0..10).to_a if name == :list
+ return super
+ end
+
+ def respond_to?(method)
+ method == :list
+ end
+end
+
+if $0 == __FILE__
+ puts MethodMissing.to_html
+end
View
@@ -546,6 +546,10 @@ def test_struct
assert_equal 'Marvin is 25', view.render
end
+ def test_method_missing
+ assert_equal('[ 0 1 2 3 4 5 6 7 8 9 10 ]', MethodMissing.render)
+ end
+
def test_custom_escaping
view = Class.new(Mustache) do
def escapeHTML(str)

0 comments on commit 140e53c

Please sign in to comment.