Added XML::NodeSet#+ and XML::NodeSet#to_a

This commit is contained in:
Yorick Peterse 2014-07-08 23:25:09 +02:00
parent 266c66569e
commit 9c661e1e60
2 changed files with 44 additions and 0 deletions

View File

@ -159,6 +159,26 @@ module Oga
return @nodes[index]
end
##
# Converts the current set to an Array.
#
# @return [Array]
#
def to_a
return @nodes
end
##
# Creates a new set based on the current and the specified set. The newly
# created set does not inherit ownership rules of the current set.
#
# @param [Oga::XML::NodeSet] other
# @return [Oga::XML::NodeSet]
#
def +(other)
return self.class.new(to_a + other.to_a)
end
##
# Removes the current nodes from their owning set. The nodes are *not*
# removed from the current set.

View File

@ -185,6 +185,30 @@ describe Oga::XML::NodeSet do
end
end
context '#to_a' do
before do
@n1 = Oga::XML::Element.new(:name => 'a')
@set = described_class.new([@n1])
end
example 'convert a set to an Array' do
@set.to_a.should == [@n1]
end
end
context '#+' do
before do
@n1 = Oga::XML::Element.new(:name => 'a')
@n2 = Oga::XML::Element.new(:name => 'b')
@set1 = described_class.new([@n1])
@set2 = described_class.new([@n2])
end
example 'merge two sets together' do
(@set1 + @set2).to_a.should == [@n1, @n2]
end
end
context '#remove' do
before do
owner = Oga::XML::Element.new