Class: Glimmer::XML::Node

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

Direct Known Subclasses

HtmlNode

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Node.



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
# File 'lib/glimmer/xml/node.rb', line 34

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



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

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::XmlExpression.new, name, attributes) {@tag = super}
  @tag
end

Instance Attribute Details

#attributesObject

TODO change is_name_space to name_space?



32
33
34
# File 'lib/glimmer/xml/node.rb', line 32

def attributes
  @attributes
end

#childrenObject

TODO change is_name_space to name_space?



32
33
34
# File 'lib/glimmer/xml/node.rb', line 32

def children
  @children
end

#contentsObject

TODO change is_name_space to name_space?



32
33
34
# File 'lib/glimmer/xml/node.rb', line 32

def contents
  @contents
end

#is_attributeObject

TODO change is_name_space to name_space?



32
33
34
# File 'lib/glimmer/xml/node.rb', line 32

def is_attribute
  @is_attribute
end

#is_name_spaceObject

TODO change is_name_space to name_space?



32
33
34
# File 'lib/glimmer/xml/node.rb', line 32

def is_name_space
  @is_name_space
end

#nameObject

TODO change is_name_space to name_space?



32
33
34
# File 'lib/glimmer/xml/node.rb', line 32

def name
  @name
end

#name_spaceObject

TODO change is_name_space to name_space?



32
33
34
# File 'lib/glimmer/xml/node.rb', line 32

def name_space
  @name_space
end

#parentObject

TODO change is_name_space to name_space?



32
33
34
# File 'lib/glimmer/xml/node.rb', line 32

def parent
  @parent
end

Instance Method Details

#idObject

override Object default id method and route it to Glimmer engine



95
96
97
# File 'lib/glimmer/xml/node.rb', line 95

def id
  method_missing(:id)
end

#name_space_context?Boolean

Returns:

  • (Boolean)


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

def name_space_context?
  attributes[:_name_space_context]
end

#rubyize(text) ⇒ Object



88
89
90
91
92
# File 'lib/glimmer/xml/node.rb', line 88

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

#text_command(text) ⇒ Object



84
85
86
# File 'lib/glimmer/xml/node.rb', line 84

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

#to_xmlObject Also known as: to_s



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

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