You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@deltacloud.apache.org by mf...@redhat.com on 2012/07/17 16:00:13 UTC

[PATCH core 3/4] Core: Removed obsoleted to_hash serialization and minor fixes in models

From: Michal Fojtik <mf...@redhat.com>


Signed-off-by: Michal fojtik <mf...@redhat.com>
---
 server/lib/deltacloud/models/address.rb          |    2 +-
 server/lib/deltacloud/models/base_model.rb       |   14 ----------
 server/lib/deltacloud/models/bucket.rb           |    8 ------
 server/lib/deltacloud/models/hardware_profile.rb |   31 ++++------------------
 server/lib/deltacloud/models/image.rb            |   12 ---------
 server/lib/deltacloud/models/instance.rb         |   18 +------------
 server/lib/deltacloud/models/key.rb              |    4 +--
 server/lib/deltacloud/models/metric.rb           |    2 +-
 8 files changed, 10 insertions(+), 81 deletions(-)

diff --git a/server/lib/deltacloud/models/address.rb b/server/lib/deltacloud/models/address.rb
index dbabb08..7054a77 100644
--- a/server/lib/deltacloud/models/address.rb
+++ b/server/lib/deltacloud/models/address.rb
@@ -22,7 +22,7 @@ class Address < BaseModel
   end
 
   def associated?
-    true unless self.instance_id.nil?
+    !self.instance_id.nil?
   end
 
 end
diff --git a/server/lib/deltacloud/models/base_model.rb b/server/lib/deltacloud/models/base_model.rb
index b122b2b..5c19ad5 100644
--- a/server/lib/deltacloud/models/base_model.rb
+++ b/server/lib/deltacloud/models/base_model.rb
@@ -44,18 +44,4 @@ class BaseModel
     @id
   end
 
-  def to_hash
-    out = {}
-    self.attributes.each do |attribute|
-      if self.send(:"#{attribute}")
-        out.merge!({ attribute => self.send(:"#{attribute}") } )
-      end
-    end
-    out
-  end
-
-  def to_json(*a)
-    self.to_hash.to_json(*a)
-  end
-
 end
diff --git a/server/lib/deltacloud/models/bucket.rb b/server/lib/deltacloud/models/bucket.rb
index 0c21c21..ad812af 100644
--- a/server/lib/deltacloud/models/bucket.rb
+++ b/server/lib/deltacloud/models/bucket.rb
@@ -26,12 +26,4 @@ class Bucket < BaseModel
     @blob_list || []
   end
 
-  def to_hash
-    h = self.to_hash_original
-    unless blob_list.nil?
-      h[:blob_list] = self.blob_list.collect { |blob| { :id => blob,
-        :href => "#{settings.root_url}/buckets/#{self.id}/#{blob}"}}
-    end
-    return h
-  end
 end
diff --git a/server/lib/deltacloud/models/hardware_profile.rb b/server/lib/deltacloud/models/hardware_profile.rb
index 0b36c4b..f9678b0 100644
--- a/server/lib/deltacloud/models/hardware_profile.rb
+++ b/server/lib/deltacloud/models/hardware_profile.rb
@@ -154,35 +154,14 @@ module Deltacloud
     end
 
     def default?(prop, v)
-      p = @properties[prop.to_sym]
-      p && p.default.to_s == v
-    end
-
-    def to_hash
-      props = []
-      self.each_property do |p|
-        if p.kind.eql? :fixed
-          props << { :kind => p.kind, :value => p.value, :name => p.name, :unit => p.unit }
-        else
-          param = { :operation => "create", :method => "post", :name => p.name }
-          if p.kind.eql? :range
-            param[:range] = { :first => p.first, :last => p.last }
-          elsif p.kind.eql? :enum
-            param[:enum] = p.values.collect { |v| { :entry => v } }
-          end
-          param
-          props << { :kind => p.kind, :value => p.default, :name => p.name, :unit => p.unit, :param => param }
-        end
-      end
-      {
-        :id => self.name,
-        :properties => props
-      }
+      property(prop) && property(prop).default.to_s == v
     end
 
     def include?(prop, v)
-      p = @properties[prop]
-      p.nil? || p.include?(v)
+      return false unless p = property(prop)
+      return true if p.kind == :range and (p.first..p.last).include?(v)
+      return true if p.kind == :enum and p.values.include?(v)
+      false
     end
 
     def params
diff --git a/server/lib/deltacloud/models/image.rb b/server/lib/deltacloud/models/image.rb
index 555004e..f9a64e1 100644
--- a/server/lib/deltacloud/models/image.rb
+++ b/server/lib/deltacloud/models/image.rb
@@ -24,16 +24,4 @@ class Image < BaseModel
   attr_accessor :state
   attr_accessor :hardware_profiles
 
-  alias :to_hash_original :to_hash
-
-  def to_hash
-    h = self.to_hash_original
-    h.merge({
-      :actions => [ :create_instance => {
-        :method => 'post',
-        :href => "#{settings.root_url}/instances;image_id=#{self.id}"
-      }]
-    })
-  end
-
 end
diff --git a/server/lib/deltacloud/models/instance.rb b/server/lib/deltacloud/models/instance.rb
index 9db5b4b..303691d 100644
--- a/server/lib/deltacloud/models/instance.rb
+++ b/server/lib/deltacloud/models/instance.rb
@@ -63,7 +63,7 @@ class Instance < BaseModel
 
   def method_missing(name, *args)
     if name =~ /is_(\w+)\?/
-      return true if self.state.downcase.eql?($1)
+      self.state.downcase.eql?($1)
     else
       raise NoMethodError.new(name.to_s)
     end
@@ -73,20 +73,4 @@ class Instance < BaseModel
     return true unless authn_error.nil?
   end
 
-  alias :to_hash_original :to_hash
-
-  def to_hash
-    h = self.to_hash_original
-    h[:public_addresses] = h[:public_addresses].collect do |address|
-      { :address => { :type => address.address_type, :value => address } }
-    end
-    h[:actions] = self.actions.collect do |action|
-      { :"#{action}" => {
-        :method => collections[:instances].operations[action.to_sym].method,
-        :href => collections[:instances].operations[action.to_sym].path.gsub(':id', self.id)
-      }}
-    end
-    h
-  end
-
 end
diff --git a/server/lib/deltacloud/models/key.rb b/server/lib/deltacloud/models/key.rb
index 64c4cc7..388c14a 100644
--- a/server/lib/deltacloud/models/key.rb
+++ b/server/lib/deltacloud/models/key.rb
@@ -28,11 +28,11 @@ class Key < BaseModel
   end
 
   def is_password?
-    true if @credential_type.eql?(:password)
+    @credential_type.eql?(:password)
   end
 
   def is_key?
-    true if @credential_type.eql?(:key)
+    @credential_type.eql?(:key)
   end
 
   # Mock fingerprint generator
diff --git a/server/lib/deltacloud/models/metric.rb b/server/lib/deltacloud/models/metric.rb
index 42edaaf..f6f3822 100644
--- a/server/lib/deltacloud/models/metric.rb
+++ b/server/lib/deltacloud/models/metric.rb
@@ -19,7 +19,7 @@ class Metric < BaseModel
   attr_accessor :properties
 
   def unknown?
-    true if self.entity == :unknown
+    self.entity == :unknown
   end
 
   def add_property(name, values=nil)
-- 
1.7.10.2