Class: SpreenTracks::Application
- Inherits:
-
Object
- Object
- SpreenTracks::Application
- Defined in:
- lib/spreen_tracks/application.rb,
sig/generated/spreen_tracks/application.rbs
Overview
Walks the current directory looking for media files with the given extension
and rewrites their filenames so the space delimiter becomes a configurable
character (and N- disc prefixes become DiscN/ subdirectories).
Defined Under Namespace
Classes: InvalidModeError
Instance Attribute Summary collapse
-
#delimiter ⇒ Object
readonly
Returns the value of attribute delimiter.
-
#extension ⇒ Object
readonly
Returns the value of attribute extension.
-
#mode ⇒ Object
readonly
Returns the value of attribute mode.
-
#paths ⇒ Object
readonly
Returns the value of attribute paths.
Class Method Summary collapse
Instance Method Summary collapse
- #after(path) ⇒ String
- #announce_empty ⇒ void
- #announce_finish ⇒ void
- #announce_start ⇒ void
- #apply_renames ⇒ void
- #exec_mode ⇒ String
- #file_conversion_map ⇒ Hash[String, String]
-
#initialize(extension: '.m4a', delimiter: '_', mode: 'd') ⇒ Application
constructor
A new instance of Application.
- #rename(before, after) ⇒ void
- #replace ⇒ void
- #rewrite_filename(filename) ⇒ String
- #validate_mode! ⇒ void
Constructor Details
#initialize(extension: '.m4a', delimiter: '_', mode: 'd') ⇒ Application
Returns a new instance of Application.
27 28 29 30 31 32 |
# File 'lib/spreen_tracks/application.rb', line 27 def initialize(extension: '.m4a', delimiter: '_', mode: 'd') @paths = Dir[File.join('.', '**', "*#{extension}")] @extension = extension @delimiter = delimiter @mode = mode end |
Instance Attribute Details
#delimiter ⇒ Object (readonly)
Returns the value of attribute delimiter.
56 57 58 |
# File 'lib/spreen_tracks/application.rb', line 56 def delimiter @delimiter end |
#extension ⇒ Object (readonly)
Returns the value of attribute extension.
56 57 58 |
# File 'lib/spreen_tracks/application.rb', line 56 def extension @extension end |
#mode ⇒ Object (readonly)
Returns the value of attribute mode.
56 57 58 |
# File 'lib/spreen_tracks/application.rb', line 56 def mode @mode end |
#paths ⇒ Object (readonly)
Returns the value of attribute paths.
56 57 58 |
# File 'lib/spreen_tracks/application.rb', line 56 def paths @paths end |
Class Method Details
.run(extension: '.m4a', delimiter: '_', mode: 'd') ⇒ void
This method returns an undefined value.
17 18 19 20 21 |
# File 'lib/spreen_tracks/application.rb', line 17 def self.run(extension: '.m4a', delimiter: '_', mode: 'd') instance = new(extension:, delimiter:, mode:) instance.validate_mode! instance.replace end |
Instance Method Details
#after(path) ⇒ String
99 100 101 102 103 |
# File 'lib/spreen_tracks/application.rb', line 99 def after(path) elements = path.split('/') elements[-1] = rewrite_filename(elements.last) elements.join('/') end |
#announce_empty ⇒ void
This method returns an undefined value.
59 60 61 |
# File 'lib/spreen_tracks/application.rb', line 59 def announce_empty puts "========== [#{exec_mode}] No #{extension} Remains ==========" end |
#announce_finish ⇒ void
This method returns an undefined value.
71 72 73 74 |
# File 'lib/spreen_tracks/application.rb', line 71 def announce_finish puts "========== [#{exec_mode}] Done! ==========" puts "========== [#{exec_mode}] Total Target File Count: #{paths.length} ==========" end |
#announce_start ⇒ void
This method returns an undefined value.
64 65 66 67 68 |
# File 'lib/spreen_tracks/application.rb', line 64 def announce_start puts "========== [#{exec_mode}] Total File Count to Clean: #{paths.length} ==========" puts "========== [#{exec_mode}] The delimiters of those files will be replaced with `#{delimiter}` ==========" puts "========== [#{exec_mode}] Start! ==========" end |
#apply_renames ⇒ void
This method returns an undefined value.
77 78 79 80 81 82 |
# File 'lib/spreen_tracks/application.rb', line 77 def apply_renames file_conversion_map.each do |before, after| puts "========== [#{exec_mode}] Replacing the delimiter: `#{before}` => `#{after}` ==========" rename(before, after) if mode == 'e' end end |
#exec_mode ⇒ String
118 119 120 |
# File 'lib/spreen_tracks/application.rb', line 118 def exec_mode @exec_mode ||= mode == 'e' ? 'EXECUTION' : 'DRY RUN' end |
#file_conversion_map ⇒ Hash[String, String]
93 94 95 |
# File 'lib/spreen_tracks/application.rb', line 93 def file_conversion_map @file_conversion_map ||= paths.to_h { |path| [path, after(path)] } end |
#rename(before, after) ⇒ void
This method returns an undefined value.
87 88 89 90 |
# File 'lib/spreen_tracks/application.rb', line 87 def rename(before, after) FileUtils.mkdir_p(File.dirname(after)) if after.match?(%r{Disc\d{1}/}) FileUtils.mv(before, after) if before != after end |
#replace ⇒ void
This method returns an undefined value.
35 36 37 38 39 40 41 42 |
# File 'lib/spreen_tracks/application.rb', line 35 def replace puts "Target extension is `#{extension}`" return announce_empty if paths.empty? announce_start apply_renames announce_finish end |
#rewrite_filename(filename) ⇒ String
107 108 109 110 111 112 113 114 115 |
# File 'lib/spreen_tracks/application.rb', line 107 def rewrite_filename(filename) unless filename.match?(/^\d{1}-/) return filename.gsub(/(?<track_number>\d{2})\s/, "\\k<track_number>#{delimiter}") end filename .gsub(/^(?<disc_number>\d{1})-/, 'Disc\k<disc_number>/') .gsub(%r{(?<disc_number>Disc\d{1})/(?<track_number>\d{2})\s}, "\\k<disc_number>/\\k<track_number>#{delimiter}") end |
#validate_mode! ⇒ void
This method returns an undefined value.
45 46 47 48 49 50 51 52 |
# File 'lib/spreen_tracks/application.rb', line 45 def validate_mode! case mode when 'd', 'e' nil else raise InvalidModeError, "#{mode} is invalid mode. Provide either `d`(default) or `e`." end end |