Class: Rails::GemDependency

Inherits:
Object show all
Defined in:
lib/rails/gem_dependency.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options = {}) ⇒ GemDependency

Returns a new instance of GemDependency.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/rails/gem_dependency.rb', line 9

def initialize(name, options = {})
  require 'rubygems' unless Object.const_defined?(:Gem)

  if options[:requirement]
    @requirement = options[:requirement]
  elsif options[:version]
    @requirement = Gem::Requirement.create(options[:version])
  end

  @version  = @requirement.instance_variable_get("@requirements").first.last if @requirement
  @name     = name.to_s
  @lib      = options[:lib]
  @source   = options[:source]
  @loaded   = @frozen = @load_paths_added = false
  @unpack_directory = nil
end

Instance Attribute Details

#libObject

Returns the value of attribute lib.



3
4
5
# File 'lib/rails/gem_dependency.rb', line 3

def lib
  @lib
end

#nameObject

Returns the value of attribute name.



3
4
5
# File 'lib/rails/gem_dependency.rb', line 3

def name
  @name
end

#requirementObject

Returns the value of attribute requirement.



3
4
5
# File 'lib/rails/gem_dependency.rb', line 3

def requirement
  @requirement
end

#sourceObject

Returns the value of attribute source.



3
4
5
# File 'lib/rails/gem_dependency.rb', line 3

def source
  @source
end

#versionObject

Returns the value of attribute version.



3
4
5
# File 'lib/rails/gem_dependency.rb', line 3

def version
  @version
end

Class Method Details

.unpacked_pathObject



5
6
7
# File 'lib/rails/gem_dependency.rb', line 5

def self.unpacked_path
  @unpacked_path ||= File.join(RAILS_ROOT, 'vendor', 'gems')
end

Instance Method Details

#==(other) ⇒ Object



101
102
103
# File 'lib/rails/gem_dependency.rb', line 101

def ==(other)
  self.name == other.name && self.requirement == other.requirement
end

#add_load_pathsObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/rails/gem_dependency.rb', line 30

def add_load_paths
  return if @loaded || @load_paths_added
  unpacked_paths = self.unpacked_paths
  if unpacked_paths.empty?
    args = [@name]
    args << @requirement.to_s if @requirement
    gem *args
  else
    $LOAD_PATH.unshift File.join(unpacked_paths.first, 'lib')
    ext = File.join(unpacked_paths.first, 'ext')
    $LOAD_PATH.unshift(ext) if File.exist?(ext)
    @frozen = true
  end
  @load_paths_added = true
rescue Gem::LoadError
end

#dependenciesObject



47
48
49
50
51
52
53
# File 'lib/rails/gem_dependency.rb', line 47

def dependencies
  all_dependencies = specification.dependencies.map do |dependency|
    GemDependency.new(dependency.name, :requirement => dependency.version_requirements)
  end
  all_dependencies += all_dependencies.map(&:dependencies).flatten
  all_dependencies.uniq
end

#frozen?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/rails/gem_dependency.rb', line 68

def frozen?
  @frozen
end

#gem_dir(base_directory) ⇒ Object



55
56
57
# File 'lib/rails/gem_dependency.rb', line 55

def gem_dir(base_directory)
  File.join(base_directory, specification.full_name)
end

#installObject



80
81
82
83
84
# File 'lib/rails/gem_dependency.rb', line 80

def install
  cmd = "#{gem_command} #{install_command.join(' ')}"
  puts cmd
  puts %x(#{cmd})
end

#loadObject



59
60
61
62
63
64
65
66
# File 'lib/rails/gem_dependency.rb', line 59

def load
  return if @loaded || @load_paths_added == false
  require(@lib || @name) unless @lib == false
  @loaded = true
rescue LoadError
  puts $!.to_s
  $!.backtrace.each { |b| puts b }
end

#load_paths_added?Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/rails/gem_dependency.rb', line 76

def load_paths_added?
  @load_paths_added
end

#loaded?Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/rails/gem_dependency.rb', line 72

def loaded?
  @loaded
end

#specificationObject



105
106
107
# File 'lib/rails/gem_dependency.rb', line 105

def specification
  @spec ||= Gem.source_index.search(Gem::Dependency.new(@name, @requirement)).sort_by { |s| s.version }.last
end

#unpack_to(directory) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/rails/gem_dependency.rb', line 86

def unpack_to(directory)
  FileUtils.mkdir_p directory
  Dir.chdir directory do
    Gem::GemRunner.new.run(unpack_command)
  end

  # copy the gem's specification into GEMDIR/.specification so that
  # we can access information about the gem on deployment systems
  # without having the gem installed
  spec_filename = File.join(gem_dir(directory), '.specification')
  File.open(spec_filename, 'w') do |file|
    file.puts specification.to_yaml
  end
end

#unpacked_pathsObject



26
27
28
# File 'lib/rails/gem_dependency.rb', line 26

def unpacked_paths
  Dir[File.join(self.class.unpacked_path, "#{@name}-#{@version || "*"}")]
end