Skip to content

Commit

Permalink
Merge pull request #218 from wbgs/master
Browse files Browse the repository at this point in the history
Allow hyphens in tags
  • Loading branch information
locks committed Mar 24, 2016
2 parents ec918d1 + 5430fb5 commit b5d2065
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 10 deletions.
13 changes: 5 additions & 8 deletions lib/mustache/context.rb
Expand Up @@ -138,8 +138,11 @@ def fetch(name, default = :__raise)
def find(obj, key, default = nil)
return find_in_hash(obj.to_hash, key, default) if obj.respond_to?(:to_hash)

key = to_tag(key)
return default unless obj.respond_to?(key)
unless obj.respond_to?(key)
# no match for the key, but it may include a hyphen, so try again replacing hyphens with underscores.
key = key.to_s.tr('-', '_')
return default unless obj.respond_to?(key)
end

meth = obj.method(key) rescue proc { obj.send(key) }
meth.arity == 1 ? meth.to_proc : meth.call
Expand All @@ -152,12 +155,6 @@ def current

private


# If a class, we need to find tags (methods) per Parser::ALLOWED_CONTENT.
def to_tag key
key.to_s.include?('-') ? key.to_s.tr('-', '_') : key
end

# Fetches a hash key if it exists, or returns the given default.
def find_in_hash(obj, key, default)
return obj[key] if obj.has_key?(key)
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/liberal.mustache
@@ -1 +1 @@
{{first-name}} {{middle_name!}} {{lastName?}}
{{first-name}} {{middle_name!}} {{lastName?}} {{street-address}}
4 changes: 4 additions & 0 deletions test/fixtures/liberal.rb
Expand Up @@ -14,6 +14,10 @@ def middle_name!
def lastName?
'sheurs'
end

define_method :'street-address' do
'123 Somewhere St'
end
end

if $0 == __FILE__
Expand Down
2 changes: 1 addition & 1 deletion test/mustache_test.rb
Expand Up @@ -541,7 +541,7 @@ def test_liberal_tag_names

def test_liberal_tag_names_in_class
assert_equal <<-end_liberal, Liberal.render
kevin j sheurs
kevin j sheurs 123 Somewhere St
end_liberal
end

Expand Down

0 comments on commit b5d2065

Please sign in to comment.