Sha256: fdc488a622120d178caef4c533b68f9b466c366b7c70a2129a3ede374b9785e6

Contents?: true

Size: 1.16 KB

Versions: 8

Compression:

Stored size: 1.16 KB

Contents

# -*- encoding : utf-8 -*-
module Exchange
  
  # The gem loader takes care of loading gems without adding too many unnecessary dependencies to the gem
  # @author Beat Richartz
  # @version 0.6
  # @since 0.6
  #
  class GemLoader
    
    # The error that gets thrown if a needed gem is not available or loadable
    #
    GemNotFoundError  = Class.new LoadError

    # initialize the loader with a gem name. 
    # @param [string] gem The gem to require
    # @return [Exchange::Gemloader] an instance of the gemloader
    # @example initialize a loader for the nokogiri gem
    #   Exchange::GemLoader.new('nokogiri')
    #
    def initialize gem
      @gem = gem
    end
    
    # Try to require the gem specified on initialization.
    # @raise [GemNotFoundError] an error indicating that the gem could not be found rather than a load error
    # @example Try to load the JSON gem
    #   Exchange::GemLoader.new('json').try_load
    #
    def try_load
      require @gem
    rescue LoadError => e
      raise GemNotFoundError.new("You specified #{@gem} to be used with Exchange, yet it is not loadable. Please install #{@gem} to be able to use it with Exchange")
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
exchange-1.2.2 lib/exchange/gem_loader.rb
exchange-1.2.1 lib/exchange/gem_loader.rb
exchange-1.2.0 lib/exchange/gem_loader.rb
exchange-1.1.1 lib/exchange/gem_loader.rb
exchange-1.1.0 lib/exchange/gem_loader.rb
exchange-1.0.4 lib/exchange/gem_loader.rb
exchange-1.0.2 lib/exchange/gem_loader.rb
exchange-1.0.0 lib/exchange/gem_loader.rb