Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: dam5s/happymapper
base: 56878f0944
...
head fork: dam5s/happymapper
compare: b8a6bf698e
  • 2 commits
  • 3 files changed
  • 0 commit comments
  • 2 contributors
Commits on Dec 27, 2012
Ivo Wever 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.
c43bcce
@dam5s Fixed require in specs b8a6bf6
View
12 lib/happymapper.rb
@@ -368,7 +368,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|
@@ -427,7 +429,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
#
@@ -477,6 +486,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
View
32 spec/default_option_spec.rb
@@ -0,0 +1,32 @@
+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
View
5 spec/happymapper_attribute_spec.rb
@@ -10,5 +10,10 @@
attr = described_class.new(:foo, String, :default => 'foobar')
attr.default.should == 'foobar'
end
+
+ it 'should accept :default as an option' do
+ attr = described_class.new(:foo, String, :default => 'foobar')
+ attr.default.should == 'foobar'
+ end
end
end

No commit comments for this range

Something went wrong with that request. Please try again.