diff options
author | Bundlerbot <bot@bundler.io> | 2019-05-09 11:34:32 +0000 |
---|---|---|
committer | Bundlerbot <bot@bundler.io> | 2019-05-09 11:34:32 +0000 |
commit | 3b3eaa9bd444043635a174cf1c7d70d012ed13a4 (patch) | |
tree | e6a7a47c05617ec5d4add1f50519682a2992633e | |
parent | b1c7297ccfd59879d4181d72b171f3ea88ac5fbd (diff) | |
parent | f76d664e2dc510d71e29850a2396ec05a4a65c91 (diff) | |
download | bundler-3b3eaa9bd444043635a174cf1c7d70d012ed13a4.tar.gz |
Merge #7118
7118: Refactors to `bundle add` r=deivid-rodriguez a=ryanfox1985
Thanks so much for the contribution!
To make reviewing this PR a bit easier, please fill out answers to the following questions.
### What was the end-user problem that led to this PR?
The problem was no problem just code refactors.
### What was your diagnosis of the problem?
My diagnosis was the possibility to reorganize the code to make clean and more readable.
### What is your fix for the problem, implemented in this PR?
My fix reorganize the code, good usage of the attributes.
Co-authored-by: Guillermo Guerrero <wolf.fox1985@gmail.com>
-rw-r--r-- | lib/bundler/cli/add.rb | 43 |
1 files changed, 27 insertions, 16 deletions
diff --git a/lib/bundler/cli/add.rb b/lib/bundler/cli/add.rb index 9709e71be0..7c6235f17c 100644 --- a/lib/bundler/cli/add.rb +++ b/lib/bundler/cli/add.rb @@ -2,34 +2,45 @@ module Bundler class CLI::Add + attr_reader :gems, :options, :version + def initialize(options, gems) @gems = gems @options = options - @options[:group] = @options[:group].split(",").map(&:strip) if !@options[:group].nil? && !@options[:group].empty? + @options[:group] = options[:group].split(",").map(&:strip) unless options[:group].nil? + @version = options[:version].split(",").map(&:strip) unless options[:version].nil? end def run - raise InvalidOption, "You can not specify `--strict` and `--optimistic` at the same time." if @options[:strict] && @options[:optimistic] - - # raise error when no gems are specified - raise InvalidOption, "Please specify gems to add." if @gems.empty? + validate_options! + inject_dependencies + perform_bundle_install unless options["skip-install"] + end - version = @options[:version].nil? ? nil : @options[:version].split(",").map(&:strip) + private - unless version.nil? - version.each do |v| - raise InvalidOption, "Invalid gem requirement pattern '#{v}'" unless Gem::Requirement::PATTERN =~ v.to_s - end - end + def perform_bundle_install + Installer.install(Bundler.root, Bundler.definition) + end - dependencies = @gems.map {|g| Bundler::Dependency.new(g, version, @options) } + def inject_dependencies + dependencies = gems.map {|g| Bundler::Dependency.new(g, version, options) } Injector.inject(dependencies, - :conservative_versioning => @options[:version].nil?, # Perform conservative versioning only when version is not specified - :optimistic => @options[:optimistic], - :strict => @options[:strict]) + :conservative_versioning => options[:version].nil?, # Perform conservative versioning only when version is not specified + :optimistic => options[:optimistic], + :strict => options[:strict]) + end + + def validate_options! + raise InvalidOption, "You can not specify `--strict` and `--optimistic` at the same time." if options[:strict] && options[:optimistic] - Installer.install(Bundler.root, Bundler.definition) unless @options["skip-install"] + # raise error when no gems are specified + raise InvalidOption, "Please specify gems to add." if gems.empty? + + version.to_a.each do |v| + raise InvalidOption, "Invalid gem requirement pattern '#{v}'" unless Gem::Requirement::PATTERN =~ v.to_s + end end end end |