Class: Jam::REXML

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

Overview

REXML Adaptor

Instance Method Summary collapse

Methods inherited from Engine

#interpolate

Constructor Details

#initialize(*options) ⇒ REXML

Returns a new instance of REXML.



38
39
40
# File 'lib/jam/rexml.rb', line 38

def initialize(*options)
  @options = options
end

Instance Method Details

#append(ns, child) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/jam/rexml.rb', line 76

def append(ns, child)
  if Array === child
    case ns
    when ::REXML::Element
      ns.add_element(child)
    when Array
      ns.each do |n|
        nadd_element(child)
      end
    end
  else
    case ns
    when ::REXML::Element
      ns.add_element(child)
    when Array
      ns.each do |n|
        n.add_element(child)
      end
    end
  end
end

#attribute(ns, att, val) ⇒ Object



110
111
112
113
114
115
116
117
118
119
# File 'lib/jam/rexml.rb', line 110

def attribute(ns, att, val)
  case ns
  when ::REXML::Element
    ns.attr(att, val)
  when Array
    ns.each do |n|
      ns.attr(att, val)
    end
  end
end

#copy(node) ⇒ Object

deep copy



54
55
56
# File 'lib/jam/rexml.rb', line 54

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

#document(source) ⇒ Object



43
44
45
# File 'lib/jam/rexml.rb', line 43

def document(source)
  ::REXML::Document.new(source, *@options)
end

#each_node(nodes) ⇒ Object

Iterate over each node.



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

def each_node(nodes)
  unless Array === nodes
    nodes = [nodes]
  end
  nodes.each do |node|
    yield(node)
  end
end

#empty(ns) ⇒ Object



64
65
66
67
68
69
70
71
72
73
# File 'lib/jam/rexml.rb', line 64

def empty(ns)
  case ns
  when ::REXML::Element
    ns.inner_html = ''
  when Array
    ns.each do |n|
      n.inner_html = ''
    end
  end
end

#remove(node) ⇒ Object



59
60
61
# File 'lib/jam/rexml.rb', line 59

def remove(node)
  node.remove
end

#replace(ns, child) ⇒ Object



99
100
101
102
# File 'lib/jam/rexml.rb', line 99

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

#search(node, qry) ⇒ Object



48
49
50
51
# File 'lib/jam/rexml.rb', line 48

def search(node, qry)
  qry = Jam.css_to_xpath(qry)
  ::REXML::XPath.match(node, qry)
end