Class: Itonoko::XML::NodeSet
- Inherits:
-
Object
- Object
- Itonoko::XML::NodeSet
- Includes:
- Enumerable
- Defined in:
- lib/itonoko/xml/node_set.rb
Instance Attribute Summary collapse
-
#document ⇒ Object
readonly
Returns the value of attribute document.
Instance Method Summary collapse
- #&(other) ⇒ Object
- #+(other) ⇒ Object (also: #|)
- #-(other) ⇒ Object
- #[](index) ⇒ Object
- #at_css(selector) ⇒ Object
- #at_xpath(expr, namespaces = {}) ⇒ Object
- #attr(name) ⇒ Object
- #attribute(name) ⇒ Object
- #css(selector) ⇒ Object
- #each(&block) ⇒ Object
- #empty? ⇒ Boolean
- #first(n = nil) ⇒ Object
-
#initialize(document, nodes = []) ⇒ NodeSet
constructor
A new instance of NodeSet.
- #inner_html ⇒ Object
- #inspect ⇒ Object
- #last(n = nil) ⇒ Object
- #length ⇒ Object (also: #size, #count)
- #push(node) ⇒ Object (also: #<<)
- #remove ⇒ Object (also: #unlink)
- #text ⇒ Object (also: #inner_text)
- #to_a ⇒ Object
- #to_html ⇒ Object (also: #to_s)
- #to_xml(options = {}) ⇒ Object
- #wrap(markup) ⇒ Object
- #xpath(expr, namespaces = {}) ⇒ Object
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
#document ⇒ Object (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
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_html ⇒ Object
107 108 109 |
# File 'lib/itonoko/xml/node_set.rb', line 107 def inner_html map(&:inner_html).join end |
#inspect ⇒ Object
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 |
#length ⇒ Object 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 |
#remove ⇒ Object Also known as: unlink
111 112 113 114 |
# File 'lib/itonoko/xml/node_set.rb', line 111 def remove each(&:remove) self end |
#text ⇒ Object Also known as: inner_text
93 94 95 |
# File 'lib/itonoko/xml/node_set.rb', line 93 def text map(&:text).join end |
#to_a ⇒ Object
130 131 132 |
# File 'lib/itonoko/xml/node_set.rb', line 130 def to_a @nodes.dup end |
#to_html ⇒ Object 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( = {}) map { |n| n.to_xml() }.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 |