Module: Gem::QuickLoader

Included in:
Gem
Defined in:
lib/core/r4a/gems.rb

Constant Summary

GemPaths =
{}
GemVersions =
{}

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



340
341
342
343
344
# File 'lib/core/r4a/gems.rb', line 340

def method_missing(method, *args, &block)
  QuickLoader.load_full_rubygems_library
  super unless Gem.respond_to?(method)
  Gem.send(method, *args, &block)
end

Class Method Details

.load_full_rubygems_libraryObject



214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/core/r4a/gems.rb', line 214

def self.load_full_rubygems_library
  return if @loaded_full_rubygems_library

  @loaded_full_rubygems_library = true

  class << Gem
    Gem::GEM_PRELUDE_METHODS.each do |method_name|
      undef_method method_name
    end
  end

  Kernel.module_eval do
    undef_method :gem if method_defined? :gem
  end

  $".delete File.join(Gem::ConfigMap[:libdir],
                      Gem::ConfigMap[:ruby_install_name],
                      Gem::ConfigMap[:ruby_version], 'rubygems.rb')

  require 'rubygems'
end

Instance Method Details

#calculate_integers_for_gem_version(gem_version) ⇒ Object



275
276
277
278
279
280
# File 'lib/core/r4a/gems.rb', line 275

def calculate_integers_for_gem_version(gem_version)
  numbers = gem_version.split(".").collect {|n| n.to_i}
  numbers.pop while numbers.last == 0
  numbers << 0 if numbers.empty?
  numbers
end

#const_missing(constant) ⇒ Object



331
332
333
334
335
336
337
338
# File 'lib/core/r4a/gems.rb', line 331

def const_missing(constant)
  QuickLoader.load_full_rubygems_library
  if Gem.const_defined?(constant)
    Gem.const_get(constant)
  else
    super
  end
end

#push_all_highest_version_gems_on_load_pathObject



282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
# File 'lib/core/r4a/gems.rb', line 282

def push_all_highest_version_gems_on_load_path
  Gem.path.each do |path|
    gems_directory = File.join(path, "gems")
    if File.exist?(gems_directory)
      Dir.entries(gems_directory).each do |gem_directory_name|
        next if gem_directory_name == "." || gem_directory_name == ".."
        dash = gem_directory_name.rindex("-")
        next if dash.nil?
        gem_name = gem_directory_name[0...dash]
        current_version = GemVersions[gem_name]
        new_version = calculate_integers_for_gem_version(gem_directory_name[dash+1..-1])
        if current_version
          if (current_version <=> new_version) == -1
            GemVersions[gem_name] = new_version
            GemPaths[gem_name] = File.join(gems_directory, gem_directory_name)
          end
        else
          GemVersions[gem_name] = new_version
          GemPaths[gem_name] = File.join(gems_directory, gem_directory_name)
        end
      end
    end
  end

  require_paths = []

  GemPaths.each_value do |path|
    if File.exist?(file = File.join(path, ".require_paths")) then
      paths = File.read(file).split.map do |require_path|
        File.join path, require_path
      end

      require_paths.concat paths
    else
      require_paths << file if File.exist?(file = File.join(path, "bin"))
      require_paths << file if File.exist?(file = File.join(path, "lib"))
    end
  end

  # "tag" the first require_path inserted into the $LOAD_PATH to enable
  # indexing correctly with rubygems proper when it inserts an explicitly
  # gem version
  unless require_paths.empty?
    require_paths.first.instance_variable_set(:@gem_prelude_index, true)
  end
  # gem directories must come after -I and ENV['RUBYLIB']
  $:[$:.index(ConfigMap[:sitelibdir]),0] = require_paths
end

#push_gem_version_on_load_path(gem_name, *version_requirements) ⇒ Object



239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
# File 'lib/core/r4a/gems.rb', line 239

def push_gem_version_on_load_path(gem_name, *version_requirements)
  if version_requirements.empty?
    unless GemPaths.has_key?(gem_name)
      raise Gem::LoadError.new("Could not find RubyGem #{gem_name} (>= 0)\n")
    end

    # highest version gems already active
    return false
  else
    if version_requirements.length > 1
      QuickLoader.load_full_rubygems_library
      return gem(gem_name, *version_requirements)
    end

    requirement, version = version_requirements[0].split
    requirement.strip!

    if loaded_version = GemVersions[gem_name]
      case requirement
      when ">", ">="
        if (loaded_version <=> Gem.calculate_integers_for_gem_version(version)) >= 0
          return false
        end
      when "~>"
        required_version = Gem.calculate_integers_for_gem_version(version)
        if (loaded_version[0] == required_version[0])
          return false
        end
      end
    end

    QuickLoader.load_full_rubygems_library
    gem(gem_name, *version_requirements)
  end
end