Created
November 24, 2009 21:17
-
-
Save defunkt/242228 to your computer and use it in GitHub Desktop.
Mustache method_missing hack
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' | |
class Ranger < Mustache | |
self.template = <<-template | |
{{#range_0_to_10}} | |
<option value="{{i}}">{{i}}</option> | |
{{/range_0_to_10}} | |
template | |
def range(first, last) | |
(first..last).map { |i| {:i => i} } | |
end | |
def method_missing(name, *args, &block) | |
return super unless name.to_s =~ /^range_(\d+)_to_(\d+)/ | |
range($1, $2) | |
end | |
def respond_to?(method) | |
method.to_s =~ /^range_(\d+)_to_(\d+)/ | |
end | |
end | |
puts Ranger.render |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment