class Mustache::Context
def find(obj, key, default = nil)
key is not found.
default - An optional default value, to return if the
key - The key whose value you want.
obj - The object to perform the lookup on.
invokes that method. You get the idea.
If it's an object that responds to the key as a method call,
appropriate. If the object is a hash, does a simple hash lookup.
Finds a key in an object, using whatever method is most
def find(obj, key, default = nil) if !obj.respond_to?(:to_hash) # If a class, we need to find tags (methods) per Parser::ALLOWED_CONTENT. key = key.to_s.tr('-', '_') if key.to_s.include?('-') if obj.respond_to?(key) meth = obj.method(key) rescue proc { obj.send(key) } meth.arity == 1 ? meth.to_proc : meth[] else default end elsif obj.has_key?(key) obj[key] elsif obj.has_key?(key.to_s) obj[key.to_s] else obj[key] || default end end