Class: Jam::LibXML

Inherits:
Engine show all
Defined in:
lib/jam/libxml.rb

Overview

LibXML Adaptor

Instance Method Summary collapse

Methods inherited from Engine

#interpolate

Constructor Details

#initialize(*options) ⇒ LibXML

Returns a new instance of LibXML.



39
40
41
# File 'lib/jam/libxml.rb', line 39

def initialize(*options)
  @options = options
end

Instance Method Details

#append(ns, child) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/jam/libxml.rb', line 87

def append(ns, child)
  if ::LibXML::XML::XPath::Object === child
    child.each do |c|
      case ns
      when ::LibXML::XML::Node
        ns << c
      when ::LibXML::XML::XPath::Object, Array
        ns.each do |n|
          n << c
        end
      end
    end
  else
    case ns
    when ::LibXML::XML::Node
      ns << child
    when ::LibXML::XML::XPath::Object, Array
      ns.each do |n|
        n << child
      end
    end
  end
end

#attribute(ns, att, val) ⇒ Object



123
124
125
126
127
128
129
130
131
132
# File 'lib/jam/libxml.rb', line 123

def attribute(ns, att, val)
  case ns
  when ::LibXML::XML::Node
    ns.attr(att, val)
  when ::LibXML::XML::XPath::Object, Array
    ns.each do |n|
      ns.attr(att, val)
    end
  end
end

#copy(node) ⇒ Object

deep copy



65
66
67
# File 'lib/jam/libxml.rb', line 65

def copy(node)
  node.copy(true)
end

#document(source) ⇒ Object



44
45
46
# File 'lib/jam/libxml.rb', line 44

def document(source)
  ::LibXML::XML::Document.string(source, *@options)
end

#each_node(nodes) ⇒ Object

Iterate over each node.



136
137
138
139
140
141
142
143
144
145
146
# File 'lib/jam/libxml.rb', line 136

def each_node(nodes)
  case nodes
  when ::LibXML::XML::XPath::Object, Array
    nodes = nodes
  else
    nodes = [nodes]
  end
  nodes.each do |node|
    yield(node)
  end
end

#empty(ns) ⇒ Object



75
76
77
78
79
80
81
82
83
84
# File 'lib/jam/libxml.rb', line 75

def empty(ns)
  case ns
  when ::LibXML::XML::Node
    ns.content = ''
  when ::LibXML::XML::XPath::Object, Array
    ns.each do |n|
      n.content = ''
    end
  end
end

#remove(node) ⇒ Object



70
71
72
# File 'lib/jam/libxml.rb', line 70

def remove(node)
  node.remove
end

#replace(ns, child) ⇒ Object



112
113
114
115
# File 'lib/jam/libxml.rb', line 112

def replace(ns, child)
  empty(ns)
  append(ns, child)
end

#search(node, qry) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/jam/libxml.rb', line 49

def search(node, qry)
  qry = Jam.css_to_xpath(qry)
  case node
  when ::LibXML::XML::XPath::Object, Array
    a = []
    node.each do |n|
      z = n.find(qry).to_a
      a.concat(z)
    end
    a
  else
    node.find(qry)
  end
end