Class: FunWith::Files::DirectoryBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/fun_with/files/directory_builder.rb

Overview

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ DirectoryBuilder

Returns a new instance of DirectoryBuilder.



9
10
11
12
13
14
# 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

Instance Attribute Details

#current_fileObject

Returns the value of attribute current_file.



75
76
77
# File 'lib/fun_with/files/directory_builder.rb', line 75

def current_file
  @current_file
end

#current_pathObject

Returns the value of attribute current_path.



7
8
9
# File 'lib/fun_with/files/directory_builder.rb', line 7

def current_path
  @current_path
end

Class Method Details

.create(path) {|builder| ... } ⇒ Object

Yields:

  • (builder)


16
17
18
19
20
# 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

.tmpdir(&block) ⇒ Object

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



30
31
32
33
34
35
36
37
38
39
40
# 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

Instance Method Details

#copy(src_filepath, dst_name = nil) ⇒ Object

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.

TODO: Improve testing, explain behavior better, need a way to distinguish between forceful and gentle copying



47
48
49
50
# File 'lib/fun_with/files/directory_builder.rb', line 47

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

#dir(*args, &block) ⇒ Object



22
23
24
25
26
# 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 = {}) ⇒ Object

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



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/fun_with/files/directory_builder.rb', line 86

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, &block) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/fun_with/files/directory_builder.rb', line 52

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) ⇒ Object

The actual method is installed by ‘fun_with_templates’



103
104
105
# File 'lib/fun_with/files/directory_builder.rb', line 103

def template( *args )
  raise "DirectoryBuilder cannot use template() function.  require 'fun_with_templates' to enable."
end