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



52
53
54
# File 'lib/moxml/node_set.rb', line 52

def +(other)
  self.class.new(nodes + other.nodes, context)
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

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



60
61
62
63
# File 'lib/moxml/node_set.rb', line 60

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



56
57
58
# File 'lib/moxml/node_set.rb', line 56

def text
  map(&:text).join
end

#to_aObject



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

def to_a
  map { |node| node }
end