Class: Glimmer::XML::Node

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

Constant Summary

Constants included from Glimmer

REGEX_METHODS_EXCLUDED

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Glimmer

included

Constructor Details

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

Returns a new instance of Node.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/glimmer/xml/node.rb', line 12

def initialize(parent, name, attributes, &contents)
  @is_name_space = false
  @children = []
  @parent = parent
  if attributes.is_a?(Array)
    attributes = attributes.compact
    hash_attributes = attributes.last.is_a?(Hash) ? attributes.delete(attributes.last) : {}
    hash_attributes = attributes.reduce(hash_attributes) do |hash, attribute|
      hash.merge(attribute => nil)
    end
    attributes = hash_attributes
  end
  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::Config.logger&.debug(attributes)
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



44
45
46
47
48
49
# File 'lib/glimmer/xml/node.rb', line 44

def method_missing(symbol, *args, &block)
  @is_name_space = true
  parent.children.delete(self) if parent
  Glimmer::DSL::Engine.add_content(self, Glimmer::DSL::XML::HtmlExpression.new) {@tag = super}
  @tag
end

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes.



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

def attributes
  @attributes
end

#childrenObject

Returns the value of attribute children.



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

def children
  @children
end

#contentsObject

Returns the value of attribute contents.



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

def contents
  @contents
end

#is_attributeObject

Returns the value of attribute is_attribute.



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

def is_attribute
  @is_attribute
end

#is_name_spaceObject

Returns the value of attribute is_name_space.



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

def is_name_space
  @is_name_space
end

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

#name_spaceObject

Returns the value of attribute name_space.



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

def name_space
  @name_space
end

#parentObject

Returns the value of attribute parent.



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

def parent
  @parent
end

Instance Method Details

#idObject

override Object default id method and route it to Glimmer engine



70
71
72
# File 'lib/glimmer/xml/node.rb', line 70

def id
  method_missing(:id)
end

#rubyize(text) ⇒ Object



63
64
65
66
67
# File 'lib/glimmer/xml/node.rb', line 63

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

#text_command(text) ⇒ Object



59
60
61
# File 'lib/glimmer/xml/node.rb', line 59

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

#to_xmlObject Also known as: to_html, to_s



51
52
53
54
55
# File 'lib/glimmer/xml/node.rb', line 51

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