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: Empact/roxml
base: master
...
head fork: hashrocket/roxml
compare: master
Checking mergeability… Don’t worry, you can still create the pull request.
  • 5 commits
  • 5 files changed
  • 0 commit comments
  • 3 contributors
Commits on Jun 11, 2012
@greis greis Fix reader to always return the same object
Signed-off-by: Hashrocket Workstation <dev@hashrocket.com>
9049074
Gabriel Reis and Thais Camilo Add xml_initializer macro
* you can specify the initializer for the class, if none is provided,
* then 'new' will be called

Signed-off-by: Hashrocket Workstation <dev@hashrocket.com>
48c213d
Evan Sparkman and Gabriel Reis Boolean option :call_setter to disable setter
Signed-off-by: Hashrocket Workstation <dev@hashrocket.com>
c02c9a4
Evan Sparkman and Gabriel Reis Does not set a field value if it's an empty array ece6347
Commits on Jun 21, 2012
Gabriel Reis and Thais Camilo Use roxml attributes from the instance class a3bb4de
View
28 lib/roxml.rb
@@ -476,14 +476,17 @@ def xml_accessor(*syms, &block)
end
end
+ def xml_initializer(&block)
+ @xml_initializer = block
+ end
+
private
def add_reader(attr)
define_method(attr.accessor) do
if instance_variable_get(attr.instance_variable_name).nil?
- attr.default
- else
- instance_variable_get(attr.instance_variable_name)
+ instance_variable_set(attr.instance_variable_name, attr.default)
end
+ instance_variable_get(attr.instance_variable_name)
end
end
end
@@ -546,16 +549,25 @@ module Operations
# See also: xml_initialize
#
def from_xml(data, *initialization_args)
+ from_xml_with_parent(data, nil, *initialization_args)
+ end
+
+ def from_xml_with_parent(data, parent, *initialization_args)
xml = XML::Node.from(data)
- new(*initialization_args).tap do |inst|
- inst.roxml_references = roxml_attrs.map {|attr| attr.to_ref(inst) }
+ object = @xml_initializer.nil? ? new(*initialization_args) : @xml_initializer.call(xml, parent)
+ object.tap do |inst|
+ inst.roxml_references = inst.class.roxml_attrs.map {|attr| attr.to_ref(inst) }
inst.roxml_references.each do |ref|
value = ref.value_in(xml)
- inst.respond_to?(ref.opts.setter) \
- ? inst.send(ref.opts.setter, value) \
- : inst.instance_variable_set(ref.opts.instance_variable_name, value)
+ unless value.nil? || ( value.is_a?(Array) && value.empty? )
+ if inst.respond_to?(ref.opts.setter)
+ inst.send(ref.opts.setter, value) if ref.opts.call_setter?
+ else
+ inst.instance_variable_set(ref.opts.instance_variable_name, value)
+ end
+ end
end
inst.send(:after_parse) if inst.respond_to?(:after_parse, true)
end
View
5 lib/roxml/definition.rb
@@ -16,11 +16,11 @@ class ContradictoryNamespaces < StandardError
class Definition # :nodoc:
attr_reader :name, :sought_type, :wrapper, :hash, :blocks, :accessor, :to_xml, :attr_name, :namespace
- bool_attr_reader :name_explicit, :array, :cdata, :required, :frozen
+ bool_attr_reader :name_explicit, :array, :cdata, :required, :frozen, :call_setter
def initialize(sym, opts = {}, &block)
opts.assert_valid_keys(:from, :in, :as, :namespace,
- :else, :required, :frozen, :cdata, :to_xml)
+ :else, :required, :frozen, :cdata, :to_xml, :call_setter)
@default = opts.delete(:else)
@to_xml = opts.delete(:to_xml)
@name_explicit = opts.has_key?(:from) && opts[:from].is_a?(String)
@@ -29,6 +29,7 @@ def initialize(sym, opts = {}, &block)
@frozen = opts.delete(:frozen)
@wrapper = opts.delete(:in)
@namespace = opts.delete(:namespace)
+ @call_setter = opts.delete(:call_setter) { true }
@accessor = sym.to_s
opts[:as] ||=
View
4 lib/roxml/xml/references.rb
@@ -306,7 +306,9 @@ def update_xml(xml, value)
private
def fetch_value(xml)
nodes_in(xml) do |node|
- if sought_type.respond_to? :from_xml
+ if sought_type.respond_to? :from_xml_with_parent
+ sought_type.from_xml_with_parent(node, @instance)
+ elsif sought_type.respond_to? :from_xml
sought_type.from_xml(node)
else
sought_type.new(node)
View
6 spec/examples/search_query_spec.rb
@@ -15,8 +15,12 @@
@search.max_results == 20
end
+ it 'should return the same object for the default value' do
+ @search.language.object_id.should == @search.language.object_id
+ end
+
it 'should respect the defaults when loading from xml' do
@saved_search.language.should == 'EN'
@saved_search.max_results == 20
end
-end
+end
View
49 spec/roxml_spec.rb
@@ -164,6 +164,55 @@ class BookWithOctalPagesType
book.pages.should == @expected_pages
end
end
+
+ describe "with #xml_initializer defined on the object" do
+ class BookWithXmlInitializer
+ include ROXML
+
+ attr_accessor :name
+ xml_initializer do |xml|
+ BookWithXmlInitializer.new.tap { |b| b.name = 'Book' }
+ end
+ xml_accessor :pages, :as => Integer, :required => true
+ end
+
+ it "should initialize the object by the block" do
+ book = BookWithXmlInitializer.from_xml(@book_with_octal_pages_xml)
+ book.name.should == 'Book'
+ end
+
+ it "should apply the mapped fields to the initialized object" do
+ book = BookWithXmlInitializer.from_xml(@book_with_octal_pages_xml)
+ book.pages.should == @expected_pages
+ end
+ end
+
+ describe "with :call_setter" do
+ context "set to false" do
+ class BookWithoutSetter
+ include ROXML
+
+ xml_accessor :pages, :as => Integer, :call_setter => false
+ end
+
+ it "should not apply filtering on input" do
+ book = BookWithoutSetter.from_xml(@book_with_octal_pages_xml)
+ book.pages.should == nil
+ end
+ end
+ context "set to true" do
+ class BookWithSetter
+ include ROXML
+
+ xml_accessor :pages, :as => Integer, :call_setter => true
+ end
+
+ it "should apply filtering on input" do
+ book = BookWithSetter.from_xml(@book_with_octal_pages_xml)
+ book.pages.should == @expected_pages
+ end
+ end
+ end
end
describe "attribute reference" do

No commit comments for this range

Something went wrong with that request. Please try again.