class FunWith::Files::DirectoryBuilder

Describes a domain-specific language for creating and populating a directory of files.

Attributes

current_file[R]
current_path[RW]

Public Class Methods

create( path ) { |builder| ... } click to toggle source
# File lib/fun_with/files/directory_builder.rb, line 16
def self.create( path, &block )
  builder = self.new( path )
  yield builder if block_given?
  builder
end
new( path ) click to toggle source
# File lib/fun_with/files/directory_builder.rb, line 9
def initialize( path )
  @paths = []
  @current_path = path.fwf_filepath
  @current_file = nil
  make_path
end
tmpdir( ) { |builder| ... } click to toggle source

Beware: if block is given, the temp directory will be

# File lib/fun_with/files/directory_builder.rb, line 30
def self.tmpdir( &block )
  if block_given?
    FilePath.tmpdir do |dir|
      self.create( dir ) do |builder|
        yield builder
      end
    end
  else
    self.create( FilePath.tmpdir )
  end
end

Public Instance Methods

copy( src_filepath, dst_name = nil ) click to toggle source

Copies the given source file into a file in the current_path. If a dest_name is given, the new file will be given that name.

# File lib/fun_with/files/directory_builder.rb, line 44
def copy( src_filepath, dst_name = nil )
  dst_filepath = dst_name ? @current_path.join( dst_name ) : @current_path
  FileUtils.copy( src_filepath, dst_filepath )
end
current_file=( file ) click to toggle source
# File lib/fun_with/files/directory_builder.rb, line 74
def current_file=( file )
  @current_file = file.fwf_filepath
end
dir( *args ) { || ... } click to toggle source
# File lib/fun_with/files/directory_builder.rb, line 22
def dir( *args, &block )
  descend( *args ) do
    yield if block_given?
  end
end
download( url, file = nil, opts = {} ) click to toggle source

if file not given, the result is appended to the current file.

# File lib/fun_with/files/directory_builder.rb, line 83
def download( url, file = nil, opts = {} )
  if file
    if file.fwf_filepath.relative?
      file = FunWith::Files::FilePath.new( @current_path, file )
    end
      
    File.open( file, "w" ) do |f|
      download_to_target( url, f )
    end
  elsif @current_file
    download_to_target( url, @current_file, opts )
  else
    puts "No current file to append #{url} to."
  end
end
file( name = nil, content = nil ) { |current_file| ... } click to toggle source
# File lib/fun_with/files/directory_builder.rb, line 49
def file( name = nil, content = nil, &block )
  # if name && content
  #   begin
  #     f = open_file( name )
  #     f << content
  #   ensure
  #     close_file
  #   end
  if name
    open_file( name )
    @current_file << content if content
    if block_given?
      begin
        yield @current_file
      ensure
        close_file
      end
    end
  else
    @current_file
  end
end
template( *args ) click to toggle source
# File lib/fun_with/files/directory_builder.rb, line 99
def template( *args )
  raise "DirectoryBuilder cannot use template() function.  require 'fun_with_templates' to enable."
end

Protected Instance Methods

close_file() click to toggle source
# File lib/fun_with/files/directory_builder.rb, line 127
def close_file
  if @current_file
    @current_file.flush
    @current_file.close
  end
  
  @current_file = nil
end
descend( *args ) { || ... } click to toggle source
# File lib/fun_with/files/directory_builder.rb, line 108
def descend( *args, &block )
  if @current_path.directory?
    close_file
        @paths << @current_path
    @current_path = @paths.last.join( *args )
    make_path
        yield
    @current_path = @paths.pop
    close_file
  else
    raise "Cannot descend."
  end
end
download_to_target( url, file, signatures = {} ) click to toggle source
# File lib/fun_with/files/directory_builder.rb, line 136
def download_to_target( url, file, signatures = {} )
  Downloader.new.download( url, file, signatures )
end
make_path() click to toggle source
# File lib/fun_with/files/directory_builder.rb, line 104
def make_path
  FileUtils.mkdir_p( @current_path ) unless @current_path.exist?
end
open_file( name ) click to toggle source
# File lib/fun_with/files/directory_builder.rb, line 122
def open_file( name )
  close_file
  @current_file = File.open( @current_path.join( name ), "w" )
end