Class: Exchange::GemLoader

Inherits:
Object
  • Object
show all
Defined in:
lib/exchange/gem_loader.rb

Overview

The gem loader takes care of loading gems without adding too many unnecessary dependencies to the gem

Author:

  • Beat Richartz

Since:

  • 0.6

Version:

  • 0.6

Constant Summary collapse

GemNotFoundError =

The error that gets thrown if a needed gem is not available or loadable

Since:

  • 0.6

Class.new UsageError

Instance Method Summary collapse

Constructor Details

#initialize(gem) ⇒ Exchange::Gemloader

initialize the loader with a gem name.

Examples:

initialize a loader for the nokogiri gem

Exchange::GemLoader.new('nokogiri')

Parameters:

  • gem (string)

    The gem to require

Since:

  • 0.6


21
22
23
# File 'lib/exchange/gem_loader.rb', line 21

def initialize gem
  @gem = gem
end

Instance Method Details

#try_loadObject

Try to require the gem specified on initialization.

Examples:

Try to load the JSON gem

Exchange::GemLoader.new('json').try_load

Raises:

  • (GemNotFoundError)

    an error indicating that the gem could not be found rather than a load error

Since:

  • 0.6


30
31
32
33
34
# File 'lib/exchange/gem_loader.rb', line 30

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