Class: Moxml::NodeSet
- Inherits:
-
Object
- Object
- Moxml::NodeSet
- Includes:
- Enumerable
- Defined in:
- lib/moxml/node_set.rb
Instance Attribute Summary collapse
-
#context ⇒ Object
readonly
Returns the value of attribute context.
-
#nodes ⇒ Object
readonly
Returns the value of attribute nodes.
Instance Method Summary collapse
- #+(other) ⇒ Object
- #<<(node) ⇒ Object (also: #push)
- #==(other) ⇒ Object
- #[](index) ⇒ Object
-
#delete(node) ⇒ Object
Delete a node from the set Accepts both wrapped Moxml nodes and native nodes.
- #each ⇒ Object
- #empty? ⇒ Boolean
- #first ⇒ Object
-
#initialize(nodes, context) ⇒ NodeSet
constructor
A new instance of NodeSet.
- #last ⇒ Object
- #remove ⇒ Object
- #size ⇒ Object (also: #length)
- #text ⇒ Object
- #to_a ⇒ Object
-
#uniq_by_native ⇒ Object
Deduplicate nodes based on native object identity This is crucial for XPath operations like descendant-or-self which may yield the same native node multiple times.
Constructor Details
#initialize(nodes, context) ⇒ NodeSet
Returns a new instance of NodeSet.
9 10 11 12 |
# File 'lib/moxml/node_set.rb', line 9 def initialize(nodes, context) @nodes = Array(nodes) @context = context end |
Instance Attribute Details
#context ⇒ Object (readonly)
Returns the value of attribute context.
7 8 9 |
# File 'lib/moxml/node_set.rb', line 7 def context @context end |
#nodes ⇒ Object (readonly)
Returns the value of attribute nodes.
7 8 9 |
# File 'lib/moxml/node_set.rb', line 7 def nodes @nodes end |
Instance Method Details
#+(other) ⇒ Object
51 52 53 |
# File 'lib/moxml/node_set.rb', line 51 def +(other) self.class.new(nodes + other.nodes, context) end |
#<<(node) ⇒ Object Also known as: push
55 56 57 58 59 60 |
# File 'lib/moxml/node_set.rb', line 55 def <<(node) # If it's a wrapped Moxml node, unwrap to native before storing native_node = node.respond_to?(:native) ? node.native : node @nodes << native_node self end |
#==(other) ⇒ Object
80 81 82 83 84 85 86 |
# File 'lib/moxml/node_set.rb', line 80 def ==(other) self.class == other.class && length == other.length && nodes.each_with_index.all? do |node, index| Node.wrap(node, context) == other[index] end end |
#[](index) ⇒ Object
21 22 23 24 25 26 27 28 |
# File 'lib/moxml/node_set.rb', line 21 def [](index) case index when Integer Node.wrap(nodes[index], context) when Range NodeSet.new(nodes[index], context) end end |
#delete(node) ⇒ Object
Delete a node from the set Accepts both wrapped Moxml nodes and native nodes
99 100 101 102 103 104 |
# File 'lib/moxml/node_set.rb', line 99 def delete(node) # If it's a wrapped Moxml node, unwrap to native native_node = node.respond_to?(:native) ? node.native : node @nodes.delete(native_node) self end |
#each ⇒ Object
14 15 16 17 18 19 |
# File 'lib/moxml/node_set.rb', line 14 def each return to_enum(:each) unless block_given? nodes.each { |node| yield Node.wrap(node, context) } self end |
#empty? ⇒ Boolean
38 39 40 |
# File 'lib/moxml/node_set.rb', line 38 def empty? nodes.empty? end |
#first ⇒ Object
30 31 32 |
# File 'lib/moxml/node_set.rb', line 30 def first Node.wrap(nodes.first, context) end |
#last ⇒ Object
34 35 36 |
# File 'lib/moxml/node_set.rb', line 34 def last Node.wrap(nodes.last, context) end |
#remove ⇒ Object
92 93 94 95 |
# File 'lib/moxml/node_set.rb', line 92 def remove each(&:remove) self end |
#size ⇒ Object Also known as: length
42 43 44 |
# File 'lib/moxml/node_set.rb', line 42 def size nodes.size end |
#text ⇒ Object
88 89 90 |
# File 'lib/moxml/node_set.rb', line 88 def text map(&:text).join end |
#to_a ⇒ Object
47 48 49 |
# File 'lib/moxml/node_set.rb', line 47 def to_a map { |node| node } end |
#uniq_by_native ⇒ Object
Deduplicate nodes based on native object identity This is crucial for XPath operations like descendant-or-self which may yield the same native node multiple times
66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/moxml/node_set.rb', line 66 def uniq_by_native seen = {} unique_natives = @nodes.select do |native| id = native.object_id if seen[id] false else seen[id] = true true end end self.class.new(unique_natives, context) end |