Module: PoolParty::FileWriter
- Included in:
- PoolParty
- Defined in:
- lib/poolparty/modules/file_writer.rb
Instance Method Summary collapse
- #clear_base_directory ⇒ Object
- #copy_file_to_storage_directory(file) ⇒ Object
- #copy_template_to_storage_directory(file) ⇒ Object
- #make_base_directory ⇒ Object
- #make_base_path(path) ⇒ Object
- #make_template_directory ⇒ Object
- #write_to_file(file, str, &block) ⇒ Object
- #write_to_file_in_storage_directory(file, str, &block) ⇒ Object
-
#write_to_temp_file(str = "", &block) ⇒ Object
Write a temp file with the content str and return the Tempfile It creates a random file name.
Instance Method Details
#clear_base_directory ⇒ Object
51 52 53 |
# File 'lib/poolparty/modules/file_writer.rb', line 51 def clear_base_directory FileUtils::rm_rf "#{Base.storage_directory}" end |
#copy_file_to_storage_directory(file) ⇒ Object
3 4 5 6 7 |
# File 'lib/poolparty/modules/file_writer.rb', line 3 def copy_file_to_storage_directory(file) make_base_directory path = ::File.join( Base.storage_directory, ::File.basename(file) ) FileUtils.cp file, path end |
#copy_template_to_storage_directory(file) ⇒ Object
8 9 10 11 12 |
# File 'lib/poolparty/modules/file_writer.rb', line 8 def copy_template_to_storage_directory(file) make_template_directory path = ::File.join( Base.template_directory, ::File.basename(file) ) FileUtils.cp file, path end |
#make_base_directory ⇒ Object
45 46 47 |
# File 'lib/poolparty/modules/file_writer.rb', line 45 def make_base_directory FileUtils.mkdir_p Base.storage_directory unless ::File.directory?(Base.storage_directory) end |
#make_base_path(path) ⇒ Object
35 36 37 38 39 40 41 42 43 44 |
# File 'lib/poolparty/modules/file_writer.rb', line 35 def make_base_path(path) unless FileTest.directory?(path) begin ::FileUtils.mkdir_p path rescue Errno::ENOTDIR rescue Errno::EEXIST puts "There was an error" end end end |
#make_template_directory ⇒ Object
48 49 50 |
# File 'lib/poolparty/modules/file_writer.rb', line 48 def make_template_directory FileUtils.mkdir_p Base.template_directory unless ::File.directory?(Base.template_directory) end |
#write_to_file(file, str, &block) ⇒ Object
17 18 19 20 21 22 23 24 25 |
# File 'lib/poolparty/modules/file_writer.rb', line 17 def write_to_file(file, str, &block) path = ::File.join( Base.storage_directory, ::File.basename(file) ) make_base_path( Base.storage_directory ) ::File.open(path, "w+") do |f| f.print str f.flush f.print block.call(f) if block end end |
#write_to_file_in_storage_directory(file, str, &block) ⇒ Object
13 14 15 16 |
# File 'lib/poolparty/modules/file_writer.rb', line 13 def write_to_file_in_storage_directory(file, str, &block) path = ::File.join( Base.storage_directory, ::File.basename(file) ) write_to_file(path, str, &block) end |
#write_to_temp_file(str = "", &block) ⇒ Object
Write a temp file with the content str and return the Tempfile It creates a random file name
28 29 30 31 32 33 34 |
# File 'lib/poolparty/modules/file_writer.rb', line 28 def write_to_temp_file(str="", &block) returning Tempfile.new("#{Base.storage_directory}/PoolParty-#{str[0..10].chomp}-#{rand(1000)}") do |fp| fp.print str fp.flush block.call(fp) end end |