Class: Renamr::Configurator

Inherits:
Object
  • Object
show all
Defined in:
lib/renamr/configurator.rb

Overview

Handles input parameters.

Constant Summary collapse

DIC =
[
  ['-a', '--act',     'Real renaming.',              :act],
  ['-r', '--rec',     'Passes recursively.',         :rec],
  ['-l', '--lim',     'Limits name length.',         :lim],
  ['-m', '--mod',     'Prepends modification time.', :mod],
  ['-d', '--dir dir', 'Directory to rename.',        :dir],
  ['-s', '--src src', 'A string to substitute.',     :src],
  ['-t', '--dst dst', 'A string to replace to.',     :dst],
  ['-p', '--pre pre', 'A string to prepend to.',     :pre],
  ['-w', '--wid wid', 'Width of the table.',         :wid]
].freeze

Instance Method Summary collapse

Constructor Details

#initializeConfigurator

Returns a new instance of Configurator.



34
35
36
37
38
39
40
41
42
# File 'lib/renamr/configurator.rb', line 34

def initialize
  @options = {}
  OptionParser.new do |o|
    o.banner = "Usage: #{File.basename($PROGRAM_NAME)} [options]."
    DIC.each { |f, p, d, k| o.on(f, p, d) { |i| @options[k] = i } }
    add(o)
  end.parse!
  validate
end

Instance Method Details

#act?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/renamr/configurator.rb', line 55

def act?
  @options[:act]
end

#add(opt) ⇒ Object



23
24
25
26
27
28
29
30
31
32
# File 'lib/renamr/configurator.rb', line 23

def add(opt)
  opt.on('-c', '--cut pos,len', Array, 'Removes symbols from pos.') do |l|
    @options[:pos] = l[0]
    @options[:len] = l[1]
  end
  opt.on('-v', '--version', 'Show version.') do
    puts "#{File.basename($PROGRAM_NAME)} #{VERSION} #{DATE}"
    exit
  end
end

#dirObject



71
72
73
# File 'lib/renamr/configurator.rb', line 71

def dir
  @options[:dir]
end

#dstObject



79
80
81
# File 'lib/renamr/configurator.rb', line 79

def dst
  @options[:dst]
end

#lenObject



91
92
93
# File 'lib/renamr/configurator.rb', line 91

def len
  @options[:len]
end

#lim?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/renamr/configurator.rb', line 63

def lim?
  @options[:lim]
end

#mod?Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/renamr/configurator.rb', line 67

def mod?
  @options[:mod]
end

#posObject



87
88
89
# File 'lib/renamr/configurator.rb', line 87

def pos
  @options[:pos]
end

#preObject



83
84
85
# File 'lib/renamr/configurator.rb', line 83

def pre
  @options[:pre]
end

#rec?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/renamr/configurator.rb', line 59

def rec?
  @options[:rec]
end

#srcObject



75
76
77
# File 'lib/renamr/configurator.rb', line 75

def src
  @options[:src]
end

#validateObject



44
45
46
47
48
49
50
51
52
53
# File 'lib/renamr/configurator.rb', line 44

def validate
  if dir.nil?
    @options[:dir] = Dir.pwd
  else
    raise "No such directory: #{dir}." unless File.directory?(dir)

    @options[:dir] = File.expand_path(dir)
  end
  raise "Width of the table should exeeds 14 symbols: #{wid}." if wid < 15
end

#widObject



95
96
97
98
99
100
101
102
103
# File 'lib/renamr/configurator.rb', line 95

def wid
  if @options[:wid].nil?
    # Reads current terminal width.
    wid = `tput cols`
    wid.to_s.empty? ? 79 : wid.to_i
  else
    @options[:wid].to_i
  end
end