ref: refs/heads/jr-basemodel commit 95a8b60a2c5fb0a46d418fabf1b4ee404dfd4e7e Author: Josef Reidinger <jreidinger@suse.cz> Date: Wed Dec 9 17:09:36 2009 +0100 add xml serialization to base model --- webservice/lib/base_model/base.rb | 1 + webservice/lib/base_model/serialization.rb | 37 ++++++++++++++++++++ .../lib/base_model/serializers/xml_serializer.rb | 26 ++++++++++++++ webservice/test/unit/base_model_test.rb | 10 +++++ 4 files changed, 74 insertions(+), 0 deletions(-) diff --git a/webservice/lib/base_model/base.rb b/webservice/lib/base_model/base.rb index f9ed7f3..0e89cc0 100644 --- a/webservice/lib/base_model/base.rb +++ b/webservice/lib/base_model/base.rb @@ -44,6 +44,7 @@ module BaseModel include BaseModel::MassAssignment + include BaseModel::Serialization end end diff --git a/webservice/lib/base_model/serialization.rb b/webservice/lib/base_model/serialization.rb new file mode 100644 index 0000000..56a8a86 --- /dev/null +++ b/webservice/lib/base_model/serialization.rb @@ -0,0 +1,37 @@ +module BaseModel + module Serialization + class Serializer + attr_reader :options + + def initialize(model,options={}) + @model = model + @options = options + @attributes = options[:attributes] + unless @attributes + @attributes = model.instance_variables.collect { |v| v.to_sym } + end + end + + #to overwrite + def serialize + end + end + + module ClassMethods + def attr_serialized(*args) + @attr_serialized ||= [] + @attr_serialized.concat args + end + + def serialized_attributes + @attr_serialized + end + end + + def self.included(base) + base.extend ClassMethods + end + + end +end +require 'base_model/serializers/xml_serializer' diff --git a/webservice/lib/base_model/serializers/xml_serializer.rb b/webservice/lib/base_model/serializers/xml_serializer.rb new file mode 100644 index 0000000..9cee3a3 --- /dev/null +++ b/webservice/lib/base_model/serializers/xml_serializer.rb @@ -0,0 +1,26 @@ +module BaseModel + module Serialization + def to_xml(options={},&block) + options[:attributes] = self.class.serialized_attributes + serializer = XmlSerializer.new(self,options) + block_given? ? serializer.serialize(&block) : serializer.serialize + end + + def from_xml(xml) + load(Hash.from_xml(xml).values.first) + self + end + + class XmlSerializer < BaseModel::Serialization::Serializer + def serialize + root = options[:root] || @model.class.model_name.singular + 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)) + end + } + end + end + end +end diff --git a/webservice/test/unit/base_model_test.rb b/webservice/test/unit/base_model_test.rb index edde777..0e9c1b0 100644 --- a/webservice/test/unit/base_model_test.rb +++ b/webservice/test/unit/base_model_test.rb @@ -53,4 +53,14 @@ MASS_DATA = { :arg1 => "last", :arg2 => "5", :callback_used => false } assert_equal "last", test2.arg1 assert test2.arg2.nil? end + + def test_xml_serialization + test= Test.new(MASS_DATA) + xml = test.to_xml + assert xml + test2 = Test.new + test2.from_xml xml + assert_equal "last", test2.arg1 + assert_equal "5", test2.arg2 + end end -- To unsubscribe, e-mail: yast-commit+unsubscribe@opensuse.org For additional commands, e-mail: yast-commit+help@opensuse.org