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-1.3.0 lib/pansophy/local/directory.rb
pansophy-1.2.0 lib/pansophy/local/directory.rb
pansophy-1.1.0 lib/pansophy/local/directory.rb
pansophy-1.0.0 lib/pansophy/local/directory.rb
pansophy-1.0.0.pre.alpha.0 lib/pansophy/local/directory.rb
pansophy-0.6.0 lib/pansophy/local/directory.rb
pansophy-0.5.6 lib/pansophy/local/directory.rb
pansophy-0.5.5 lib/pansophy/local/directory.rb
pansophy-0.5.4 lib/pansophy/local/directory.rb
pansophy-0.5.3 lib/pansophy/local/directory.rb
pansophy-0.5.2 lib/pansophy/local/directory.rb
pansophy-0.5.1 lib/pansophy/local/directory.rb
pansophy-0.5.0 lib/pansophy/local/directory.rb
pansophy-0.4.0 lib/pansophy/local/directory.rb
pansophy-0.3.0 lib/pansophy/local/directory.rb
pansophy-0.2.6 lib/pansophy/local/directory.rb
pansophy-0.2.5 lib/pansophy/local/directory.rb
pansophy-0.2.4 lib/pansophy/local/directory.rb
pansophy-0.2.3 lib/pansophy/local/directory.rb
pansophy-0.2.1 lib/pansophy/local/directory.rb