Class: SequentialFile::CounterFinder

Inherits:
Object
  • Object
show all
Defined in:
lib/sequential_file/counter_finder.rb

Overview

This class takes a filename and an extension, and finds the counter (Integer) part of the file name

Constant Summary collapse

NUMBER_REGEX =
/\d*/

Instance Method Summary collapse

Constructor Details

#initialize(filename, extension) ⇒ CounterFinder

determines the counter value of the filename passed in. params:

filename - string, complete filename
extension - string, the part of the filename that is the extension (e.g. .log or .csv)


11
12
13
14
15
# File 'lib/sequential_file/counter_finder.rb', line 11

def initialize(filename, extension)
  @filename = File.basename(filename, extension)
  @length = @filename.length
  @index_of_extension_separator = @filename.rindex('.')
end

Instance Method Details

#counterObject

returns:

an integer representation of the counter


23
24
25
26
27
# File 'lib/sequential_file/counter_finder.rb', line 23

def counter
  has_extension_separator? ?
    @filename[@index_of_extension_separator + 1,@length][NUMBER_REGEX].to_i :
    0
end

#has_extension_separator?Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/sequential_file/counter_finder.rb', line 17

def has_extension_separator?
  !!@index_of_extension_separator
end