Created
July 29, 2011 14:22
-
-
Save jeroenvandijk/1113898 to your computer and use it in GitHub Desktop.
Hypertemplate hack
This file contains hidden or 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
| class Hypertemplate::Builder::Json | |
| def members(options = {}, &block) | |
| collection = options[:collection] || @obj | |
| raise Hypertemplate::BuilderError.new("Members method require a collection to execute") unless collection.respond_to?(:each) | |
| # Start hack | |
| root = options[:root] || "members" if options[:root] != false | |
| if root | |
| add_to(@current, root, []) | |
| else | |
| root = @raw.keys.first | |
| @raw = { root => [] } | |
| @current = @raw | |
| end | |
| # /end of hack | |
| collection.each do |member| | |
| parent = @current | |
| @current = {} | |
| if block.arity==1 | |
| # new dsl | |
| block.call(member) | |
| else | |
| # old dsl (deprecate at 1.3?) | |
| block.call(self, member) | |
| end | |
| add_to(parent, root, @current) | |
| @current = parent | |
| end | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment