Permalink
Browse files

Support for default attribute values

Added a :default option to attribute declarations, which result in a
default value being set on objects. This default value will not be
included in generated XML.
  • Loading branch information...
Ivo Wever
Ivo Wever committed Dec 1, 2012
1 parent df40b76 commit a4b9bf9c1677644b223117b7600935fbc9a2a1a8
Showing with 59 additions and 2 deletions.
  1. +11 −1 lib/happymapper.rb
  2. +11 −1 lib/happymapper/attribute.rb
  3. +32 −0 spec/default_option_spec.rb
  4. +5 −0 spec/happymapper_attribute_spec.rb
View
@@ -370,7 +370,9 @@ def parse(xml, options = {})
obj = options[:update] ? options[:update] : new
attributes.each do |attr|
- obj.send("#{attr.method_name}=",attr.from_xml_node(n, namespace, namespaces))
+ value = attr.from_xml_node(n, namespace, namespaces)
+ value = attr.default if value.nil?
+ obj.send("#{attr.method_name}=", value)
end
elements.each do |elem|
@@ -429,7 +431,14 @@ def parse(xml, options = {})
collection
end
end
+ end
+ # Set all attributes with a default to their default values
+ def initialize
+ super
+ self.class.attributes.reject {|attr| attr.default.nil?}.each do |attr|
+ send("#{attr.method_name}=", attr.default)
+ end
end
#
@@ -479,6 +488,7 @@ def to_xml(builder = nil,default_namespace = nil,tag_from_parent = nil)
unless attribute.options[:read_only]
value = send(attribute.method_name)
+ value = nil if value == attribute.default
#
# If the attribute defines an on_save lambda/proc or value that maps to
@@ -1,3 +1,13 @@
module HappyMapper
- class Attribute < Item; end
+ class Attribute < Item
+ attr_accessor :default
+
+ # @see Item#initialize
+ # Additional options:
+ # :default => Object The default value for this
+ def initialize(name, type, o={})
+ super
+ self.default = o[:default]
+ end
+ end
end
@@ -0,0 +1,32 @@
+require 'spec/spec_helper'

This comment has been minimized.

Show comment Hide comment
@dam5s

dam5s Dec 27, 2012

This require breaks the build. It should be require 'spec_helper'

@dam5s

dam5s Dec 27, 2012

This require breaks the build. It should be require 'spec_helper'

+
+describe "The default option of attributes" do
+
+ class WithDefault
+ include HappyMapper
+ tag 'foo'
+ attribute :bar, String, :default => 'baz'
+ end
+
+ it 'should return the default value after parsing a string without it' do
+ foo = WithDefault.parse('<foo />')
+ foo.bar.should == 'baz'
+ end
+
+ it 'should not include the default value in the produced xml' do
+ foo = WithDefault.new
+ foo.to_xml.should == %{<?xml version="1.0"?>\n<foo/>\n}
+ end
+
+ it 'should not return the default value when a non-nil value has been set' do
+ foo = WithDefault.parse('<foo />')
+ foo.bar = 'not-baz'
+ foo.bar.should_not == 'baz'
+ end
+
+ it 'should include a non-nil value in the XML' do
+ foo = WithDefault.new
+ foo.bar = 'not-baz'
+ foo.to_xml.should == %{<?xml version="1.0"?>\n<foo bar="not-baz"/>\n}
+ end
+end
@@ -17,5 +17,10 @@
it 'should know that it is NOT a text node' do
@attr.text_node?.should be_false
end
+
+ it 'should accept :default as an option' do
+ attr = described_class.new(:foo, String, :default => 'foobar')
+ attr.default.should == 'foobar'
+ end
end
end

0 comments on commit a4b9bf9

Please sign in to comment.