Skip to content

Instantly share code, notes, and snippets.

@aumgn
Created March 10, 2011 20:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save aumgn/864853 to your computer and use it in GitHub Desktop.
Save aumgn/864853 to your computer and use it in GitHub Desktop.
Abstractview implementation
module ViewMixin
def view_has?(key)
false
end
def view_section(key, contents)
# use #view_fetch to render section
end
# partial method, ...
end
class Hash
include ViewMixin
alias view_has? has_key?
alias view_fetch []
end
module ViewObject
include ViewMixin
alias view_has? respond_to?
def view_fetch(key)
meth = method(key)
if meth.arity.size==1
return meth.to_proc
else
return meth[]
end
end
end
#An example :
module MyHighLighterHelper
extend ViewMixin
def self.view_has?(language)
MyHighLighter.highlight?(language)
end
def self.view_fetch(language)
lambda { |text|
MyHighLighter.render(text, language)
}
end
end
myview = Object.new
myview.extend(ViewObject)
def myview.hl
MyHighLighterHelper
end
tpl = <<-END
{{#hl.ruby}}
#some ruby code
{{/hl.ruby}}
END
Mustache.render(tpl, myview)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment