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 2013/02/21 14:51:01 UTC

[4/5] git commit: Core: Fix code duplication in how we load collections

Core: Fix code duplication in how we load collections

This patch introduce new helper that provide mechanism
to load all collection for the given frontend


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

Branch: refs/heads/master
Commit: cdf14f31f4a5ab8a1631d97a1b885fe663b0e666
Parents: 19babd4
Author: Michal Fojtik <mf...@redhat.com>
Authored: Thu Feb 21 14:36:28 2013 +0100
Committer: Michal fojtik <mf...@redhat.com>
Committed: Thu Feb 21 14:36:28 2013 +0100

----------------------------------------------------------------------
 server/lib/cimi/collections.rb                     |   36 +-----
 server/lib/cimi/models/cloud_entry_point.rb        |    2 +-
 server/lib/deltacloud/collections.rb               |   34 +----
 server/lib/deltacloud/drivers/base_driver.rb       |    2 +-
 server/lib/deltacloud/helpers/collection_helper.rb |  106 +++++++++++++++
 server/tests/deltacloud/collections_test.rb        |    6 +-
 6 files changed, 119 insertions(+), 67 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/deltacloud/blob/cdf14f31/server/lib/cimi/collections.rb
----------------------------------------------------------------------
diff --git a/server/lib/cimi/collections.rb b/server/lib/cimi/collections.rb
index e0a93b5..9990fa5 100644
--- a/server/lib/cimi/collections.rb
+++ b/server/lib/cimi/collections.rb
@@ -15,46 +15,18 @@
 # under the License.
 
 require_relative './collections/base'
+require_relative './../deltacloud/helpers/collection_helper'
 
 module CIMI
 
-  def self.collection_names
-    @collections.map { |c| c.collection_name }
-  end
-
-  def self.collections
-    @collections ||= []
-  end
-
   module Collections
+    extend Deltacloud::CollectionHelper
 
-    def self.collection(name)
-      CIMI.collections.find { |c| c.collection_name == name }
-    end
-
-    def self.cimi_modules
-      @cimi_modules ||= []
-    end
-
-    Dir[File.join(File::dirname(__FILE__), "collections", "*.rb")].each do |collection|
-      base_collection_name = File.basename(collection).gsub('.rb', '')
-      next if base_collection_name == 'base'
-      require collection
-      cimi_module_class = CIMI::Collections.const_get(base_collection_name.camelize)
-      cimi_modules << cimi_module_class
-      unless cimi_module_class.collections.nil?
-        cimi_module_class.collections.each do |c|
-          raise "ERROR: CIMI collection #{c} already registred" if CIMI.collections.include? c
-          CIMI.collections << c
-        end
-      else
-        warn "WARNING: File %s placed in collections directory but does not have any collections defined" % base_collection_name
-      end
-    end
+    load_collections_for :cimi, :from => File.join(File.dirname(__FILE__), 'collections')
 
     def self.included(klass)
       klass.class_eval do
-        CIMI::Collections.cimi_modules.each { |c| use c }
+        CIMI::Collections.modules(:cimi).each { |c| use c }
       end
     end
 

http://git-wip-us.apache.org/repos/asf/deltacloud/blob/cdf14f31/server/lib/cimi/models/cloud_entry_point.rb
----------------------------------------------------------------------
diff --git a/server/lib/cimi/models/cloud_entry_point.rb b/server/lib/cimi/models/cloud_entry_point.rb
index 7f274e9..6c28e53 100644
--- a/server/lib/cimi/models/cloud_entry_point.rb
+++ b/server/lib/cimi/models/cloud_entry_point.rb
@@ -51,7 +51,7 @@ class CIMI::Model::CloudEntryPoint < CIMI::Model::Base
 
   # Return an Hash of the CIMI root entities used in CloudEntryPoint
   def self.entities(context)
-    CIMI::Collections.cimi_modules.inject({}) do |supported_entities, m|
+    CIMI::Collections.modules(:cimi).inject({}) do |supported_entities, m|
       m.collections.each do |c|
         index_operation_capability = c.operation(:index).required_capability
         next if m.settings.respond_to?(:capability) and !m.settings.capability(index_operation_capability)

http://git-wip-us.apache.org/repos/asf/deltacloud/blob/cdf14f31/server/lib/deltacloud/collections.rb
----------------------------------------------------------------------
diff --git a/server/lib/deltacloud/collections.rb b/server/lib/deltacloud/collections.rb
index d3517b8..b41ef06 100644
--- a/server/lib/deltacloud/collections.rb
+++ b/server/lib/deltacloud/collections.rb
@@ -15,44 +15,18 @@
 # under the License.
 
 require_relative './collections/base'
+require_relative './helpers/collection_helper'
 
 module Deltacloud
 
-  def self.collection_names
-    @collections.map { |c| c.collection_name }
-  end
-
-  def self.collections
-    @collections ||= []
-  end
-
   module Collections
+    extend Deltacloud::CollectionHelper
 
-    def self.collection(name)
-      Deltacloud.collections.find { |c| c.collection_name == name }
-    end
-
-    def self.deltacloud_modules
-      @deltacloud_modules ||= []
-    end
-
-    Dir[File.join(File::dirname(__FILE__), "collections", "*.rb")].each do |collection|
-      base_collection_name = File.basename(collection).gsub('.rb', '')
-      next if base_collection_name == 'base'
-      require collection
-      deltacloud_module_class = Deltacloud::Collections.const_get(base_collection_name.camelize)
-      deltacloud_modules << deltacloud_module_class
-      deltacloud_module_class.collections.each do |c|
-        if Deltacloud.collections.include? c
-          raise "ERROR: Deltacloud collection #{c} already registred"
-        end
-        Deltacloud.collections << c
-      end unless deltacloud_module_class.collections.nil?
-    end
+    load_collections_for :deltacloud, :from => File.join(File.dirname(__FILE__), 'collections')
 
     def self.included(klass)
       klass.class_eval do
