Class: Nrename::NumberedFile
- Inherits:
-
Object
- Object
- Nrename::NumberedFile
- Extended by:
- Forwardable
- Defined in:
- lib/nrename/numbered_file.rb
Instance Attribute Summary collapse
-
#dir ⇒ Object
readonly
Returns the value of attribute dir.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Instance Method Summary collapse
- #adjusted_number ⇒ Object
-
#initialize(path, dir) ⇒ NumberedFile
constructor
A new instance of NumberedFile.
- #normalize ⇒ Object
- #normalized_path ⇒ Object
- #number ⇒ Object
- #number_from_path ⇒ Object
- #rename_to(new_name) ⇒ Object
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
#dir ⇒ Object (readonly)
Returns the value of attribute dir.
7 8 9 |
# File 'lib/nrename/numbered_file.rb', line 7 def dir @dir end |
#path ⇒ Object (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_number ⇒ Object
55 56 57 |
# File 'lib/nrename/numbered_file.rb', line 55 def adjusted_number number.to_s.rjust num_field_length, '0' end |
#normalize ⇒ Object
27 28 29 |
# File 'lib/nrename/numbered_file.rb', line 27 def normalize rename_to normalized_path end |
#normalized_path ⇒ Object
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 .numbers_only name = adjusted_number extname = filename.extname new_filename = "#{name}#{extname}" else new_filename[.pattern, 1] = adjusted_number end dirname + new_filename end |
#number ⇒ Object
19 20 21 |
# File 'lib/nrename/numbered_file.rb', line 19 def number @number ||= .renumber ? counter.next : number_from_path end |
#number_from_path ⇒ Object
23 24 25 |
# File 'lib/nrename/numbered_file.rb', line 23 def number_from_path path.basename.to_s[.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 => !.execute, :verbose => .verbose } end |