Module: Duxml::ElementGuts

Includes:
Duxml, LazyOx, Enumerable, Reportable
Included in:
Doc, Element
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

DITA_GRAMMAR

Constants included from Meta

Meta::FILE_EXT

Instance Attribute Summary

Attributes included from Duxml

#doc, #file, #meta

Attributes included from Saxer

#io

Instance Method Summary collapse

Methods included from LazyOx

#method_missing

Methods included from Reportable

#add_observer

Methods included from Duxml

#create, #load, #log, #relaxng, #save, #validate

Methods included from Meta

#grammar=, meta_path, xml

Methods included from Saxer

#sax

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

Parameters:

  • obj (Element)

    element or string to add to this Element

Returns:

See Also:

  • Ox::Element#<<


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.

Parameters:

  • attr_sym (String, Symbol)

    name of attribute

  • val (String)

Returns:



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.

Returns:

  • (Boolean)

    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?

Parameters:

  • obj (Element)

    element child to delete

Returns:



119
120
121
122
# File 'lib/duxml/doc/element.rb', line 119

def delete(obj)
  report(:Remove, @nodes.delete(obj))
  obj
end

#descriptionString

Returns self description.

Returns:

  • (String)

    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

Parameters:

  • &block (block)

    code to execute for each child node



147
148
149
# File 'lib/duxml/doc/element.rb', line 147

def each(&block)
  @nodes.each(&block)
end

#historyHistoryClass

Returns history that is observing this element for changes.

Returns:

  • (HistoryClass)

    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

#inspectObject

Returns #to_s.

Returns:

  • #to_s



112
113
114
# File 'lib/duxml/doc/element.rb', line 112

def inspect
  to_s
end

#name_spaceString

Returns namespace of element, derived from name e.g. ‘<duxml:element>’ => ‘duxml’.

Returns:

  • (String)

    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

#stubElement

Returns copy of this Element but with no children.

Returns:

  • (Element)

    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_sString

Returns XML string (overrides Ox’s to_s which just prints the object pointer).

Returns:

  • (String)

    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

Parameters:

  • &block (block)

    code to execute for each yielded node



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