Class: Glimmer::XML::Node

Inherits:
Object
  • Object
show all
Includes:
Glimmer, Parent
Defined in:
lib/glimmer/xml/node.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Glimmer

#add_contents, add_contents, dsl, #dsl, extended, included, logger, method_missing

Methods included from SwtPackages

included

Constructor Details

#initialize(parent, name, attributes, &contents) ⇒ Node

Returns a new instance of Node.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/glimmer/xml/node.rb', line 11

def initialize(parent, name, attributes, &contents)
  @is_name_space = false
  @children = []
  @parent = parent
  if (parent and parent.is_name_space)
    @name_space = parent
    @parent = @name_space.parent
  end
  @parent.children << self if @parent
  @name = name
  @contents = contents
  @attributes = attributes
  if @attributes
    @attributes.each_key do |attribute|
      if attribute.is_a?(Node)
        attribute.is_attribute = true
        attribute.parent.children.delete(attribute) if attribute.parent
        attribute.parent = nil #attributes do not usually have parents
      end
    end
    Glimmer.logger.debug(attributes)
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(symbol, *args, &block) ⇒ Object



35
36
37
38
39
40
# File 'lib/glimmer/xml/node.rb', line 35

def method_missing(symbol, *args, &block)
  @is_name_space = true
  parent.children.delete(self) if parent
  Glimmer.add_contents(self) {@tag = super}
  @tag
end

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes.



9
10
11
# File 'lib/glimmer/xml/node.rb', line 9

def attributes
  @attributes
end

#childrenObject

Returns the value of attribute children.



9
10
11
# File 'lib/glimmer/xml/node.rb', line 9

def children
  @children
end

#contentsObject

Returns the value of attribute contents.



9
10
11
# File 'lib/glimmer/xml/node.rb', line 9

def contents
  @contents
end

#is_attributeObject

Returns the value of attribute is_attribute.



9
10
11
# File 'lib/glimmer/xml/node.rb', line 9

def is_attribute
  @is_attribute
end

#is_name_spaceObject

Returns the value of attribute is_name_space.



9
10
11
# File 'lib/glimmer/xml/node.rb', line 9

def is_name_space
  @is_name_space
end

#nameObject

Returns the value of attribute name.



9
10
11
# File 'lib/glimmer/xml/node.rb', line 9

def name
  @name
end

#name_spaceObject

Returns the value of attribute name_space.



9
10
11
# File 'lib/glimmer/xml/node.rb', line 9

def name_space
  @name_space
end

#parentObject

Returns the value of attribute parent.



9
10
11
# File 'lib/glimmer/xml/node.rb', line 9

def parent
  @parent
end

Instance Method Details

#idObject

override Object default id method and route it to Glimmer engine



78
79
80
# File 'lib/glimmer/xml/node.rb', line 78

def id
  method_missing(:id)
end

#process_block(block) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/glimmer/xml/node.rb', line 48

def process_block(block)
  return_value = block.call(@widget)
  if return_value.is_a?(String) and !@children.include?(return_value)
    text = return_value
    first_match = text.match(/[#][^{]+[{][^}]+[}]/)
    match = first_match
    while (match)
      Glimmer.module_eval(text_command(match.pre_match))
      tag_text = match.to_s
      Glimmer.module_eval(rubyize(tag_text))
      text = tag_text
      post_match = match.post_match
      match = text.match(/[#]\w+[{]\w+[}]/)
    end
    Glimmer.module_eval(text_command(post_match)) if post_match
    @children << return_value unless first_match
  end
end

#rubyize(text) ⇒ Object



71
72
73
74
75
# File 'lib/glimmer/xml/node.rb', line 71

def rubyize(text)
text = text.gsub(/[}]/, '"}')
text = text.gsub(/[{]/, '{"')
  text = text.gsub(/[#]/, '')
end

#text_command(text) ⇒ Object



67
68
69
# File 'lib/glimmer/xml/node.rb', line 67

def text_command(text)
  "text \"#{text}\""
end

#to_xmlObject



42
43
44
45
46
# File 'lib/glimmer/xml/node.rb', line 42

def to_xml
  xml_visitor = XmlVisitor.new
  DepthFirstSearchIterator.new(self, xml_visitor).iterate
  xml_visitor.document
end