Sha256: a9e30cbce4b02247068cc91eb4e88f60dedae498fea7b3673af628fc43ce2021

Contents?: true

Size: 1.27 KB

Versions: 35

Compression:

Stored size: 1.27 KB

Contents

# frozen_string_literal: true

require 'eac_ruby_utils/core_ext'

module EhbrsRubyUtils
  module Videos
    module Series
      module Rename
        class File
          class Renamer
            enable_speaker
            common_constructor :owner
            delegate :current_name, :dirname, :file, :new_name, to: :owner

            def perform
              return unless rename?

              version_number = 0
              loop do
                target_path = dirname.join(build_target_name(version_number))
                break perform_rename(target_path) unless target_path.exist?

                version_number += 1
              end
            end

            private

            def build_target_name(version_number)
              return new_name unless version_number.positive?

              extname = ::File.extname(new_name)
              "#{::File.basename(new_name, extname)}.v#{version_number}#{extname}"
            end

            def perform_rename(target_path)
              infov 'Renaming', file.to_path
              ::FileUtils.mv(file.to_path, target_path.to_path)
            end

            def rename?
              return false unless new_name

              current_name != new_name
            end
          end
        end
      end
    end
  end
end

Version data entries

35 entries across 35 versions & 1 rubygems

Version Path
ehbrs_ruby_utils-0.44.2 lib/ehbrs_ruby_utils/videos/series/rename/file/renamer.rb
ehbrs_ruby_utils-0.44.1 lib/ehbrs_ruby_utils/videos/series/rename/file/renamer.rb
ehbrs_ruby_utils-0.44.0 lib/ehbrs_ruby_utils/videos/series/rename/file/renamer.rb
ehbrs_ruby_utils-0.41.1 lib/ehbrs_ruby_utils/videos/series/rename/file/renamer.rb
ehbrs_ruby_utils-0.41.0 lib/ehbrs_ruby_utils/videos/series/rename/file/renamer.rb
ehbrs_ruby_utils-0.40.0 lib/ehbrs_ruby_utils/videos/series/rename/file/renamer.rb
ehbrs_ruby_utils-0.39.0 lib/ehbrs_ruby_utils/videos/series/rename/file/renamer.rb
ehbrs_ruby_utils-0.38.0 lib/ehbrs_ruby_utils/videos/series/rename/file/renamer.rb
ehbrs_ruby_utils-0.37.0 lib/ehbrs_ruby_utils/videos/series/rename/file/renamer.rb
ehbrs_ruby_utils-0.36.1 lib/ehbrs_ruby_utils/videos/series/rename/file/renamer.rb
ehbrs_ruby_utils-0.36.0 lib/ehbrs_ruby_utils/videos/series/rename/file/renamer.rb
ehbrs_ruby_utils-0.35.0 lib/ehbrs_ruby_utils/videos/series/rename/file/renamer.rb
ehbrs_ruby_utils-0.33.0 lib/ehbrs_ruby_utils/videos/series/rename/file/renamer.rb
ehbrs_ruby_utils-0.32.0 lib/ehbrs_ruby_utils/videos/series/rename/file/renamer.rb
ehbrs_ruby_utils-0.31.0 lib/ehbrs_ruby_utils/videos/series/rename/file/renamer.rb
ehbrs_ruby_utils-0.30.0 lib/ehbrs_ruby_utils/videos/series/rename/file/renamer.rb
ehbrs_ruby_utils-0.29.0 lib/ehbrs_ruby_utils/videos/series/rename/file/renamer.rb
ehbrs_ruby_utils-0.28.0 lib/ehbrs_ruby_utils/videos/series/rename/file/renamer.rb
ehbrs_ruby_utils-0.27.1 lib/ehbrs_ruby_utils/videos/series/rename/file/renamer.rb
ehbrs_ruby_utils-0.27.0 lib/ehbrs_ruby_utils/videos/series/rename/file/renamer.rb