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



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

#==(other) ⇒ Object



55
56
57
58
59
60
61
# File 'lib/moxml/node_set.rb', line 55

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

#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



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



67
68
69
70
# File 'lib/moxml/node_set.rb', line 67

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



63
64
65
# File 'lib/moxml/node_set.rb', line 63

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