Skip to content
This repository has been archived by the owner on May 6, 2019. It is now read-only.

Commit

Permalink
Allows Hash default proc to work in context.
Browse files Browse the repository at this point in the history
  • Loading branch information
trans committed Nov 22, 2011
1 parent 228bea9 commit 8617742
Showing 1 changed file with 21 additions and 11 deletions.
32 changes: 21 additions & 11 deletions lib/mustache/context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -121,22 +121,32 @@ def fetch(name, default = :__raise)
#
# Returns the value of key in obj if it is found and default otherwise.
def find(obj, key, default = nil)
hash = obj.respond_to?(:has_key?)

if hash && obj.has_key?(key)
hash = obj.respond_to?(:to_hash)

if !hash
if obj.respond_to?(key)
meth = obj.method(key) rescue proc { obj.send(key) }
if meth.arity == 1
meth.to_proc
else
meth[]
end
else
default
end
elsif hash && obj.has_key?(key)
obj[key]
elsif hash && obj.has_key?(key.to_s)
obj[key.to_s]
elsif !hash && obj.respond_to?(key)
meth = obj.method(key) rescue proc { obj.send(key) }
if meth.arity == 1
meth.to_proc
else
meth[]
end
else
default
obj[key] || default
end
end
end
end

class Hash
def to_hash
self
end unless method_defined?(:to_hash)
end

0 comments on commit 8617742

Please sign in to comment.