Module: Nehm::PathManager

Defined in:
lib/nehm/path_manager.rb

Overview

Path manager works with download paths

Class Method Summary collapse

Class Method Details

.default_dl_pathObject



8
9
10
# File 'lib/nehm/path_manager.rb', line 8

def self.default_dl_path
  Cfg[:dl_path]
end

.get_path(path) ⇒ Object

Checks path for validation and returns it if valid



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/nehm/path_manager.rb', line 15

def self.get_path(path)
  unless Dir.exist?(path)
    UI.warning "This directory doesn't exist."
    wish = UI.ask('Want to create it? (Y/n):')
    wish = 'y' if wish == ''

    if wish.downcase =~ /y/
      UI.say "Creating directory: #{path}"
      UI.newline
      Dir.mkdir(File.expand_path(path), 0775)
    else
      UI.term
    end
  end

  File.expand_path(path)
end

.set_dl_pathObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/nehm/path_manager.rb', line 33

def self.set_dl_path
  loop do
    ask_sentence = 'Enter path to desirable download directory'
    default_path = File.join(ENV['HOME'], '/Music')

    if Dir.exist?(default_path)
      ask_sentence << " (press Enter to set it to #{default_path.magenta})"
    else
      default_path = nil
    end

    path = UI.ask(ask_sentence + ':')

    # If user press enter, set path to default
    path = default_path if path == '' && default_path

    if Dir.exist?(path)
      Cfg[:dl_path] = File.expand_path(path)
      UI.say "#{'Download directory set up to'.green} #{path.magenta}"
      break
    else
      UI.error "This directory doesn't exist. Please enter correct path"
    end
  end
end