From e71fe3d6fa4c15e10838a9b7ac4ca07a2b3e8194 Mon Sep 17 00:00:00 2001 From: Yorick Peterse Date: Sun, 29 Jun 2014 23:59:27 +0200 Subject: [PATCH] Indexing of NodeSet instances. Similar to arrays the NodeSet class now allows one to retrieve nodes for a given index. --- lib/oga/xml/node_set.rb | 10 ++++++++++ spec/oga/xml/node_set_spec.rb | 11 +++++++++++ 2 files changed, 21 insertions(+) diff --git a/lib/oga/xml/node_set.rb b/lib/oga/xml/node_set.rb index 65cf3cc..b4f73ee 100644 --- a/lib/oga/xml/node_set.rb +++ b/lib/oga/xml/node_set.rb @@ -103,6 +103,16 @@ module Oga return @nodes.pop end + ## + # Returns the node for the given index. + # + # @param [Fixnum] index + # @return [Oga::XML::Node] + # + def [](index) + return @nodes[index] + end + ## # Removes the current nodes from their owning set. The nodes are *not* # removed from the current set. diff --git a/spec/oga/xml/node_set_spec.rb b/spec/oga/xml/node_set_spec.rb index 9ebc66b..4379aaa 100644 --- a/spec/oga/xml/node_set_spec.rb +++ b/spec/oga/xml/node_set_spec.rb @@ -128,6 +128,17 @@ describe Oga::XML::NodeSet do end end + context '#[]' do + before do + @n1 = Oga::XML::Element.new(:name => 'a') + @set = described_class.new([@n1]) + end + + example 'return a node from a given index' do + @set[0].should == @n1 + end + end + context '#remove' do before do @n1 = Oga::XML::Element.new(:name => 'a')