ref: refs/heads/jr-basemodel commit cb148ac8ac16c8346c67f902f616001802789c9d Author: Josef Reidinger <jreidinger@suse.cz> Date: Fri Dec 11 10:37:06 2009 +0100 improve xml serialization --- webservice/lib/base_model/base.rb | 9 ++++++- .../lib/base_model/serializers/xml_serializer.rb | 24 +++++++++++++++++++- webservice/test/unit/base_model_test.rb | 16 ++++++++++++- 3 files changed, 46 insertions(+), 3 deletions(-) diff --git a/webservice/lib/base_model/base.rb b/webservice/lib/base_model/base.rb index 0e89cc0..eb72bcb 100644 --- a/webservice/lib/base_model/base.rb +++ b/webservice/lib/base_model/base.rb @@ -1,5 +1,10 @@ module BaseModel class Base + + def to_model + self + end + def initialize(attr={}) load(attr) end @@ -14,7 +19,7 @@ module BaseModel end def new_record? - true + false #always update by default end def create_or_update @@ -46,5 +51,7 @@ module BaseModel include BaseModel::MassAssignment include BaseModel::Serialization + include YastRoles #to access permission check in models + end end diff --git a/webservice/lib/base_model/serializers/xml_serializer.rb b/webservice/lib/base_model/serializers/xml_serializer.rb index 9cee3a3..8522710 100644 --- a/webservice/lib/base_model/serializers/xml_serializer.rb +++ b/webservice/lib/base_model/serializers/xml_serializer.rb @@ -17,10 +17,32 @@ module BaseModel builder = options[:builder] || Builder::XmlMarkup.new(options) builder.tag!(root){ @attributes.each do |attr| - builder.tag!(attr.to_s[1..-1],@model.instance_variable_get(attr)) + value = @model.instance_variable_get(attr) + name = attr.to_s[1..-1] + serialize_value(name,value,builder) end } end + + protected +#place for all types for special serializing + def serialize_value(name,value,builder) + if value.is_a? Array + builder.tag!(name,{:type => "array"}) do + value.each do |v| + serialize_value(name,v,builder) + end + end + elsif value.is_a? Hash + builder.tag!(name) do + value.each do |k,v| + serialize_value(k,v,builder) + end + end + else + builder.tag!(name,value.to_s) + end + end end end end diff --git a/webservice/test/unit/base_model_test.rb b/webservice/test/unit/base_model_test.rb index 7be9418..34f9466 100644 --- a/webservice/test/unit/base_model_test.rb +++ b/webservice/test/unit/base_model_test.rb @@ -5,7 +5,7 @@ class BaseModelTest < ActiveSupport::TestCase validates_format_of :arg2, :with => /^\d$/ before_save :call - attr_accessor :arg1, :arg2, :callback_used + attr_accessor :arg1, :arg2, :callback_used, :carg attr_protected :callback_used def call @callback_used = true; @@ -54,23 +54,37 @@ MASS_DATA = { :arg1 => "last", :arg2 => "5", :callback_used => false } assert test2.arg2.nil? end +COMPLEX_DATA = { + "test" => [ "a","b"], #serializers doesn't differ symbol from string and always sue string + "test2" => [ "5","6"], #number after serializing is converted to string + "test3" => { "a" => "b","c"=> "d" }, #number after serializing is converted to string + "test_escapes" => "<arg>/&\\test", + "test_hash" => [{"a"=>"a"},{"b"=>"b"}] +} + def test_xml_serialization test= Test.new(MASS_DATA) + test.carg = COMPLEX_DATA xml = test.to_xml assert xml + puts xml.inspect test2 = Test.new test2.from_xml xml assert_equal "last", test2.arg1 assert_equal "5", test2.arg2 + assert_equal COMPLEX_DATA, test2.carg end def test_json_serialization test= Test.new(MASS_DATA) + test.carg = COMPLEX_DATA json = test.to_json + puts json.inspect assert json test2 = Test.new test2.from_json json assert_equal "last", test2.arg1 assert_equal "5", test2.arg2 + assert_equal COMPLEX_DATA, test2.carg end end -- To unsubscribe, e-mail: yast-commit+unsubscribe@opensuse.org For additional commands, e-mail: yast-commit+help@opensuse.org