class FilenameIncrement
Constants
- ORDINALS
- VERSION
Attributes
path[R]
Public Class Methods
new(filepath, options = {})
click to toggle source
# File lib/filename_increment.rb, line 13 def initialize(filepath, options = {}) @options = options @platform = @options[:platform] || Gem::Platform.local.os @file = parse(filepath) @start = @options[:start] || 1 @file = parse(filepath) if @start == 1 && (@platform == 'win32' || @platform == 'windows') @start += 1 end if @options[:fs].is_a? TrueClass if File.exist?(filepath) name = @file[:name] dest = filepath while File.exist?(dest) @start += 1 @file[:basename] = format(name, @start) + @file[:extname] dest = File.join(@file[:dirname], @file[:basename]) end else # raise error if file not found raise FileNotFound.new('File not found') end else @file[:basename] = format(@file[:name], @start) + @file[:extname] end @path = Pathname.new(@file[:dirname]).join(@file[:basename]) end
Public Instance Methods
to_s()
click to toggle source
# File lib/filename_increment.rb, line 48 def to_s @path.cleanpath.to_s end
Private Instance Methods
format(stem, number)
click to toggle source
# File lib/filename_increment.rb, line 78 def format(stem, number) case @platform when 'win32', 'windows' number > 1 ? "#{stem} (#{number})" : stem when 'linux' if number == 0 stem elsif number == 1 "#{stem} (copy)" elsif number == 2 "#{stem} (another copy)" else "#{stem} (#{to_ordinal(number)} copy)" end when 'darwin' if number == 1 "#{stem} copy" elsif number > 1 "#{stem} (#{stem} copy #{number})" end else number > 1 ? "#{stem} (#{number})" : stem end end
ordinal(number)
click to toggle source
# File lib/filename_increment.rb, line 66 def ordinal(number) raise TypeError.new('expected a number') unless number.is_a? Numeric ORDINALS[((number % 100) - 20) % 10] || ORDINALS[number % 100] || ORDINALS[0] end
parse(filepath)
click to toggle source
# File lib/filename_increment.rb, line 54 def parse(filepath) pathname = Pathname.new(filepath) path = {path: filepath} path[:dirname] = pathname.dirname path[:basename] = pathname.basename path[:name] = pathname.basename(pathname.extname) path[:extname] = pathname.extname path end
to_ordinal(number)
click to toggle source
# File lib/filename_increment.rb, line 72 def to_ordinal(number) raise TypeError.new('expected a number') unless number.is_a? Numeric "#{number}#{ordinal(number)}" end