Skip to content
Browse files

Boolean option :call_setter to disable setter

Signed-off-by: Hashrocket Workstation <dev@hashrocket.com>
  • Loading branch information...
1 parent 48c213d commit c02c9a43e161462c0cced294f656d353f0503bfd Evan Sparkman and Gabriel Reis committed with Hashrocket Workstation Jun 11, 2012
Showing with 31 additions and 3 deletions.
  1. +1 −1 lib/roxml.rb
  2. +3 −2 lib/roxml/definition.rb
  3. +27 −0 spec/roxml_spec.rb
View
2 lib/roxml.rb
@@ -563,7 +563,7 @@ def from_xml_with_parent(data, parent, *initialization_args)
value = ref.value_in(xml)
unless value.nil?
if inst.respond_to?(ref.opts.setter)
- inst.send(ref.opts.setter, value)
+ inst.send(ref.opts.setter, value) if ref.opts.call_setter?
else
inst.instance_variable_set(ref.opts.instance_variable_name, value)
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
27 spec/roxml_spec.rb
@@ -186,6 +186,33 @@ class BookWithXmlInitializer
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

0 comments on commit c02c9a4

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