You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@deltacloud.apache.org by lu...@redhat.com on 2012/10/06 02:52:07 UTC

[PATCH 3/7] CIMI models: let the schema convert raw values for model objects

From: David Lutterkort <lu...@redhat.com>

This is a noop for now
---
 server/lib/cimi/models/base.rb   |    9 +++++++--
 server/lib/cimi/models/schema.rb |   10 ++++++++++
 2 files changed, 17 insertions(+), 2 deletions(-)

diff --git a/server/lib/cimi/models/base.rb b/server/lib/cimi/models/base.rb
index 4c9b2dc..eefa868 100644
--- a/server/lib/cimi/models/base.rb
+++ b/server/lib/cimi/models/base.rb
@@ -161,14 +161,19 @@ class CIMI::Model::Base
   end
 
   def []=(a, v)
-    @attribute_values[a] = v
+    @attribute_values[a] = self.class.schema.convert(a, v)
+  end
   end
 
   #
   # Factory methods
   #
   def initialize(values = {})
-    @attribute_values = values
+    names = self.class.schema.attribute_names
+    @attribute_values = names.inject({}) do |hash, name|
+      hash[name] = self.class.schema.convert(name, values[name])
+      hash
+    end
   end
 
   # Construct a new object from the XML representation +xml+
diff --git a/server/lib/cimi/models/schema.rb b/server/lib/cimi/models/schema.rb
index ed3541a..e679b2e 100644
--- a/server/lib/cimi/models/schema.rb
+++ b/server/lib/cimi/models/schema.rb
@@ -46,6 +46,10 @@ class CIMI::Model::Schema
     def to_json(model, json)
       json[@json_name] = model[@name] if model and model[@name]
     end
+
+    def convert(value)
+      value
+    end
   end
 
   class Scalar < Attribute
@@ -222,6 +226,12 @@ class CIMI::Model::Schema
     @attributes = []
   end
 
+  def convert(name, value)
+    attr = @attributes.find { |a| a.name == name }
+    raise "Unknown attribute #{name}" unless attr
+    attr.convert(value)
+  end
+
   def from_xml(xml, model = {})
     @attributes.freeze
     @attributes.each { |attr| attr.from_xml(xml, model) }
-- 
1.7.7.6