Class: FileRenamer::Commander
- Inherits:
-
Object
- Object
- FileRenamer::Commander
- Defined in:
- lib/filerenamer/commander.rb
Overview
ファイル群の名前変更を一括して行うためのクラス。他のアプリケーションからも使えるけれど、基本的にはコマンドライン関係の取り扱いを全部まとめてできるようにするもの。そのため、initialize で files を受け取ってしまい、execute の度に指定しなくても良いことにしている。execute の度に指定するようにすると、renpad のように対象全体を見てどうこうするコマンドで不便。
MEMO: 一度 temporary directory にリネームして格納し、これをカレントディレクトリからのパスに移動する形を取ると、ディレクトリをまたぐリネームが非常に面倒なことになる。
-
他のリネームに依存する場合に順序問題。
-
相互依存のデッドロック
gem などで提供される temporary directory は基本的に 抜けたときに削除される筈なので、途中まで変換してから interrupt されたときに中にあるファイルごと消される可能性がありそう。そのため、メソッド内で自分で temporary directory を作成する。
没案
rename_rule メソッドをデフォルトで例外にしておいて、
コマンドスクリプト側で定義するという方法は、
メソッドの引数で ArgumentError のもとになり、
自由度が少なくなる。
たとえば old_str, new_str を渡したい時と、
更新時刻でリネームしたいときでは必要な引数の数が異なる。
Defined Under Namespace
Classes: ModeError, 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] で始まる文字列ならば実行、 それ以外なら実行しない。 新しい名前の生成方法をブロックで渡す。 ブロックがなければ例外 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 ならば例外。
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 96 97 98 99 100 101 102 103 |
# File 'lib/filerenamer/commander.rb', line 61 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 # mode が1つもなければ :move に。 @options[:move] = true if fileProcessModes == [] # 2つ以上あれば例外。 if fileProcessModes.size > 1 raise OptionError, "File process modes duplicate: #{fileProcessModes.join(", ")}" end @mode = :move if @options[:move] @mode = :copy if @options[:copy] @mode = :hardlink if @options[:hardlink] @mode = :symlink if @options[:symlink] @mode = :git if @options[:git] case @mode when :move ; then; @command = "mv" when :copy ; then; @command = "cp -r" when :hardlink ; then; @command = "ln" when :symlink ; then; @command = "ln -s" when :git ; then; @command = "git mv" else raise ModeError, "Unknown mode: #{@mode}" end @options[:yes] = true if @options[:quiet] # :quiet ならば自動的に :yes if @options[:quiet] && @options[:no] # :quiet と同時に :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
48 49 50 51 52 53 54 |
# File 'lib/filerenamer/commander.rb', line 48 def self.files(strs) if strs.empty? return Dir::glob("*").sort else return strs end end |
Instance Method Details
#execute(&block) ⇒ Object
変更される名前のリストを表示し、ユーザの指示に従って実行する。ユーザの入力は [y|Y] で始まる文字列ならば実行、それ以外なら実行しない。新しい名前の生成方法をブロックで渡す。ブロックがなければ例外 FileRenamerNoRenameRuleError
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 |
# File 'lib/filerenamer/commander.rb', line 110 def execute(&block) new_names = make_new_names(&block) if @mode == :move || @mode == :git ro = FileRenamer::RenameOrderer.new(new_names) ok_files = ro.rename_processes ng_files = ro.unable_processes else ok_files = [] ng_files = [] new_names.each do |old, new| if File.exist? new ng_files << [old, new] else ok_files << [old, new] end end end show_conversions(ok_files, "Enable files:") #show_conversions(ng_files, "Unable files:") (puts "Execute? no"; return) if @options[:no] if ok_files.empty? puts "Done. No executable files." unless @options[:quiet] return end if @options[:yes] puts "Execute? yes" unless @options[:quiet] elsif (! ask_yes?) return end convert( ok_files) end |