You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@deltacloud.apache.org by mf...@apache.org on 2012/10/11 10:33:28 UTC

[6/7] git commit: CIMI models: let the schema convert raw values for model objects

CIMI models: let the schema convert raw values for model objects

This is a noop for now
TrackedAt: http://tracker.deltacloud.org/patch/c59cc9969b00874cbfc7339742a8b954d270088a


Project: http://git-wip-us.apache.org/repos/asf/deltacloud/repo
Commit: http://git-wip-us.apache.org/repos/asf/deltacloud/commit/6ba23752
Tree: http://git-wip-us.apache.org/repos/asf/deltacloud/tree/6ba23752
Diff: http://git-wip-us.apache.org/repos/asf/deltacloud/diff/6ba23752

Branch: refs/heads/master
Commit: 6ba23752a735bd7c7aec9b9eccbd3e5663410510
Parents: 292dabf
Author: David Lutterkort <lu...@redhat.com>
Authored: Fri Oct 5 17:03:53 2012 -0700
Committer: Michal fojtik <mf...@redhat.com>
Committed: Thu Oct 11 10:31:07 2012 +0200

----------------------------------------------------------------------
 server/lib/cimi/models/base.rb   |    9 +++++++--
 server/lib/cimi/models/schema.rb |   10 ++++++++++
 2 files changed, 17 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/deltacloud/blob/6ba23752/server/lib/cimi/models/base.rb
----------------------------------------------------------------------
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+

http://git-wip-us.apache.org/repos/asf/deltacloud/blob/6ba23752/server/lib/cimi/models/schema.rb
----------------------------------------------------------------------
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) }