Class: Loadable::GemLoader

Inherits:
Object
  • Object
show all
Includes:
Loadable
Defined in:
lib/loadable/loaders/gem_loader.rb

Overview

The Gem Wedge allows gem files to be loaded in a isolated fashion.

require 'tracepoint', :from=>'tracepoint'

The example would load the tracepoint file from the tracepoint gem. It will also fallback to the RubyLoader if ‘tracepoint’ is not found among available gems. Loading can be limited to gems only by using the ‘:gem` options instead.

require 'tracepoint', :gem=>'tracepoint'

Now, if the ‘tracepoint` script is not found among availabe gems, a LoadError will be raised.

Constant Summary

Constants included from Loadable

RB_EXTS

Instance Method Summary collapse

Methods included from Loadable

call, #default_file_extensions, each, #lookup, #name, #raise_load_error, register, search, #traverse, vendor

Instance Method Details

#apply?(fname, options = {}) ⇒ Boolean

Determine if this load wedge is applicable given the fname and options.

Returns:

  • (Boolean)


86
87
88
89
90
91
92
# File 'lib/loadable/loaders/gem_loader.rb', line 86

def apply?(fname, options={})
  return true if options[:gem]
  return true if options[:from] && (
    ::Gem::Specification.find{ |s| options[:from].to_s == s.name }
  )
  return false
end

#call(fname, options = {}) ⇒ Object

Load script from specific gem.

Returns nil if this loader is not applicable, which is determined by the use of ‘:gem => ’foo’‘ or `:from => ’foo’‘ options.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/loadable/loaders/gem_loader.rb', line 34

def call(fname, options={})
  return unless apply?(fname, options)

  gem_name = options[:gem] || options[:from]

  if vers = options[:version]
    spec = ::Gem::Specification.find_by_name(gem_name, vers)
  else
    spec = ::Gem::Specification.find_by_name(gem_name)
  end

  if options[:gem]
    raise_load_error(fname) unless spec
  else
    return unless spec
  end

  file = spec.find_requirable_file(fname)
  file = spec.find_requirable_file(File.join(gem_name, fname)) unless file

  if file
    super(file, options)
  else
    raise_load_error(fname)
  end
end

#each(options = {}, &block) ⇒ Object

Iterate over each loadable file in specified gem.



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/loadable/loaders/gem_loader.rb', line 63

def each(options={}, &block)
  return unless apply?(nil, options)

  gem_name = (options[:gem] || options[:from]).to_s

  if vers = options[:version]
    spec = ::Gem::Specification.find_by_name(gem_name, vers)
  else
    spec = ::Gem::Specification.find_by_name(gem_name)
  end

  return unless spec

  #spec.activate

  spec.require_paths.each do |path|
    traverse(File.join(spec.full_gem_path, path), &block)
  end
end