module Rtml::DSL # Processes the given block using the RTML DSL # Examples: # tml_docs = Rtml::Document.new # tml_docs.process_block do # # scope is transferred to the element # screen :init, :next => :idle do # display # end # end # # tml_docs.process_block do |document| # # scope is maintained and element is referenced explicitly # document.screen :init, :next => :idle do |scr| # scr.display # end # # # note that these approaches are interchangeable: # document.screen :idle do # display # end # end # def process(&block) # arity == -1 is bug 574 in Ruby 1.8 -- should be 0. It's of no consequence in this case. if block.arity == 0 || block.arity == -1 then self.instance_eval &block else block.call(self) end end # Constructs and returns a new instance of Rtml::Dom::Element. If Rtml::Dom::[name]Element exists, that is built # instead. The model is added to the "elements" array (if available), but not saved. def build_element(name, properties = {}) document = self.respond_to?(:document) ? self.document : (self.respond_to?(:parent) && parent ? parent.document : self) type = begin ("Rtml::Dom::#{name.to_s.camelize}Element".constantize) rescue NameError Rtml::Dom::Element end #if new_record? || changed? element = type.new(:name => name, :document => document) if respond_to?(:elements) elements << element elsif respond_to?(:root) root = element end # element = elements.build(:name => name, :document => document, :type => type.to_s) # element.document = document if element.respond_to?(:document) element.parent = self if element.respond_to?(:parent) # else # element = type.create!(:name => name, :document => document) # if respond_to?(:elements) # elements << element # elsif respond_to?(:root) # self.root = element # end # #element = elements.create!(:name => name, :document => document, :type => type.to_s) # end #element = type.new(:name => name, :document => document) #self.elements << element if respond_to?(:elements) # element = if respond_to?(:elements) && elements.respond_to?(:build) # el = self.elements.build(:name => name, :document => document) # el.type = type.to_s # el # else # element = type.new(:name => name, :document => document) # self.elements << element if respond_to?(:elements) # element # end properties.each { |key, value| element.property key, value } if respond_to?(:root) && !self.root self.root = element end element end # backward compatibility to RTML 1 alias process_block process deprecate :process_block end