Class: RightScale::CloudApi::Manager

Inherits:
Object
  • Object
show all
Defined in:
lib/base/manager.rb

Overview

The class is the parent class for all the cloud based thread-safe managers.

The main purpose of the manager is to check if the current thread or fiber has a thread-unsafe ApiManager instance created or not. If not them the manager creates than instance of ApiManager in the current thread/fiber and feeds the method and parameters to it.

Examples:


module RightScale
  module CloudApi
    module MyCoolCloudNamespace
      class Manager < CloudApi::Manager
      end
    end
  end
end

# Create an instance of MyCoolCloudNamespace manager.
my_cloud = RightScale::CloudApi::MyCoolCloudNamespace::Manager.new(my_cool_creds)

# Make an API call.
# The call below creates an instance of RightScale::CloudApi::YourCoolCloudNamespace::ApiManager in the
# current thread and invokes "ListMyCoolResources" method on it.
my_cloud.ListMyCoolResources #=> cloud response

See Also:

Instance Method Summary collapse

Constructor Details

#initialize(*args) {|Any| ... } ⇒ Manager

The initializer.

Parameters:

  • args (Any)

    Usually a set of credentials.

Yields:

  • (Any)

    Optional: the block will be passed to ApiManager on its initialization.



96
97
98
99
100
101
# File 'lib/base/manager.rb', line 96

def initialize(*args, &block)
  @args, @block      = args, block
  options            = args.last.is_a?(Hash) ? args.last : {}
  @api_manager_class = options[:api_manager_class] || self.class.name.sub(/::Manager$/, '::ApiManager')._constantize
  @api_manager_storage = {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args, &block) ⇒ Object

Feeds all unknown methods to the ApiManager instance.



116
117
118
# File 'lib/base/manager.rb', line 116

def method_missing(m, *args, &block)
  api_manager.__send__(m, *args, &block)
end

Instance Method Details

#api_manager..::MyCoolCloudNamespace::ApiManager

Returns the an instance of ApiManager for the current thread/fiber. The method creates a new ApiManager instance ubless it exist.

Returns:

  • (..::MyCoolCloudNamespace::ApiManager)


108
109
110
111
112
# File 'lib/base/manager.rb', line 108

def api_manager
  # Delete dead threads and their managers from the list.
  Utils::remove_dead_fibers_and_threads_from_storage(@api_manager_storage)
  @api_manager_storage[Utils::current_thread_and_fiber] ||= @api_manager_class::new(*@args, &@block)
end