Created
March 10, 2011 20:23
-
-
Save aumgn/864853 to your computer and use it in GitHub Desktop.
Abstractview implementation
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
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