Permalink
Browse files

Properly handle setting attributes to an array on Ruby 1.9

  • Loading branch information...
1 parent 7a16ead commit 7cba30f076590116f5b825f0fca4319019d15cae @benlangfeld benlangfeld committed Sep 28, 2011
Showing with 15 additions and 1 deletion.
  1. +6 −1 lib/niceogiri/core_ext/nokogiri.rb
  2. +9 −0 spec/niceogiri/core_ext/nokogiri_spec.rb
@@ -16,7 +16,12 @@ class Node
# @param [#to_s, nil] value the new value or nil to remove it
def []=(name, value)
name = name.to_s
- value.nil? ? remove_attribute(name) : attr_set(name, value.to_s)
+ if value.nil?
+ remove_attribute name
+ else
+ value = value.is_a?(Array) ? value.join : value
+ attr_set name, value.to_s
+ end
end
alias_method :nokogiri_xpath, :xpath
@@ -30,6 +30,15 @@
attrs[:foo] = 1
attrs[:foo].must_equal '1'
+ attrs[:some_attr] = :bah
+ attrs[:some_attr].must_equal 'bah'
+ end
+
+ it 'joins an array into a string when passed to the attribute setter' do
+ attrs = Nokogiri::XML::Node.new('foo', @doc)
+ attrs[:foo] = 1
+ attrs[:foo].must_equal '1'
+
attrs[:some_attr] = [:bah, :boo]
attrs[:some_attr].must_equal 'bahboo'
end

0 comments on commit 7cba30f

Please sign in to comment.