Class: Moxml::NodeSet

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

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#contextObject (readonly)

Returns the value of attribute context.



7
8
9
# File 'lib/moxml/node_set.rb', line 7

def context
  @context
end

#nodesObject (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

#eachObject



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

Returns:

  • (Boolean)


38
39
40
# File 'lib/moxml/node_set.rb', line 38

def empty?
  nodes.empty?
end

#firstObject



30
31
32
# File 'lib/moxml/node_set.rb', line 30

def first
  Node.wrap(nodes.first, context)
end

#lastObject



34
35
36
# File 'lib/moxml/node_set.rb', line 34

def last
  Node.wrap(nodes.last, context)
end

#removeObject



92
93
94
95
# File 'lib/moxml/node_set.rb', line 92

def remove
  each(&:remove)
  self
end

#sizeObject Also known as: length



42
43
44
# File 'lib/moxml/node_set.rb', line 42

def size
  nodes.size
end

#textObject



88
89
90
# File 'lib/moxml/node_set.rb', line 88

def text
  map(&:text).join
end

#to_aObject



47
48
49
# File 'lib/moxml/node_set.rb', line 47

def to_a
  map { |node| node }
end

#uniq_by_nativeObject

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