deltacloud-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mfoj...@redhat.com
Subject [PATCH core 4/8] Core: Added possibility to run filter_on with multiple attrs
Date Mon, 18 Feb 2013 13:41:14 GMT
From: Michal Fojtik <mfojtik@redhat.com>

So instead of writing:

instances = filter_on instances, :id, opts
instances = filter_on instances, :realm_id, opts
instances = filter_on instances, :state, opts

You can now use:

instances = filter_on instances, opts, :id, :realm_id, :state

The backward compatibility with old filter_on included.

Signed-off-by: Michal fojtik <mfojtik@redhat.com>
---
 server/lib/deltacloud/drivers/base_driver.rb | 27 ++++++++++++++++++++-------
 1 file changed, 20 insertions(+), 7 deletions(-)

diff --git a/server/lib/deltacloud/drivers/base_driver.rb b/server/lib/deltacloud/drivers/base_driver.rb
index 61e1989..8b89aec 100644
--- a/server/lib/deltacloud/drivers/base_driver.rb
+++ b/server/lib/deltacloud/drivers/base_driver.rb
@@ -262,14 +262,27 @@ module Deltacloud
     MEMBER_SHOW_METHODS = [ :realm, :image, :instance, :storage_volume, :bucket, :blob,
                             :key, :firewall ] unless defined?(MEMBER_SHOW_METHODS)
 
-    def filter_on(collection, attribute, opts)
+    def filter_on(collection, opts, *attributes)
+      opts, attributes = attributes.pop, [opts] if !opts.nil? and !(opts.is_a?(Hash))
+      # FIXME: ^^ This is just to keep backward compatibility until
+      # drivers will be fixed to use the new syntax.
+      #
+      # filter_on instances, :opts, [ :id, :state, :realm_id]
+      #
+      # instead of old:
+      #
+      # filter_on instances, :id, opts
+      # filter_on instances, :state, opts
+      # filter_on instances, :realm_id, opts
+
       return collection if opts.nil?
-      return collection if opts[attribute].nil?
-      filter = opts[attribute]
-      if ( filter.is_a?( Array ) )
-        return collection.select{|e| filter.include?( e.send(attribute) ) }
-      else
-        return collection.select{|e| filter == e.send(attribute) }
+      attributes.each do |attribute|
+        next unless filter = opts[attribute]
+        if ( filter.is_a?( Array ) )
+          collection.select! { |e| filter.include?( e.send(attribute) ) }
+        else
+          collection.select! { |e| filter == e.send(attribute) }
+        end
       end
     end
 
-- 
1.8.1.2


Mime
View raw message