lib/carbonado.rb
# Activate the named gem. Specify a version requirement as a string or a Gem::Requirement
# object. This will also recursively activate the dependencies of the gem. It can fail if a gem
# has a dependency that is already activated which doesn't satisfy the requirements of the given
# gem or one of its dependencies. Otherwise it can fail if the given gem or one of its 
# dependencies is not installed
Activate the named gem. Specify a version requirement as a string or a Gem::Requirement
object. This will also recursively activate the dependencies of the gem. It can fail if a gem
has a dependency that is already activated which doesn't satisfy the requirements of the given
gem or one of its dependencies. Otherwise it can fail if the given gem or one of its
dependencies is not installed
Activate the named gem . Specify a version requirement as a string or a Gem :: Requirement object . This will also recursively activate the dependencies of the gem . It can fail if a gem has a dependency that is already activated which doesn ' t satisfy the requirements of the given gem or one of its dependencies . Otherwise it can fail if the given gem or one of its dependencies is not installed
def activate_gem(gem_name, version = nil, requirer = nil)
    version_requirement = version.is_a?(Gem::Requirement) ? version : Gem::Requirement.default
    loaded_gem = Gem::Specification.stubs.select{ |s| s.name == gem_name }.first
    if !loaded_gem.nil?
      return true if Gem::Requirement.new(version_requirement).satisfied_by?(loaded_gem.version)      
      error_msg = "There is alread an activated version of #{gem_name}"
      error_msg += ", required by #{requirer}" if requirer
      error_msg += ", that does not meet the version requirement #{version_requirement}"
      raise Error, error_msg
    end
    installed_versions = installed_stubs.select{ |s| s.name == gem_name }.sort_by(&:version)
    raise Error, "Could not find #{gem_name} in list of installed gems" if installed_versions.empty?
    allowed_versions = installed_versions.select do |s|
      Gem::Requirement.new(version_requirement).satisfied_by?(s.version)
    end
    selected_spec = allowed_versions.last.to_spec
    selected_spec.dependencies.select(&:runtime?).each{ |dep| activate_gem(dep.name) }    
    selected_spec.activate
  end
def activate_gem ( gem_name , version = nil , requirer = nil ) version_requirement = version . is_a? ( Gem :: Requirement ) ? version : Gem :: Requirement . default loaded_gem = Gem :: Specification . stubs . select { | s | s . name == gem_name } . first if ! loaded_gem . nil? return true if Gem :: Requirement . new ( version_requirement ) . satisfied_by? ( loaded_gem . version ) error_msg = "There is alread an activated version of #{gem_name}" error_msg += ", required by #{requirer}" if requirer error_msg += ", that does not meet the version requirement #{version_requirement}" raise Error , error_msg end installed_versions = installed_stubs . select { | s | s . name == gem_name } . sort_by ( & :version ) raise Error , "Could not find #{gem_name} in list of installed gems" if installed_versions . empty? allowed_versions = installed_versions . select do | s | Gem :: Requirement . new ( version_requirement ) . satisfied_by? ( s . version ) end selected_spec = allowed_versions . last . to_spec selected_spec . dependencies . select ( & :runtime? ) . each { | dep | activate_gem ( dep . name ) } selected_spec . activate end
Activate the named gem.
Activate the named gem .
gem_name
version
requirer