Skip to content

Commit

Permalink
[Downloader] Add ::strategy_from_options
Browse files Browse the repository at this point in the history
  • Loading branch information
fabiopelosin committed Apr 15, 2014
1 parent 7e59cd4 commit a66c45e
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 14 deletions.
38 changes: 24 additions & 14 deletions lib/cocoapods-downloader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,21 @@ def self.downloader_class_by_key
}
end

# Identifies the concrete strategy for the given options.
#
# @param [Hash{Symbol}] options
# The options for which a strategy is needed.
#
# @return [Symbol] The symbol associated with a concrete strategy.
# @return [Nil] If no suitable concrete strategy could be selected.
#
def self.strategy_from_options(options)
common = downloader_class_by_key.keys & options.keys
if common.count == 1
common.first
end
end

# @return [Downloader::Base] A concrete downloader according to the
# options.
#
Expand All @@ -41,30 +56,25 @@ def self.for_target(target_path, options)
end

if options.nil? || options.empty?
raise DownloaderError, "No source url provided."
end

options = options.dup
klass = nil
url = nil
downloader_class_by_key.each do |key, key_klass|
url = options.delete(key)
if url
klass = key_klass
break
end
raise DownloaderError, "No source URL provided."
end

unless klass
strategy = strategy_from_options(options)
unless strategy
raise DownloaderError, "Unsupported download strategy " \
"`#{options.inspect}`."
end

url = options[strategy]
sub_options = options.dup
sub_options.delete(strategy)
klass = downloader_class_by_key[strategy]

if klass == Git && url.to_s =~ /github.com/
klass = GitHub
end

klass.new(target_path, url, options)
klass.new(target_path, url, sub_options)
end
end
end
24 changes: 24 additions & 0 deletions spec/cocoapods-downloaders_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,30 @@ module Downloader
end
end

describe '::strategy_from_options' do
it 'returns the strategy' do
options = {
:git => ''
}
@subject.strategy_from_options(options).should == :git
end

it 'returns nil if no strategy could be identified' do
options = {
:scm_from_future => ''
}
@subject.strategy_from_options(options).should.be.nil
end

it 'returns nil if no single strategy could be identified' do
options = {
:git => '',
:svn => '',
}
@subject.strategy_from_options(options).should.be.nil
end
end

describe '::for_target' do
it "returns the Git downloader" do
concrete = @subject.for_target(tmp_folder, :git => 'url')
Expand Down

0 comments on commit a66c45e

Please sign in to comment.