Class: FunWith::Files::Utils::Succession

Inherits:
Object
  • Object
show all
Defined in:
lib/fun_with/files/utils/succession.rb

Class Method Summary collapse

Class Method Details

.format_counter(i, len) ⇒ Object



26
27
28
# File 'lib/fun_with/files/utils/succession.rb', line 26

def self.format_counter( i, len )
  sprintf( "%0#{len}i", i )
end

.get_successor_name(basename, digit_count) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/fun_with/files/utils/succession.rb', line 5

def self.get_successor_name( basename, digit_count )
  pieces = basename.to_s.split(".")
  
  if pieces.length == 0
    pieces = [ self.format_counter( 0, digit_count ) ]
  elsif is_counter?( pieces.last, digit_count )
    pieces = self.increment_position( pieces, pieces.length - 1 )
  elsif is_counter?( pieces[-2], digit_count )
    pieces = self.increment_position( pieces, pieces.length - 2 )
  else
    pieces = self.install_counter( pieces, digit_count )
  end
  
  pieces.join(".")
end

.increment_position(arr, pos_to_increment) ⇒ Object



30
31
32
33
34
35
36
37
38
# File 'lib/fun_with/files/utils/succession.rb', line 30

def self.increment_position( arr, pos_to_increment )
  arr.map.each_with_index do |str, i|
    if i == pos_to_increment
      self.format_counter( str.to_i + 1, str.length )
    else
      str
    end
  end
end

.install_counter(arr, count) ⇒ Object



40
41
42
43
# File 'lib/fun_with/files/utils/succession.rb', line 40

def self.install_counter( arr, count )
  counter = self.format_counter( 0, count )
  arr[0..-2] + [counter] + [arr[-1]]
end

.is_counter?(str, digit_count) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
24
# File 'lib/fun_with/files/utils/succession.rb', line 21

def self.is_counter?( str, digit_count )
  return false if str.nil?
  (str =~ /^\d{#{digit_count}}$/) != nil
end