Skip to content
Browse files

Allow writing attributes with converter methods

  • Loading branch information...
1 parent b9f2fa0 commit f5c4df00dc5faf560efd55bb1329746b86b81986 @benlangfeld committed Nov 14, 2012
Showing with 10 additions and 2 deletions.
  1. +2 −2 lib/niceogiri/xml/node.rb
  2. +8 −0 spec/niceogiri/xml/node_spec.rb
View
4 lib/niceogiri/xml/node.rb
@@ -32,8 +32,8 @@ def read_attr(attr_name, to_call = nil)
#
# @param [#to_sym] attr_name the name of the attribute
# @param [#to_s] value the value to set the attribute to
- def write_attr(attr_name, value)
- self[attr_name.to_sym] = value
+ def write_attr(attr_name, value, to_call = nil)
+ self[attr_name.to_sym] = value && to_call ? value.__send__(to_call) : value
end
# Helper method to read the content of a node
View
8 spec/niceogiri/xml/node_spec.rb
@@ -46,6 +46,14 @@ module XML
subject[:bar].should == 'baz'
end
+ it 'provides an attribute writer with conversion' do
+ subject[:bar].should be_nil
+ subject.write_attr :bar, '1.0', :to_i
+ subject[:bar].should == '1'
+ subject.write_attr :bar, nil, :to_i
+ subject[:bar].should == nil
+ end
+
it 'provides a content reader' do
foo = Node.new 'foo'
foo << (bar = Node.new('bar', foo.document))

0 comments on commit f5c4df0

Please sign in to comment.
Something went wrong with that request. Please try again.