Class: Renamr::TruncateAction

Inherits:
Action
  • Object
show all
Defined in:
lib/renamr/truncate.rb

Overview

Truncates file names that exceed the length limit, preserving the extension.

Instance Method Summary collapse

Methods inherited from Action

#p2m, #set

Constructor Details

#initialize(lim) ⇒ TruncateAction

Returns a new instance of TruncateAction.



13
14
15
16
17
18
# File 'lib/renamr/truncate.rb', line 13

def initialize(lim)
  super()
  raise 'lim cannot be nil.' if lim.nil?

  @lim = lim
end

Instance Method Details

#do(src) ⇒ Object



20
21
22
23
24
25
26
27
28
29
# File 'lib/renamr/truncate.rb', line 20

def do(src)
  return src unless src.length > @lim

  ext = File.extname(src)
  len = ext.length
  dst = len >= @lim ? ext[0..(@lim - 1)] : src[0..(@lim - 1 - len)] << ext
  dst.gsub!(/-$/, '')
  dst.gsub!('-.', '.')
  dst
end