Class: Nrename::NumberedFile

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/nrename/numbered_file.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, dir) ⇒ NumberedFile

Returns a new instance of NumberedFile.



14
15
16
17
# File 'lib/nrename/numbered_file.rb', line 14

def initialize(path, dir)
  @path = path
  @dir = dir
end

Instance Attribute Details

#dirObject (readonly)

Returns the value of attribute dir.



7
8
9
# File 'lib/nrename/numbered_file.rb', line 7

def dir
  @dir
end

#pathObject (readonly)

Returns the value of attribute path.



7
8
9
# File 'lib/nrename/numbered_file.rb', line 7

def path
  @path
end

Instance Method Details

#adjusted_numberObject



55
56
57
# File 'lib/nrename/numbered_file.rb', line 55

def adjusted_number
  number.to_s.rjust num_field_length, '0'
end

#normalizeObject



27
28
29
# File 'lib/nrename/numbered_file.rb', line 27

def normalize
  rename_to normalized_path
end

#normalized_pathObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/nrename/numbered_file.rb', line 40

def normalized_path
  dirname, filename = path.split
  new_filename = filename.to_s

  if options.numbers_only
    name = adjusted_number
    extname = filename.extname
    new_filename = "#{name}#{extname}"
  else
    new_filename[options.pattern, 1] = adjusted_number
  end

  dirname + new_filename
end

#numberObject



19
20
21
# File 'lib/nrename/numbered_file.rb', line 19

def number
  @number ||= options.renumber ? counter.next : number_from_path
end

#number_from_pathObject



23
24
25
# File 'lib/nrename/numbered_file.rb', line 23

def number_from_path
  path.basename.to_s[options.pattern, 1].to_i
end

#rename_to(new_name) ⇒ Object



31
32
33
34
35
36
37
38
# File 'lib/nrename/numbered_file.rb', line 31

def rename_to(new_name)
  return if path == new_name

  FileUtils.mv path, new_name, {
    :noop    => !options.execute,
    :verbose => options.verbose
  }
end