From: Michal Fojtik <mfoj...@redhat.com>
Signed-off-by: Michal fojtik <mfoj...@redhat.com> --- server/lib/cimi/model.rb | 2 + server/lib/cimi/model/base.rb | 4 +- server/lib/cimi/model/schema.rb | 42 +++++++++++++-------------------------- 3 files changed, 18 insertions(+), 30 deletions(-) diff --git a/server/lib/cimi/model.rb b/server/lib/cimi/model.rb index c1d7f02..fcc6e17 100644 --- a/server/lib/cimi/model.rb +++ b/server/lib/cimi/model.rb @@ -14,6 +14,8 @@ # under the License. # +# Declare namespace for CIMI model +# module CIMI module Model; end end diff --git a/server/lib/cimi/model/base.rb b/server/lib/cimi/model/base.rb index 0ef2b27..de6529b 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 :properties, :content => :value do - scalar :key + array :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 e470121..d8520d5 100644 --- a/server/lib/cimi/model/schema.rb +++ b/server/lib/cimi/model/schema.rb @@ -60,23 +60,19 @@ class CIMI::Model::Schema def nested_text?; @text == :nested; end def from_xml(xml, model) - if @text == :nested - model[@name] = xml[@xml_name].first["content"] if xml[@xml_name] - elsif @text == :direct - model[@name] = xml["content"] - else - model[@name] = xml[@xml_name] + case @text + when :nested : model[@name] = xml[@xml_name].first["content"] if xml[@xml_name] + when :direct : model[@name] = xml["content"] + else model[@name] = xml[@xml_name] end end def to_xml(model, xml) return unless model[@name] - if @text == :nested - xml[@xml_name] = [{ "content" => model[@name] }] - elsif @text == :direct - xml["content"] = model[@name] - else - xml[@xml_name] = model[@name] + case @text + when :nested : xml[@xml_name] = [{ "content" => model[@name] }] + when :direct : xml["content"] = model[@name] + else xml[@xml_name] = model[@name] end end end @@ -84,7 +80,7 @@ class CIMI::Model::Schema class Struct < Attribute def initialize(name, opts, &block) content = opts[:content] - super(name) + super(name, opts) @schema = CIMI::Model::Schema.new @schema.instance_eval(&block) if block_given? @schema.scalar(content, :text => :direct) if content @@ -156,15 +152,11 @@ class CIMI::Model::Schema end def from_xml(xml, model) - model[name] = (xml[xml_name] || []).map do |elt| - @struct.convert_from_xml(elt) - end + model[name] = (xml[xml_name] || []).map { |elt| @struct.convert_from_xml(elt) } end def from_json(json, model) - model[name] = (json[json_name] || []).map do |elt| - @struct.convert_from_json(elt) - end + model[name] = (json[json_name] || []).map { |elt| @struct.convert_from_json(elt) } end def to_xml(model, xml) @@ -220,9 +212,7 @@ class CIMI::Model::Schema # +add_attributes!+ method module DSL def href(*args) - args.each do |arg| - struct(arg) { scalar :href } - end + args.each { |arg| struct(arg) { scalar :href } } end def text(*args) @@ -246,12 +236,8 @@ class CIMI::Model::Schema include DSL def add_attributes!(args, attr_klass, &block) - if @attributes.frozen? - raise "The schema has already been used to convert objects" - end + raise "The schema has already been used to convert objects" if @attributes.frozen? opts = args.extract_opts! - args.each do |arg| - @attributes << attr_klass.new(arg, opts, &block) - end + args.each { |arg| @attributes << attr_klass.new(arg, opts, &block) } end end -- 1.7.4.4