Sha256: 3ca7675ca8cc038c722b263cc0eb8839cc9b48901d16abeddfaaba0f45ff638a

Contents?: true

Size: 781 Bytes

Versions: 2

Compression:

Stored size: 781 Bytes

Contents

# frozen_string_literal: true

require 'pathname'
require 'fileutils'

module Flexdot
  class Backup
    BASE_DIR = 'backup'

    class AlreadyFinishedError < StandardError; end

    class << self
      def clear_all
        base_dir.glob('*').each(&:rmtree)
      end

      def base_dir
        Pathname.pwd.join(BASE_DIR)
      end
    end

    def initialize
      backup_dir.mkpath unless backup_dir.exist?
      @finished = false
    end

    def call(file)
      raise AlreadyFinishedError if @finished
      FileUtils.mv(file, backup_dir)
    end

    def finish!
      backup_dir.delete if backup_dir.empty?
      @finished = true
    end

    private

    def backup_dir
      @backup_dir ||= self.class.base_dir.join(Time.now.strftime('%Y%m%d%H%M%S'))
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
flexdot-3.2.0 lib/flexdot/backup.rb
flexdot-3.1.0 lib/flexdot/backup.rb