Class: Itonoko::XML::NodeSet

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/itonoko/xml/node_set.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(document, nodes = []) ⇒ NodeSet

Returns a new instance of NodeSet.



10
11
12
13
# File 'lib/itonoko/xml/node_set.rb', line 10

def initialize(document, nodes = [])
  @document = document
  @nodes    = nodes.to_a
end

Instance Attribute Details

#documentObject (readonly)

Returns the value of attribute document.



8
9
10
# File 'lib/itonoko/xml/node_set.rb', line 8

def document
  @document
end

Instance Method Details

#&(other) ⇒ Object



64
65
66
67
# File 'lib/itonoko/xml/node_set.rb', line 64

def &(other)
  other_ids = other.to_a.map(&:object_id).to_set
  NodeSet.new(document, @nodes.select { |n| other_ids.include?(n.object_id) })
end

#+(other) ⇒ Object Also known as: |



47
48
49
50
51
52
53
54
55
56
# File 'lib/itonoko/xml/node_set.rb', line 47

def +(other)
  seen = {}
  combined = (@nodes + other.to_a).each_with_object([]) do |n, arr|
    unless seen[n.object_id]
      seen[n.object_id] = true
      arr << n
    end
  end
  NodeSet.new(document, combined)
end

#-(other) ⇒ Object



59
60
61
62
# File 'lib/itonoko/xml/node_set.rb', line 59

def -(other)
  other_ids = other.to_a.map(&:object_id).to_set
  NodeSet.new(document, @nodes.reject { |n| other_ids.include?(n.object_id) })
end

#[](index) ⇒ Object



25
26
27
# File 'lib/itonoko/xml/node_set.rb', line 25

def [](index)
  @nodes[index]
end

#at_css(selector) ⇒ Object



77
78
79
80
81
82
83
# File 'lib/itonoko/xml/node_set.rb', line 77

def at_css(selector)
  each do |n|
    result = n.at_css(selector)
    return result if result
  end
  nil
end

#at_xpath(expr, namespaces = {}) ⇒ Object



85
86
87
88
89
90
91
# File 'lib/itonoko/xml/node_set.rb', line 85

def at_xpath(expr, namespaces = {})
  each do |n|
    result = n.at_xpath(expr, namespaces)
    return result if result
  end
  nil
end

#attr(name) ⇒ Object



122
123
124
# File 'lib/itonoko/xml/node_set.rb', line 122

def attr(name)
  first&.[](name)
end

#attribute(name) ⇒ Object



126
127
128
# File 'lib/itonoko/xml/node_set.rb', line 126

def attribute(name)
  first&.attribute(name)
end

#css(selector) ⇒ Object



69
70
71
# File 'lib/itonoko/xml/node_set.rb', line 69

def css(selector)
  NodeSet.new(document, flat_map { |n| n.css(selector).to_a })
end

#each(&block) ⇒ Object



43
44
45
# File 'lib/itonoko/xml/node_set.rb', line 43

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

#empty?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/itonoko/xml/node_set.rb', line 21

def empty?
  @nodes.empty?
end

#first(n = nil) ⇒ Object



29
30
31
# File 'lib/itonoko/xml/node_set.rb', line 29

def first(n = nil)
  n ? @nodes.first(n) : @nodes.first
end

#inner_htmlObject



107
108
109
# File 'lib/itonoko/xml/node_set.rb', line 107

def inner_html
  map(&:inner_html).join
end

#inspectObject



134
135
136
# File 'lib/itonoko/xml/node_set.rb', line 134

def inspect
  "#<#{self.class} [#{@nodes.map(&:inspect).join(', ')}]>"
end

#last(n = nil) ⇒ Object



33
34
35
# File 'lib/itonoko/xml/node_set.rb', line 33

def last(n = nil)
  n ? @nodes.last(n) : @nodes.last
end

#lengthObject Also known as: size, count



15
16
17
# File 'lib/itonoko/xml/node_set.rb', line 15

def length
  @nodes.length
end

#push(node) ⇒ Object Also known as: <<



37
38
39
40
# File 'lib/itonoko/xml/node_set.rb', line 37

def push(node)
  @nodes.push(node)
  self
end

#removeObject Also known as: unlink



111
112
113
114
# File 'lib/itonoko/xml/node_set.rb', line 111

def remove
  each(&:remove)
  self
end

#textObject Also known as: inner_text



93
94
95
# File 'lib/itonoko/xml/node_set.rb', line 93

def text
  map(&:text).join
end

#to_aObject



130
131
132
# File 'lib/itonoko/xml/node_set.rb', line 130

def to_a
  @nodes.dup
end

#to_htmlObject Also known as: to_s



98
99
100
# File 'lib/itonoko/xml/node_set.rb', line 98

def to_html
  map(&:to_html).join
end

#to_xml(options = {}) ⇒ Object



103
104
105
# File 'lib/itonoko/xml/node_set.rb', line 103

def to_xml(options = {})
  map { |n| n.to_xml(options) }.join
end

#wrap(markup) ⇒ Object



117
118
119
120
# File 'lib/itonoko/xml/node_set.rb', line 117

def wrap(markup)
  each { |node| node.wrap(markup) }
  self
end

#xpath(expr, namespaces = {}) ⇒ Object



73
74
75
# File 'lib/itonoko/xml/node_set.rb', line 73

def xpath(expr, namespaces = {})
  NodeSet.new(document, flat_map { |n| n.xpath(expr, namespaces).to_a })
end