Sha256: 9bbf9194b94f9582bedbdbcde2704ae2d89694aae19bf36aa56361c0c8b5c51c

Contents?: true

Size: 1.42 KB

Versions: 1

Compression:

Stored size: 1.42 KB

Contents

# frozen_string_literal: true

module Stashify
  class Directory
    attr_reader :name, :files, :path

    def initialize(name: nil, path: nil, files: [])
      raise StandardError, "name or path must be defined" unless name || path

      @path = path
      @name = name || ::File.basename(path)
      @files = files
    end

    def find(name)
      if directory?(name)
        directory(name)
      elsif exists?(name)
        file(name)
      end
    end

    def write(file)
      if file.is_a?(Stashify::Directory)
        write_directory(file)
      else
        write_file(file)
      end
    end

    def write_directory(directory)
      subdir = self.directory(directory.name)
      directory.files.each { |file| subdir.write(file) }
    end

    def write_file(file)
      file(file.name).write(file.contents)
    end

    def delete(name)
      if directory?(name)
        delete_directory(name)
      else
        delete_file(name)
      end
    end

    def delete_directory(name)
      subdir = directory(name)
      subdir.files.each { |file| subdir.delete(file.name) }
    end

    def delete_file(name)
      file(name).delete
    end

    def ==(other)
      files == other.files
    end

    def eql?(other)
      self.class == other.class && name == other.name && path == other.path
    end

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

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
stashify-3.2.0 lib/stashify/directory.rb