lib/compositor/composite.rb in compositor-0.1.1 vs lib/compositor/composite.rb in compositor-0.1.3

- old
+ new

@@ -1,53 +1,42 @@ module Compositor class Composite < Compositor::Base attr_accessor :collection, :renderer - def initialize(view_context, *args) + def initialize(view_context, args = {}) super + @collection_set = true if args.has_key?(:collection) self.collection ||= [] end def to_hash with_root_element do renderer.new(self, collection).render end end - def map_collection - self.collection = self.collection.map do |item| - yield item - end - end - def composite? true end - def self.inherited(subclass) - method_name = subclass.name.gsub(/.*::/, '').underscore - unless method_name.eql?("base") - Compositor::DSL.send(:define_method, method_name) do |args = {}, &block| - original_generator = self.generator - composite = subclass.new(self.view_context, args) + def dsl(dsl, &block) + original_generator = dsl.generator - self.generator = composite + dsl.generator = self - if args[:collection] && block - # reset collection, we'll be mapping it via a block - composite.collection = [] - args[:collection].each do |object| - self.instance_exec(object, &block) - end - elsif block - self.instance_eval &block - end - - if original_generator - self.generator = original_generator - self.generator.collection << composite - end + if self.collection && @collection_set && block + # reset collection, we'll be mapping it via a block + unmapped_collection = collection + self.collection = [] + unmapped_collection.each do |object| + dsl.instance_exec(object, &block) end + elsif block + dsl.instance_eval &block end + + dsl.generator = original_generator if original_generator + + dsl.generator.collection << self if dsl.generator != self end end end