From a65536063f766b2516b9a1923b68ddd8dda5ea5b Mon Sep 17 00:00:00 2001 From: Akinori MUSHA Date: Thu, 6 Nov 2014 19:10:32 +0900 Subject: [PATCH] Implement Slop#respond_to_missing?. (#1176) --- CHANGELOG.rdoc | 4 ++++ lib/nokogiri/decorators/slop.rb | 8 ++++++++ test/decorators/test_slop.rb | 8 ++++++-- 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.rdoc b/CHANGELOG.rdoc index c9ac86975f..857b14beb8 100644 --- a/CHANGELOG.rdoc +++ b/CHANGELOG.rdoc @@ -1,5 +1,9 @@ === 1.6.5 / unreleased +==== Features + +* Implement Slop#respond_to_missing?. (#1176) + ==== Bug fixes * Fix a bug where CFLAGS passed in are dropped. diff --git a/lib/nokogiri/decorators/slop.rb b/lib/nokogiri/decorators/slop.rb index f5fdc3d002..1eee56302a 100644 --- a/lib/nokogiri/decorators/slop.rb +++ b/lib/nokogiri/decorators/slop.rb @@ -30,6 +30,14 @@ def method_missing name, *args, &block super if list.empty? list.length == 1 ? list.first : list end + + def respond_to_missing? name, include_private = false + prefix = implied_xpath_context + + list = xpath("#{prefix}#{name.to_s.sub(/^_/, '')}") + + !list.empty? + end end end end diff --git a/test/decorators/test_slop.rb b/test/decorators/test_slop.rb index 396fa0404e..d0258890f1 100644 --- a/test/decorators/test_slop.rb +++ b/test/decorators/test_slop.rb @@ -9,8 +9,12 @@ def test_description_tag this is the foo thing eoxml - assert doc.item.title - assert doc.item._description, 'should have description' + assert doc.item.respond_to?(:title) + assert_equal 'foo', doc.item.title.text + assert doc.item.respond_to?(:_description), 'should have description' + assert 'this is the foo thing', doc.item._description.text + assert !doc.item.respond_to?(:foo) + assert_raise(NoMethodError) { doc.item.foo } end end end