Skip to content

Commit

Permalink
Merge pull request mustache#223 from nahiluhmot/add-support-for-defau…
Browse files Browse the repository at this point in the history
…lt-procs

Hashes with default_procs may lazily fetch values.
  • Loading branch information
locks committed Oct 10, 2016
2 parents 3c7af8f + c5f4261 commit 9e5a149
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/mustache/context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ def current
def find_in_hash(obj, key, default)
return obj[key] if obj.has_key?(key)
return obj[key.to_s] if obj.has_key?(key.to_s)
return obj[key] if obj.respond_to?(:default_proc) && obj.default_proc && obj[key]

# If default is :__missing then we are from #fetch which is hunting through the stack
# If default is nil then we are reducing dot notation
Expand Down
17 changes: 17 additions & 0 deletions test/mustache_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -724,6 +724,23 @@ def test_inherited_attributes
end
end

def test_hash_default_proc
template = <<template
{{greetings.Peter}}
{{greetings.Paul}}
{{greetings.Mary}}
template
data = {
'greetings' => Hash.new { |hash, key| hash[key] = "Hello, #{key}!" }
}

assert_equal <<expected, Mustache.render(template, data)
Hello, Peter!
Hello, Paul!
Hello, Mary!
expected
end

def test_array_of_arrays
template = <<template
{{#items}}
Expand Down

0 comments on commit 9e5a149

Please sign in to comment.