Sha256: 247e9e2b4eea454103ce044d733a78255bc65bd7015dcb8748cdd95ab4fd9142

Contents?: true

Size: 1.13 KB

Versions: 22

Compression:

Stored size: 1.13 KB

Contents

module Pansophy
  module Local
    class Directory
      include Adamantium

      def initialize(path)
        @path = path
        verify_directory!
      end

      def pathname
        Pathname.new(@path)
      end
      memoize :pathname

      def files
        entries.select(&:file?).map { |file| File.new(file) }
      end

      def create(options)
        remove(options)
        pathname.mkpath
      end

      def create_file(path, body, options = {})
        CreateFile.new(pathname.join(path), body).call(options)
      end

      private

      def entries
        Dir[pathname.join('**/*')].map { |entry| Pathname.new(entry) }
      end
      memoize :entries

      def verify_directory!
        return if pathname.directory? || !pathname.exist?
        fail ArgumentError, "#{pathname} is not a directory"
      end

      def remove(options)
        return unless pathname.exist?
        prevent_overwrite! unless options[:overwrite]
        pathname.rmtree
      end

      def prevent_overwrite!
        fail ArgumentError,
             "#{pathname} already exists, pass ':overwrite => true' to overwrite"
      end
    end
  end
end

Version data entries

22 entries across 22 versions & 1 rubygems

Version Path
pansophy-0.2.0 lib/pansophy/local/directory.rb
pansophy-0.1.0 lib/pansophy/local/directory.rb