Class: Renamr::TruncateAction

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

Overview

Limits file length.

Instance Method Summary collapse

Methods inherited from Action

#p2m, #set

Constructor Details

#initialize(lim) ⇒ TruncateAction

Returns a new instance of TruncateAction.



11
12
13
14
15
# File 'lib/renamr/truncate.rb', line 11

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

  @lim = lim
end

Instance Method Details

#do(src) ⇒ Object



17
18
19
20
21
22
23
24
25
26
# File 'lib/renamr/truncate.rb', line 17

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