Class: FileRenamer::Commander
- Inherits:
-
Object
- Object
- FileRenamer::Commander
- Defined in:
- lib/filerenamer/commander.rb
Defined Under Namespace
Classes: NoRenameRuleError, OptionError
Instance Attribute Summary collapse
-
#files ⇒ Object
readonly
Returns the value of attribute files.
Class Method Summary collapse
Instance Method Summary collapse
-
#execute(&block) ⇒ Object
変更される名前のリストを表示し、ユーザの指示に従って実行する。 ユーザの入力は [y|Y] で始まる文字列ならば実行、 それ以外なら実行しない。 files は対象のファイル名のリスト。 新しい名前の生成方法をブロックで渡す。 ブロックがなければ例外 FileRenamerNoRenameRuleError.
-
#initialize(options, files) ⇒ Commander
constructor
:yes と :no が両方 true ならば例外。 :copy, :move, :hardlink, :symlink, :git のうち、1つ以下が true。 全て nil ならば :move が true になる。 :quiet が true ならば自動的に :yes が立てられる。 :quiet が true で :no も true ならば例外。.
Constructor Details
#initialize(options, files) ⇒ Commander
:yes と :no が両方 true ならば例外。:copy, :move, :hardlink, :symlink, :git のうち、1つ以下が true。全て nil ならば :move が true になる。:quiet が true ならば自動的に :yes が立てられる。:quiet が true で :no も true ならば例外。
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/filerenamer/commander.rb', line 60 def initialize(, files) @options = if (@options[:yes] && @options[:no]) raise OptionError, "Conflict options: --yes and --no." end fileProcessModes = [] [:move, :copy, :hardlink, :symlink, :git].each do |mode| fileProcessModes << mode if @options[mode] end # 1つもなければ :move に。 @options[:move] = true if fileProcessModes == [] # 2つ以上あれば例外。 if fileProcessModes.size > 1 raise OptionError, "File process modes duplicate: #{fileProcessModes.join(", ")}" end @command = "mv" if @options[:move] @command = "cp -r" if @options[:copy] @command = "ln" if @options[:hardlink] @command = "ln -s" if @options[:symlink] @command = "git mv" if @options[:git] # :quiet ならば自動的に :yes @options[:yes] = true if @options[:quiet] # :quiet と同時に :no なら例外 if @options[:quiet] && @options[:no] raise OptionError, "Conflict options: --quiet and --no" end @files = self.class.files(files) end |
Instance Attribute Details
#files ⇒ Object (readonly)
Returns the value of attribute files.
42 43 44 |
# File 'lib/filerenamer/commander.rb', line 42 def files @files end |
Class Method Details
.files(strs) ⇒ Object
47 48 49 50 51 52 53 |
# File 'lib/filerenamer/commander.rb', line 47 def self.files(strs) if strs.empty? return Dir::glob("*").sort else return strs end end |
Instance Method Details
#execute(&block) ⇒ Object
変更される名前のリストを表示し、ユーザの指示に従って実行する。ユーザの入力は [y|Y] で始まる文字列ならば実行、それ以外なら実行しない。files は対象のファイル名のリスト。新しい名前の生成方法をブロックで渡す。ブロックがなければ例外 FileRenamerNoRenameRuleError
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/filerenamer/commander.rb', line 103 def execute(&block) new_names = make_new_names(&block) ok_files, ng_files = check_new_names(new_names) unless @options[:quiet] if ok_files.size > 0 puts "Enable files:" max_width = ok_files.keys.max_by{|file| file.width}.width ok_files.keys.sort.each do |old| printf(" %s %s%s %s\n", @command, old, " " * (max_width - old.width), ok_files[old] ) end end if ng_files.size > 0 puts "Unable files:" max_width = ng_files.keys.max_by{|file| file.width}.width ng_files.keys.sort.each do |old| printf(" %s %s%s %s\n", @command, old, " " * (max_width - old.width), ng_files[old] ) end puts end end if ok_files.empty? puts "Done. No executable files." unless @options[:quiet] return elsif @options[:no] puts "Execute? no" return end if @options[:yes] puts "Execute? yes" unless @options[:quiet] elsif (! ask_yes?) return end ok_files.each do |old, new| run(old, new) end end |