-        Deltacloud::Collections.deltacloud_modules.each do |collection_class|
+        Deltacloud::Collections.modules(:deltacloud).each do |collection_class|
           use collection_class
         end
       end

http://git-wip-us.apache.org/repos/asf/deltacloud/blob/cdf14f31/server/lib/deltacloud/drivers/base_driver.rb
----------------------------------------------------------------------
diff --git a/server/lib/deltacloud/drivers/base_driver.rb b/server/lib/deltacloud/drivers/base_driver.rb
index 61e1989..1bb80d5 100644
--- a/server/lib/deltacloud/drivers/base_driver.rb
+++ b/server/lib/deltacloud/drivers/base_driver.rb
@@ -156,7 +156,7 @@ module Deltacloud
 
     def supported_collections(credentials)
       collection_arr = []
-      Deltacloud::Collections.deltacloud_modules.each do |m|
+      Deltacloud::Collections.modules(:deltacloud).each do |m|
         m.collections.each do |c|
           # Get the required capability for the :index operation (like 'realms' or 'instance_state_machine')
           index_operation_capability = c.operation(:index).required_capability

http://git-wip-us.apache.org/repos/asf/deltacloud/blob/cdf14f31/server/lib/deltacloud/helpers/collection_helper.rb
----------------------------------------------------------------------
diff --git a/server/lib/deltacloud/helpers/collection_helper.rb b/server/lib/deltacloud/helpers/collection_helper.rb
new file mode 100644
index 0000000..a4347a4
--- /dev/null
+++ b/server/lib/deltacloud/helpers/collection_helper.rb
@@ -0,0 +1,106 @@
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.  The
+# ASF licenses this file to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance with the
+# License.  You may obtain a copy of the License at
+#
+#       http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+# License for the specific language governing permissions and limitations
+# under the License.
+
+module Deltacloud
+
+  # Collection methods that every frontend need to provide.
+  #
+  module CollectionMethods
+
+    # Return all collections provided by the current frontend
+    #
+    def collections
+      module_klass::Collections.collections
+    end
+
+    # Return all collection names provided by the current frontend
+    #
+    def collection_names
+      module_klass::Collections.collection_names
+    end
+
+    # Simple check if the collection is available in the current frontend
+    #
+    def collection_exists?(c)
+      collections.include? c
+    end
+
+    private
+
+    def module_klass
+      @klass ||= self
+    end
+
+  end
+
+  extend CollectionMethods
+
+  module CollectionHelper
+
+    def collection_names
+      collections.map { |c| c.collection_name }
+    end
+
+    def collections
+      @collections ||= []
+    end
+
+    def collection(name)
+      collections.find { |c| c.collection_name == name }
+    end
+
+
+    def modules(frontend)
+      case frontend
+        when :cimi then @cimi_modules ||= []
+        when :deltacloud then @deltacloud_modules ||= []
+      end
+    end
+
+    # This method will load all collections from given directory.
+    #
+    # Syntax:
+    #
+    # load_collections_for :cimi, :from => DIRECTORY
+    #
+    def load_collections_for(frontend, opts={})
+      frontend_module = (frontend == :cimi) ? CIMI : Deltacloud
+      Dir[File.join(opts[:from], '*.rb')].each do |collection|
+        base_collection_name = File.basename(collection).gsub('.rb', '')
+        next if base_collection_name == 'base'
+        require collection
+        collection_module_class = frontend_module::Collections.const_get(
+          base_collection_name.camelize
+        )
+        modules(frontend) << collection_module_class
+        if collection_module_class.collections.nil?
+          warn "WARNING: #{collection_module_class} does not include any collections"
+        else
+          collection_module_class.collections.each do |c|
+            if frontend_module.collection_exists?(c)
+              raise "ERROR: Collection already registred #{c}"
+            end
+            frontend_module.collections << c
+          end
+        end
+      end
+    end
+
+  end
+end
+
+module CIMI
+  extend Deltacloud::CollectionMethods
+end

http://git-wip-us.apache.org/repos/asf/deltacloud/blob/cdf14f31/server/tests/deltacloud/collections_test.rb
----------------------------------------------------------------------
diff --git a/server/tests/deltacloud/collections_test.rb b/server/tests/deltacloud/collections_test.rb
index d3e41e9..47dfb6c 100644
--- a/server/tests/deltacloud/collections_test.rb
+++ b/server/tests/deltacloud/collections_test.rb
@@ -24,9 +24,9 @@ describe Deltacloud do
     end
 
     it 'must provide access to Deltacloud Sinatra modules' do
-      Deltacloud::Collections.must_respond_to :deltacloud_modules
-      Deltacloud::Collections.deltacloud_modules.wont_be_empty
-      Deltacloud::Collections.deltacloud_modules.must_include Deltacloud::Collections::Drivers
+      Deltacloud::Collections.must_respond_to :modules
+      Deltacloud::Collections.modules(:deltacloud).wont_be_empty
+      Deltacloud::Collections.modules(:deltacloud).must_include Deltacloud::Collections::Drivers
     end
 
   end