Module: Rtml::DSL

Defined in:
lib/rtml/dsl.rb

Instance Method Summary collapse

Instance Method Details

#build_element(name, properties = {}) ⇒ Object

Constructs and returns a new instance of Rtml::Dom::Element. If Rtml::Dom::Element exists, that is built instead. The model is added to the “elements” array (if available), but not saved.



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/rtml/dsl.rb', line 33

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

#process(&block) ⇒ Object Also known as: process_block

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


24
25
26
27
28
29
# File 'lib/rtml/dsl.rb', line 24

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