Class: Loadable::GemLoader
- Inherits:
-
Object
- Object
- Loadable::GemLoader
- 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
Instance Method Summary collapse
-
#apply?(fname, options = {}) ⇒ Boolean
Determine if this load wedge is applicable given the
fname
andoptions
. -
#call(fname, options = {}) ⇒ Object
Load script from specific gem.
-
#each(options = {}, &block) ⇒ Object
Iterate over each loadable file in specified gem.
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
.
86 87 88 89 90 91 92 |
# File 'lib/loadable/loaders/gem_loader.rb', line 86 def apply?(fname, ={}) return true if [:gem] return true if [:from] && ( ::Gem::Specification.find{ |s| [: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, ={}) return unless apply?(fname, ) gem_name = [:gem] || [:from] if vers = [:version] spec = ::Gem::Specification.find_by_name(gem_name, vers) else spec = ::Gem::Specification.find_by_name(gem_name) end if [: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, ) 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(={}, &block) return unless apply?(nil, ) gem_name = ([:gem] || [:from]).to_s if vers = [: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 |