Class: Renamr::Configurator
- Inherits:
-
Object
- Object
- Renamr::Configurator
- 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
- #act? ⇒ Boolean
- #add(opt) ⇒ Object
- #add_cut(opt) ⇒ Object
- #add_prepend(opt) ⇒ Object
- #add_version(opt) ⇒ Object
- #beg ⇒ Object
- #dir ⇒ Object
- #dst ⇒ Object
-
#initialize ⇒ Configurator
constructor
A new instance of Configurator.
- #len ⇒ Object
- #lim? ⇒ Boolean
- #mod? ⇒ Boolean
- #pos ⇒ Object
- #pre ⇒ Object
- #rec? ⇒ Boolean
- #src ⇒ Object
- #validate ⇒ Object
- #wid ⇒ Object
Constructor Details
#initialize ⇒ Configurator
Returns a new instance of Configurator.
50 51 52 53 54 55 56 57 58 |
# File 'lib/renamr/configurator.rb', line 50 def initialize @options = {} OptionParser.new do |o| o. = "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
71 72 73 |
# File 'lib/renamr/configurator.rb', line 71 def act? @options[:act] end |
#add(opt) ⇒ Object
44 45 46 47 48 |
# File 'lib/renamr/configurator.rb', line 44 def add(opt) add_cut(opt) add_prepend(opt) add_version(opt) end |
#add_cut(opt) ⇒ Object
23 24 25 26 27 28 |
# File 'lib/renamr/configurator.rb', line 23 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
30 31 32 33 34 35 |
# File 'lib/renamr/configurator.rb', line 30 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
37 38 39 40 41 42 |
# File 'lib/renamr/configurator.rb', line 37 def add_version(opt) opt.on('-v', '--version', 'Show version.') do puts "#{File.basename($PROGRAM_NAME)} #{VERSION} #{DATE}" exit end end |
#beg ⇒ Object
103 104 105 |
# File 'lib/renamr/configurator.rb', line 103 def beg @options[:beg] end |
#dir ⇒ Object
87 88 89 |
# File 'lib/renamr/configurator.rb', line 87 def dir @options[:dir] end |
#dst ⇒ Object
95 96 97 |
# File 'lib/renamr/configurator.rb', line 95 def dst @options[:dst] end |
#len ⇒ Object
111 112 113 |
# File 'lib/renamr/configurator.rb', line 111 def len @options[:len] end |
#lim? ⇒ Boolean
79 80 81 |
# File 'lib/renamr/configurator.rb', line 79 def lim? @options[:lim] end |
#mod? ⇒ Boolean
83 84 85 |
# File 'lib/renamr/configurator.rb', line 83 def mod? @options[:mod] end |
#pos ⇒ Object
107 108 109 |
# File 'lib/renamr/configurator.rb', line 107 def pos @options[:pos] end |
#pre ⇒ Object
99 100 101 |
# File 'lib/renamr/configurator.rb', line 99 def pre @options[:pre] end |
#rec? ⇒ Boolean
75 76 77 |
# File 'lib/renamr/configurator.rb', line 75 def rec? @options[:rec] end |
#src ⇒ Object
91 92 93 |
# File 'lib/renamr/configurator.rb', line 91 def src @options[:src] end |
#validate ⇒ Object
60 61 62 63 64 65 66 67 68 69 |
# File 'lib/renamr/configurator.rb', line 60 def validate if dir.nil? @options[:dir] = Dir.pwd else raise "No such directory: #{dir}." unless File.directory?(dir) @options[:dir] = File.(dir) end raise "Width of the table should exceeds 14 symbols: #{wid}." if wid < 15 end |
#wid ⇒ Object
115 116 117 118 119 120 121 122 123 |
# File 'lib/renamr/configurator.rb', line 115 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 |