diff --git a/.gitignore b/.gitignore index c7aaca3..6656d47 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +.DS_Store /.bundle/ /.yardoc /Gemfile.lock diff --git a/bin/whereto b/bin/whereto index c95f21a..cf3a7d2 100755 --- a/bin/whereto +++ b/bin/whereto @@ -1,8 +1,10 @@ #!/usr/bin/env ruby require 'gli' require_relative '../lib/where_to' +require_relative '../lib/where_to_cli/helpers' include GLI::App +include WhereToCLI PROMPT_DESCRIPTORS = Hash.new do |hash, key| hash[key] = "a #{key}" @@ -55,13 +57,13 @@ command :organize do |cmd| cmd.action do |global_options, options, args| puts '' - location = args[0] - raise "The location '#{location}' does not exist" unless File.exist? location + location = Helpers::check_location!(args.first) @options = options Dir.chdir location files = Dir.glob "*#{options[:ext]}" + raise WhereTo::NoFilesError.new(options[:ext], location) if files.none? puts "I found the following files:" files.each { |file| puts file } puts '' diff --git a/lib/where_to.rb b/lib/where_to.rb index 1391a75..16706e8 100644 --- a/lib/where_to.rb +++ b/lib/where_to.rb @@ -1,7 +1,17 @@ require 'date' # Require all the libraries! -Dir[File.dirname(__FILE__) + "/where_to/**/*.rb"].each { |f| require_relative f } +require_files = %w( + configuration + episode_formatter + location + locator + tvdb + version + error/no_files_error +).each do |require_file| + require_relative "where_to/#{require_file}" +end module WhereTo class << self diff --git a/lib/where_to/error/no_files_error.rb b/lib/where_to/error/no_files_error.rb new file mode 100644 index 0000000..1b14651 --- /dev/null +++ b/lib/where_to/error/no_files_error.rb @@ -0,0 +1,7 @@ +module WhereTo + class NoFilesError < RuntimeError + def initialize(extension, location) + super("I couldn't find any files with the extension #{extension} in #{location}") + end + end +end diff --git a/lib/where_to/version.rb b/lib/where_to/version.rb index c035cbb..6fee9fb 100644 --- a/lib/where_to/version.rb +++ b/lib/where_to/version.rb @@ -1,3 +1,3 @@ module WhereTo - VERSION = "1.0.0" + VERSION = "1.0.1" end diff --git a/lib/where_to_cli/helpers.rb b/lib/where_to_cli/helpers.rb new file mode 100644 index 0000000..d755b9e --- /dev/null +++ b/lib/where_to_cli/helpers.rb @@ -0,0 +1,9 @@ +module WhereToCLI + module Helpers + def self.check_location!(location) + return '.' if location.nil? + raise "The location '#{location}' does not exist" unless File.exist? location + location + end + end +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 4c497ab..70f773c 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -10,4 +10,7 @@ require 'where_to/locator' require 'where_to/episode_formatter' require 'where_to/configuration' -require 'where_to/tvdb' \ No newline at end of file +require 'where_to/tvdb' + +require 'where_to_cli/helpers' + diff --git a/spec/where_to_cli/helpers_spec.rb b/spec/where_to_cli/helpers_spec.rb new file mode 100644 index 0000000..740cc59 --- /dev/null +++ b/spec/where_to_cli/helpers_spec.rb @@ -0,0 +1,25 @@ +require 'spec_helper' + +describe WhereToCLI::Helpers do + + describe ".check_location!" do + it "returns . if no location is given" do + expect(WhereToCLI::Helpers.check_location!(nil)).to eql '.' + end + + it "raises if the location does not exist" do + expect(File).to receive(:exist?).with('/some/location') { false } + + expect { + WhereToCLI::Helpers.check_location!('/some/location') + }.to raise_error "The location '/some/location' does not exist" + end + + it "returns a valid location" do + expect(File).to receive(:exist?).with('/some/location') { true } + + expect(WhereToCLI::Helpers.check_location!('/some/location')) + .to eql '/some/location' + end + end +end