Class: Lhj::Command::RenameImage
- Inherits:
-
Lhj::Command
- Object
- CLAide::Command
- Lhj::Command
- Lhj::Command::RenameImage
- Defined in:
- lib/lhj/command/rename_image.rb
Class Method Summary collapse
Instance Method Summary collapse
- #handle ⇒ Object
-
#initialize(argv) ⇒ RenameImage
constructor
A new instance of RenameImage.
- #modify_name(file_name, extname) ⇒ Object
- #rename_image ⇒ Object
- #validate! ⇒ Object
Methods inherited from Lhj::Command
Constructor Details
#initialize(argv) ⇒ RenameImage
Returns a new instance of RenameImage.
25 26 27 28 29 30 31 |
# File 'lib/lhj/command/rename_image.rb', line 25 def initialize(argv) @current_path = argv.shift_argument || Dir.pwd @image_pre = argv.option('pre') @image_name = argv.option('name') @image_other_name = argv.option('other') super end |
Class Method Details
.options ⇒ Object
12 13 14 15 16 17 18 |
# File 'lib/lhj/command/rename_image.rb', line 12 def self. [ %w[--pre 图片前缀], %w[--name 图片名称], %w[--other 图片变化后的名称] ] end |
Instance Method Details
#handle ⇒ Object
33 34 35 |
# File 'lib/lhj/command/rename_image.rb', line 33 def handle rename_image end |
#modify_name(file_name, extname) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/lhj/command/rename_image.rb', line 46 def modify_name(file_name, extname) name = file_name.downcase + extname if @image_pre name = @image_pre + file_name.downcase + extname elsif @image_name && @image_other_name if file_name =~ /#{@image_name}/ other_name = file_name.gsub(/#{@image_name}/, @image_other_name) name = other_name + extname end end name end |
#rename_image ⇒ Object
37 38 39 40 41 42 43 44 |
# File 'lib/lhj/command/rename_image.rb', line 37 def rename_image Dir.glob("#{@current_path}/**/*.{png}").sort.each do |f| filename = File.basename(f, File.extname(f)) m_filename = modify_name(filename, File.extname(f)) target = File.join(@current_path, m_filename) File.rename(f, target) end end |
#validate! ⇒ Object
20 21 22 23 |
# File 'lib/lhj/command/rename_image.rb', line 20 def validate! super help! '输入图片信息' unless @image_pre || @image_name || @image_other_name end |