From 9899a419b73bcddf07941bcda45b7050339a3b60 Mon Sep 17 00:00:00 2001 From: Yorick Peterse Date: Wed, 26 Aug 2015 22:26:46 +0200 Subject: [PATCH] Added Attribute#each_ancestor --- lib/oga/xml/attribute.rb | 9 +++++++++ spec/oga/xml/attribute_spec.rb | 29 +++++++++++++++++++++++++---- 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/lib/oga/xml/attribute.rb b/lib/oga/xml/attribute.rb index 99dc96d..4abbb43 100644 --- a/lib/oga/xml/attribute.rb +++ b/lib/oga/xml/attribute.rb @@ -125,6 +125,15 @@ module Oga "Attribute(#{segments.join(' ')})" end + # @see [Oga::XML::Node#each_ancestor] + def each_ancestor(&block) + return unless element + + yield element + + element.each_ancestor(&block) + end + private ## diff --git a/spec/oga/xml/attribute_spec.rb b/spec/oga/xml/attribute_spec.rb index 0bb22cf..64c6e61 100644 --- a/spec/oga/xml/attribute_spec.rb +++ b/spec/oga/xml/attribute_spec.rb @@ -168,17 +168,38 @@ EOF describe '#expanded_name' do describe 'with a namespace' do it 'returns a String' do - element = described_class.new(:namespace_name => 'foo', :name => 'bar') + attr = described_class.new(:namespace_name => 'foo', :name => 'bar') - element.expanded_name.should == 'foo:bar' + attr.expanded_name.should == 'foo:bar' end end describe 'without a namespace' do it 'returns a String' do - element = described_class.new(:name => 'bar') + attr = described_class.new(:name => 'bar') - element.expanded_name.should == 'bar' + attr.expanded_name.should == 'bar' + end + end + end + + describe '#each_ancestor' do + describe 'without an element' do + it 'simply returns' do + attr = described_class.new(:name => 'class') + + expect { |b| attr.each_ancestor(&b) }.to_not yield_control + end + end + + describe 'with an element' do + it 'yields the supplied block for every ancestor' do + child = Oga::XML::Element.new(:name => 'b') + parent = Oga::XML::Element.new(:name => 'a', :children => [child]) + attr = described_class.new(:name => 'class', :element => child) + + expect { |b| attr.each_ancestor(&b) } + .to yield_successive_args(child, parent) end end end