Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Support for default attribute values #26

Closed
wants to merge 1 commit into from

2 participants

@Confusion

(Sorry, I messed up #24 by pushing additional commits to this branch, which should be considered separately for inclusion. I force-pushed to remove them, but for some reason the commit in that pull request was also removed)

I've added support for a :default option to attribute declarations. This makes it easy to declare default values, without requiring you to write initialize functions that set them. When working with an XML schema, these :default values can nicely reflect the 'default' declarations in the XSD.

Our use case is we generate classes that include HappyMapper from information parsed from an XSD. With this extension we can easily add a :default => 'some_value' to the generated attribute :foo, String lines, based on the default declarations parsed from the XSD. However, it seems like something that may more generally be considered useful.

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.
a4b9bf9
@dam5s

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

@dam5s
Owner

Fixed and merged

@dam5s dam5s closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Dec 4, 2012
  1. Support for default attribute values

    Ivo Wever authored
    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.
This page is out of date. Refresh to see the latest.
View
12 lib/happymapper.rb
@@ -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
View
12 lib/happymapper/attribute.rb
@@ -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
View
32 spec/default_option_spec.rb
@@ -0,0 +1,32 @@
+require 'spec/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
@@ -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
Something went wrong with that request. Please try again.