Module: Duxml::ElementGuts
- Includes:
- Duxml, LazyOx, Enumerable, Reportable
- Defined in:
- lib/duxml/doc/element.rb,
lib/duxml/doc/element.rb
Overview
contains actual methods of XML Element
Constant Summary
Constants included from Duxml
Constants included from Meta
Instance Attribute Summary
Attributes included from Duxml
Attributes included from Saxer
Instance Method Summary collapse
-
#<<(obj) ⇒ Element
this override reports changes to history; NewText for Strings, Add for elements.
-
#[]=(attr_sym, val) ⇒ Element
Self.
-
#abstract? ⇒ Boolean
Whether or not this has been written to file.
-
#delete(obj) ⇒ Element
(also: #remove)
TODO do we need this method to take Fixnum node index as well?.
-
#description ⇒ String
Self description.
-
#each(&block) ⇒ Object
traverse through just the children of this node.
-
#history ⇒ HistoryClass
History that is observing this element for changes.
-
#inspect ⇒ Object
#to_s.
-
#name_space ⇒ String
Namespace of element, derived from name e.g.
-
#stub ⇒ Element
Copy of this Element but with no children.
-
#to_s ⇒ String
XML string (overrides Ox’s to_s which just prints the object pointer).
-
#traverse(&block) ⇒ Object
pre-order traverse through this node and all of its descendants.
Methods included from LazyOx
Methods included from Reportable
Methods included from Duxml
#create, #load, #log, #relaxng, #save, #validate
Methods included from Meta
Methods included from Saxer
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class Duxml::LazyOx
Instance Method Details
#<<(obj) ⇒ Element
this override reports changes to history; NewText for Strings, Add for elements
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/duxml/doc/element.rb', line 56 def <<(obj) case when obj.is_a?(Array), obj.is_a?(NodeSet) obj.each do |e| self << e end when obj.is_a?(String) type = :NewText super(obj) else type = :Add super(obj) if nodes.last.count_observers < 1 && @observer_peers nodes.last.add_observer(@observer_peers.first.first) end end report(type, nodes.size - 1) self end |
#[]=(attr_sym, val) ⇒ Element
Returns self.
77 78 79 80 81 82 83 84 85 86 |
# File 'lib/duxml/doc/element.rb', line 77 def []=(attr_sym, val) attr = attr_sym.to_s raise "argument to [] must be a Symbol or a String." unless attr.is_a?(Symbol) or attr.is_a?(String) args = [attr] args << attributes[attr] if attributes[attr] super(attr, val) type = args.size == 1 ? :NewAttr : :ChangeAttr report(type, *args) self end |
#abstract? ⇒ Boolean
Returns whether or not this has been written to file.
47 48 49 |
# File 'lib/duxml/doc/element.rb', line 47 def abstract? line < 0 || column < 0 end |
#delete(obj) ⇒ Element Also known as: remove
TODO do we need this method to take Fixnum node index as well?
119 120 121 122 |
# File 'lib/duxml/doc/element.rb', line 119 def delete(obj) report(:Remove, @nodes.delete(obj)) obj end |
#description ⇒ String
Returns self description.
89 90 91 |
# File 'lib/duxml/doc/element.rb', line 89 def description "<#{name}>" end |
#each(&block) ⇒ Object
traverse through just the children of this node
147 148 149 |
# File 'lib/duxml/doc/element.rb', line 147 def each(&block) @nodes.each(&block) end |
#history ⇒ HistoryClass
Returns history that is observing this element for changes.
99 100 101 |
# File 'lib/duxml/doc/element.rb', line 99 def history @observer_peers.first.first if @observer_peers.respond_to?(:any?) and @observer_peers.any? and @observer_peers.first.any? end |
#inspect ⇒ Object
Returns #to_s.
112 113 114 |
# File 'lib/duxml/doc/element.rb', line 112 def inspect to_s end |
#name_space ⇒ String
Returns namespace of element, derived from name e.g. ‘<duxml:element>’ => ‘duxml’.
152 153 154 155 |
# File 'lib/duxml/doc/element.rb', line 152 def name_space return nil unless (i = name.index(':')) name[0..i-1] end |
#stub ⇒ Element
Returns copy of this Element but with no children.
94 95 96 |
# File 'lib/duxml/doc/element.rb', line 94 def stub Element.new(name, attributes) end |
#to_s ⇒ String
Returns XML string (overrides Ox’s to_s which just prints the object pointer).
104 105 106 107 108 109 |
# File 'lib/duxml/doc/element.rb', line 104 def to_s s = %(<#{name}) attributes.each do |k,v| s << %( #{k.to_s}="#{v}") end return s+'/>' if nodes.empty? s << ">#{nodes.collect do |n| n.to_s end.join}</#{name}>" end |
#traverse(&block) ⇒ Object
pre-order traverse through this node and all of its descendants
129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/duxml/doc/element.rb', line 129 def traverse(&block) return self.to_enum unless block_given? node_stack = [self] until node_stack.empty? current = node_stack.shift if current yield current node_stack = node_stack.concat(current.nodes) if current.respond_to?(:nodes) end end self if block_given? end |