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],
  ['-w', '--wid wid', 'Width of the table.',         :wid]
].freeze

Instance Method Summary collapse

Constructor Details

#initializeConfigurator

Returns a new instance of Configurator.



49
50
51
52
53
54
55
56
57
# File 'lib/renamr/configurator.rb', line 49

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)


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

def act?
  @options[:act]
end

#add(opt) ⇒ Object



43
44
45
46
47
# File 'lib/renamr/configurator.rb', line 43

def add(opt)
  add_cut(opt)
  add_prepend(opt)
  add_version(opt)
end

#add_cut(opt) ⇒ Object



22
23
24
25
26
27
# File 'lib/renamr/configurator.rb', line 22

def add_cut(opt)
  opt.on('-c', '--cut pos,len', Array, 'Removes symbols from pos.') do |l|
    @options[:pos] = l[0]
    @options[:len] = l[1]
  end
end

#add_prepend(opt) ⇒ Object



29
30
31
32
33
34
# File 'lib/renamr/configurator.rb', line 29

def add_prepend(opt)
  opt.on('-p', '--prepend str,beg', Array, 'Prepend a string.') do |l|
    @options[:pre] = l[0]
    @options[:beg] = l[1].nil? ? 0 : l[1].to_i
  end
end

#add_version(opt) ⇒ Object



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

def add_version(opt)
  opt.on('-v', '--version', 'Show version.') do
    puts "#{File.basename($PROGRAM_NAME)} #{VERSION} #{DATE}"
    exit
  end
end

#begObject



102
103
104
# File 'lib/renamr/configurator.rb', line 102

def beg
  @options[:beg]
end

#dirObject



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

def dir
  @options[:dir]
end

#dstObject



94
95
96
# File 'lib/renamr/configurator.rb', line 94

def dst
  @options[:dst]
end

#lenObject



110
111
112
# File 'lib/renamr/configurator.rb', line 110

def len
  @options[:len]
end

#lim?Boolean

Returns:

  • (Boolean)


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

def lim?
  @options[:lim]
end

#mod?Boolean

Returns:

  • (Boolean)


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

def mod?
  @options[:mod]
end

#posObject



106
107
108
# File 'lib/renamr/configurator.rb', line 106

def pos
  @options[:pos]
end

#preObject



98
99
100
# File 'lib/renamr/configurator.rb', line 98

def pre
  @options[:pre]
end

#rec?Boolean

Returns:

  • (Boolean)


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

def rec?
  @options[:rec]
end

#srcObject



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

def src
  @options[:src]
end

#validateObject



59
60
61
62
63
64
65
66
67
68
# File 'lib/renamr/configurator.rb', line 59

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



114
115
116
117
118
119
120
121
122
# File 'lib/renamr/configurator.rb', line 114

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