Sha256: a66d07b80e2209f77569a59710b2d0f5a0559b0f2f44d4d0a458ad9e3d460824

Contents?: true

Size: 1.33 KB

Versions: 1

Compression:

Stored size: 1.33 KB

Contents

# frozen_string_literal: true

require "stashify/file/local"
require "stashify/directory"

module Stashify
  class Directory
    class Local < Stashify::Directory
      attr_reader :path

      def initialize(path)
        @path = path
        super(name: ::File.basename(path))
      end

      def write_directory(directory)
        path = path_of(directory.name)
        FileUtils.mkdir(path)
        subdir = Stashify::Directory::Local.new(path)
        directory.files.each { |file| subdir.write(file) }
      end

      def write_file(file)
        ::File.write(path_of(file.name), file.contents)
      end

      def delete(name)
        path = ::File.join(@path, name)
        if ::File.directory?(path)
          FileUtils.rm_r(path)
        else
          ::File.delete(path)
        end
      end

      def files
        Dir.entries(@path).grep_v(/^[.][.]?$/).map do |file_name|
          find(::File.basename(file_name))
        end
      end

      def ==(other)
        @path == other.path
      end

      private

      def directory?(name)
        ::File.directory?(path_of(name))
      end

      def file(name)
        Stashify::File::Local.new(path_of(name))
      end

      def directory(name)
        Stashify::Directory::Local.new(path_of(name))
      end

      def path_of(name)
        ::File.join(@path, name)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
stashify-1.0.2 lib/stashify/directory/local.rb