Class: Reactive::GemDependency

Inherits:
Object
  • Object
show all
Defined in:
lib/reactive-core/gem_dependency.rb

Overview

:nodoc:

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of GemDependency.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/reactive-core/gem_dependency.rb', line 5

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

  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]
  @init = options[:init]
  @loaded   = false
end

Instance Attribute Details

#libObject

Returns the value of attribute lib.



3
4
5
# File 'lib/reactive-core/gem_dependency.rb', line 3

def lib
  @lib
end

#nameObject

Returns the value of attribute name.



3
4
5
# File 'lib/reactive-core/gem_dependency.rb', line 3

def name
  @name
end

#requirementObject

Returns the value of attribute requirement.



3
4
5
# File 'lib/reactive-core/gem_dependency.rb', line 3

def requirement
  @requirement
end

#sourceObject

Returns the value of attribute source.



3
4
5
# File 'lib/reactive-core/gem_dependency.rb', line 3

def source
  @source
end

#versionObject

Returns the value of attribute version.



3
4
5
# File 'lib/reactive-core/gem_dependency.rb', line 3

def version
  @version
end

Class Method Details

.missing_gemsObject

Returns an Array of missing gems



223
224
225
226
227
228
# File 'lib/reactive-core/gem_dependency.rb', line 223

def self.missing_gems
  Reactive.configuration.gems.inject([]) do |missing, gem|
    missing << gem unless gem.installed?
    gem.dependencies.inject(missing) {|missing, dependency| dependency.installed? ? missing : missing << dependency}
  end.uniq
end

.report_gems_state(with_dependencies = true) ⇒ Object



201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/reactive-core/gem_dependency.rb', line 201

def self.report_gems_state(with_dependencies = true)
  Reactive.configuration.gems.inject('') do |report, gem|
    installed_dependency_count = 0
    dependencies_status = gem.dependencies.collect do |dependency|
      if dependency.installed?
        code = dependency.embedded? ? "E" : "I"
        installed_dependency_count += 1
      else
        code = " "
      end
      "  [#{code}] #{dependency.name} #{dependency.requirement.to_s}\n"
    end.uniq

    code = gem.installed? ? (gem.embedded? ? "E" : "I") : " "
    code.downcase! if installed_dependency_count != gem.dependencies.size
    report << "[#{code}] #{gem.name} #{gem.requirement.to_s}\n"
    report << dependencies_status.join('') if with_dependencies
    report
  end << "I = Installed, i = Installed but missing dependencies\nE = Embedded,  e = Embedded but missing dependencies"
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



107
108
109
# File 'lib/reactive-core/gem_dependency.rb', line 107

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

#dependenciesObject



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/reactive-core/gem_dependency.rb', line 24

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

#embedObject



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/reactive-core/gem_dependency.rb', line 89

def embed
  return "#{name} #{requirement} is already embedded." if embedded?
  # If the gem is already installed, use Gem::Installer to embed because we only need gem files and the spec
  if installed?
    require 'rubygems/installer'
    path = get_gem_path(name, requirement)
    basename = File.basename(path).sub(/\.gem$/, '')
    installer = Gem::Installer.new(path, :install_dir => Reactive.path_for('gems'))
    installer.unpack Reactive.path_for('gems', 'gems', basename)
    installer.write_spec
    @embedded = true
    "Successfully embedded #{name} #{installer.spec.version}"
  else
    cmd = "#{gem_command} #{install_command.join(' ')} -i #{Reactive.path_for('gems')} --ignore-dependencies"
    [cmd, %x(#{cmd})]
  end
end

#embedded?Boolean

Returns:

  • (Boolean)


66
67
68
69
70
# File 'lib/reactive-core/gem_dependency.rb', line 66

def embedded?
  return true if @embedded
  return false unless specification
  File.expand_path(specification.installation_path) == File.expand_path(Reactive.path_for('gems'))
end

#hashObject



111
112
113
# File 'lib/reactive-core/gem_dependency.rb', line 111

def hash
  "#{name}#{requirement}".hash
end

#init_pluginObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/reactive-core/gem_dependency.rb', line 46

def init_plugin
  if @init == :rails
    require(@lib) if @lib
  else
    require(@lib || @name) unless @lib == false
  end
  gem_path = File.expand_path(specification.full_gem_path)
  specification.require_paths.map {|path| File.join(gem_path, path) }.each do |path|
    begin
      require File.join(path, 'reactive', 'init.rb')
      return
    rescue LoadError
      # No reactive init code, let's try a Rails init code
      if @init == :rails
        return if load_rails_init(path)
      end
    end
  end
end

#installObject



84
85
86
87
# File 'lib/reactive-core/gem_dependency.rb', line 84

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

#installed?Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/reactive-core/gem_dependency.rb', line 72

def installed?
  loaded? || specification
end

#loadObject



36
37
38
39
40
41
42
43
44
# File 'lib/reactive-core/gem_dependency.rb', line 36

def load
  return if @loaded
  args = [@name]
  args << @requirement.to_s if @requirement
  gem *args
  # Now, run its reactive init code
  init_plugin
  @loaded = true
end

#loaded?Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/reactive-core/gem_dependency.rb', line 76

def loaded?
  @loaded
end

#loaded_specObject



80
81
82
# File 'lib/reactive-core/gem_dependency.rb', line 80

def loaded_spec
  Gem.loaded_specs[@name]
end