Sha256: 2386c80609b9871b894d0462c835d896779ae098e917f9ae1cc7a6c0e2114f36

Contents?: true

Size: 1 KB

Versions: 4

Compression:

Stored size: 1 KB

Contents

module Construct
  module PathExtensions
    
    attr_accessor :construct__chdir_default
    
    def directory(path,chdir=construct__chdir_default)
      subdir = (self + path)
      subdir.mkpath
      subdir.extend(PathExtensions)
      subdir.maybe_change_dir(chdir) do
        yield(subdir) if block_given?
      end
      subdir
    end
    
    def file(filepath,contents=nil,&block)
      path = (self+filepath)
      path.dirname.mkpath
      File.open(path,'w') do |f|
        if(block)
          if(block.arity==1)
            block.call(f)
          else
            f << block.call
          end
        else
          f << contents
        end
      end
      path
    end

    def maybe_change_dir(chdir, &block)
      if(chdir)
        self.chdir(&block)
      else
        block.call
      end
    end

    # Note: Pathname implements #chdir directly, but it is deprecated in favor
    # of Dir.chdir
    def chdir(&block)
      Dir.chdir(self, &block)
    end

    def destroy!
      rmtree
    end

  end
end

Version data entries

4 entries across 4 versions & 2 rubygems

Version Path
devver-construct-1.1.0 lib/construct/path_extensions.rb
test-construct-1.2.2 lib/construct/path_extensions.rb
test-construct-1.2.1 lib/construct/path_extensions.rb
test-construct-1.2.0 lib/construct/path_extensions.rb