OK Tong Li Emerging Technologies & Standards Building 501/B205 liton...@us.ibm.com
mfoj...@redhat.com wrote on 11/18/2011 08:32:33 AM: > From: mfoj...@redhat.com > To: deltacloud-dev@incubator.apache.org > Date: 11/18/2011 08:32 AM > Subject: [PATCH core 4/4] CIMI: Added new type 'hash' to deal with properties > > From: Michal Fojtik <mfoj...@redhat.com> > > > Signed-off-by: Michal fojtik <mfoj...@redhat.com> > --- > server/lib/cimi/model/base.rb | 2 +- > server/lib/cimi/model/schema.rb | 33 +++++++++++++++++++++++++++++++++ > 2 files changed, 34 insertions(+), 1 deletions(-) > > diff --git a/server/lib/cimi/model/base.rb b/server/lib/cimi/model/base.rb > index de6529b..7cc3f49 100644 > --- a/server/lib/cimi/model/base.rb > +++ b/server/lib/cimi/model/base.rb > @@ -163,7 +163,7 @@ class CIMI::Model::Base > text :uri, :name, :description, :created > > # FIXME: this doesn't match with JSON > - array :property, :content => :value do > + hash :property, :content => :value do > scalar :name > end > end > diff --git a/server/lib/cimi/model/schema.rb b/server/lib/cimi/model/schema.rb > index 946c1a2..46d537b 100644 > --- a/server/lib/cimi/model/schema.rb > +++ b/server/lib/cimi/model/schema.rb > @@ -170,6 +170,35 @@ class CIMI::Model::Schema > end > end > > + class Hash < Attribute > + > + def initialize(name, opts = {}, &block) > + opts[:json_name] = name.to_s.pluralize unless opts[:json_name] > + super(name, opts) > + @struct = Struct.new(name, opts, &block) > + end > + > + def from_xml(xml, model) > + model[name] = (xml[xml_name] || []).map { |elt| > @struct.convert_from_xml(elt) } > + end > + > + def from_json(json, model) > + model[name] = (json[json_name] || {}).inject([]) do |result,item| > + result << @struct.convert_from_json({ 'name' => item[0], > 'value' => item[1] }) > + end > + end > + > + def to_xml(model, xml) > + ary = model[name].map { |elt| @struct.convert_to_xml(elt) } > + xml[xml_name] = ary unless ary.empty? > + end > + > + def to_json(model, json) > + ary = model[name].map { |elt| @struct.convert_to_json(elt) } > + json[json_name] = ary.inject({}) { |result, item| result[item > ['name']] = item['value']; result } unless ary.empty? > + end > + end > + > # > # The actual Schema class > # > @@ -231,6 +260,10 @@ class CIMI::Model::Schema > def struct(name, opts={}, &block) > add_attributes!([name, opts], Struct, &block) > end > + > + def hash(name, opts={}, &block) > + add_attributes!([name, opts], Hash, &block) > + end > end > > include DSL > -- > 1.7.4.4 >