Class: Bundler::CLI::Inject

Inherits:
Object
  • Object
show all
Defined in:
lib/bundler/cli/inject.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options, name, version, gems) ⇒ Inject

Returns a new instance of Inject.



4
5
6
7
8
9
# File 'lib/bundler/cli/inject.rb', line 4

def initialize(options, name, version, gems)
  @options = options
  @name = name
  @version = version
  @gems = gems
end

Instance Attribute Details

#gemsObject (readonly)

Returns the value of attribute gems.



3
4
5
# File 'lib/bundler/cli/inject.rb', line 3

def gems
  @gems
end

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/bundler/cli/inject.rb', line 3

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



3
4
5
# File 'lib/bundler/cli/inject.rb', line 3

def options
  @options
end

#versionObject (readonly)

Returns the value of attribute version.



3
4
5
# File 'lib/bundler/cli/inject.rb', line 3

def version
  @version
end

Instance Method Details

#runObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/bundler/cli/inject.rb', line 11

def run
  # The required arguments allow Thor to give useful feedback when the arguments
  # are incorrect. This adds those first two arguments onto the list as a whole.
  gems.unshift(version).unshift(name)

  # Build an array of Dependency objects out of the arguments
  deps = []
  gems.each_slice(2) do |gem_name, gem_version|
    deps << Bundler::Dependency.new(gem_name, gem_version)
  end

  added = Injector.inject(deps)

  if added.any?
    Bundler.ui.confirm "Added to Gemfile:"
    Bundler.ui.confirm added.map{ |g| "  #{g}" }.join("\n")
  else
    Bundler.ui.confirm "All injected gems were already present in the Gemfile"
  end
end