Class: FunWith::Files::DirectoryBuilder
- 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
-
#current_file ⇒ Object
Returns the value of attribute current_file.
-
#current_path ⇒ Object
Returns the value of attribute current_path.
Class Method Summary collapse
- .create(path) {|builder| ... } ⇒ Object
-
.tmpdir(&block) ⇒ Object
Beware: if block is given, the temp directory will be.
Instance Method Summary collapse
-
#copy(src_filepath, dst_name = nil) ⇒ Object
Copies the given source file into a file in the current_path.
- #dir(*args, &block) ⇒ Object
-
#download(url, file = nil) ⇒ Object
if file not given, the result is appended to the current file.
- #file(name = nil, content = nil, &block) ⇒ Object
-
#initialize(path) ⇒ DirectoryBuilder
constructor
A new instance of DirectoryBuilder.
- #template(*args) ⇒ Object
Constructor Details
#initialize(path) ⇒ DirectoryBuilder
Returns a new instance of DirectoryBuilder.
9 10 11 12 13 |
# File 'lib/fun_with/files/directory_builder.rb', line 9 def initialize( path ) @paths = [] @current_path = path.fwf_filepath make_path end |
Instance Attribute Details
#current_file ⇒ Object
Returns the value of attribute current_file.
7 8 9 |
# File 'lib/fun_with/files/directory_builder.rb', line 7 def current_file @current_file end |
#current_path ⇒ Object
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
15 16 17 18 19 |
# File 'lib/fun_with/files/directory_builder.rb', line 15 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
29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/fun_with/files/directory_builder.rb', line 29 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.
43 44 45 46 |
# File 'lib/fun_with/files/directory_builder.rb', line 43 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
21 22 23 24 25 |
# File 'lib/fun_with/files/directory_builder.rb', line 21 def dir( *args, &block ) descend( *args ) do yield if block_given? end end |
#download(url, file = nil) ⇒ Object
if file not given, the result is appended to the current file.
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/fun_with/files/directory_builder.rb', line 76 def download( url, file = nil ) 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 ) else puts "No current file to append #{url} to." end end |
#file(name = nil, content = nil, &block) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/fun_with/files/directory_builder.rb', line 48 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
92 93 94 |
# File 'lib/fun_with/files/directory_builder.rb', line 92 def template( *args ) raise "DirectoryBuilder cannot use template() function. require 'fun_with_templates' to enable." end |