-
-
Save ctrochalakis/404874 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'mustache' | |
module ExistentialMethodMissing | |
def method_missing(method, *args) | |
return super unless method.to_s =~ /\?$/ | |
val = send(method.to_s.chomp('?')) | |
val.respond_to?(:empty?) ? !val.empty? : !!val | |
end | |
def respond_to?(method) | |
super method.to_s.chomp('?') | |
end | |
end | |
class MyView < Mustache | |
include ExistentialMethodMissing | |
self.template = <<template | |
{{# people?}} | |
<ul> | |
{{# people}} | |
<li>{{ name }}</li> | |
{{/ people}} | |
</ul> | |
{{/ people?}} | |
template | |
def people | |
return [ | |
{ :name => "Chris" }, | |
{ :name => "Bob" }, | |
{ :name => "Mislav" } | |
] | |
end | |
end | |
puts MyView.render |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment