Sha256: 975512caac10c933707f3d7c44c96399d2dee259737d0eccebe3d05faf5fffd1

Contents?: true

Size: 1019 Bytes

Versions: 4

Compression:

Stored size: 1019 Bytes

Contents

# frozen_string_literal: true

module Expire
  # Reads contents of a directory and returns a corresponding BackupList
  class GenerateBackupListService
    def self.call(path)
      new(path).call
    end

    def initialize(path)
      @path = path
    end

    attr_reader :path

    def call
      if path == "-"
        generate_backup_list_from($stdin)
      else
        pathname = Pathname.new(path)
        raise InvalidPathError, "#{pathname} does not exit" unless pathname.exist?

        generate_backup_list_from(pathname.children)
      end
    end

    private

    def generate_backup_list_from(source)
      backup_list = BackupList.new

      source.each do |backup_path|
        backup_list << BackupFromPathService.call(path: purify_backup_path(backup_path))
      end

      backup_list
    end

    # backup_path may be a String or a Pathname so we call #to_s to
    # ensure that chomp works as expected
    def purify_backup_path(backup_path)
      backup_path.to_s.chomp.strip
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
expire-0.2.6 lib/expire/generate_backup_list_service.rb
expire-0.2.5 lib/expire/generate_backup_list_service.rb
expire-0.2.4 lib/expire/generate_backup_list_service.rb
expire-0.2.3 lib/expire/generate_backup_list_service.rb