From 3a18d23792558ed4feb135d2d2c4b6c6a416e493 Mon Sep 17 00:00:00 2001 From: Yorick Peterse Date: Tue, 28 Jul 2015 19:44:13 +0200 Subject: [PATCH] to_boolean support for truthy Ruby values --- lib/oga/xpath/conversion.rb | 2 ++ spec/oga/xpath/conversion_spec.rb | 6 ++++++ 2 files changed, 8 insertions(+) diff --git a/lib/oga/xpath/conversion.rb b/lib/oga/xpath/conversion.rb index fd72f54..ed95f9f 100644 --- a/lib/oga/xpath/conversion.rb +++ b/lib/oga/xpath/conversion.rb @@ -80,6 +80,8 @@ module Oga bool = !value.zero? elsif value.respond_to?(:empty?) bool = !value.empty? + elsif value + bool = true end bool diff --git a/spec/oga/xpath/conversion_spec.rb b/spec/oga/xpath/conversion_spec.rb index f69c260..b7ea936 100644 --- a/spec/oga/xpath/conversion_spec.rb +++ b/spec/oga/xpath/conversion_spec.rb @@ -220,5 +220,11 @@ describe Oga::XPath::Conversion do it 'returns false for an empty NodeSet' do described_class.to_boolean(node_set).should == false end + + it 'returns true for an Element' do + element = Oga::XML::Element.new(:name => 'foo') + + described_class.to_boolean(element).should == true + end end end