Module: PoolParty::FileWriter
- Included in:
- PoolParty
- Defined in:
- lib/poolparty/modules/file_writer.rb
Instance Method Summary collapse
- #cleanup_storage_directory ⇒ Object
- #clear_base_directory ⇒ Object
- #copy_directory_into_storage_directory(from, pat) ⇒ Object
- #copy_directory_into_template_storage_directory(dir) ⇒ Object
- #copy_file_to_storage_directory(file, preceded = "") ⇒ Object
- #copy_template_to_storage_directory(file, force = false) ⇒ Object
- #make_base_directory ⇒ Object
- #make_base_path(path) ⇒ Object
- #make_directory_in_storage_directory(dirname = "newdir") ⇒ Object
- #make_template_directory(dir = nil) ⇒ Object
- #write_to_file(file, str = "", preceded = "", &block) ⇒ Object
- #write_to_file_in_storage_directory(file, str = "", preceded = "", &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
#cleanup_storage_directory ⇒ Object
8 9 10 |
# File 'lib/poolparty/modules/file_writer.rb', line 8 def cleanup_storage_directory ::FileUtils.rm_rf "#{Default.storage_directory}" end |
#clear_base_directory ⇒ Object
89 90 91 92 93 |
# File 'lib/poolparty/modules/file_writer.rb', line 89 def clear_base_directory Dir["#{Default.storage_directory}/**/*"].each do |f| ::FileUtils.rm f if ::File.file?(f) end end |
#copy_directory_into_storage_directory(from, pat) ⇒ Object
27 28 29 30 31 32 |
# File 'lib/poolparty/modules/file_writer.rb', line 27 def copy_directory_into_storage_directory(from, pat) to = ::File.join(Default.storage_directory, pat) # make_directory_in_storage_directory(to) unless ::File.directory?(to) FileUtils.cp_r(from, to) end |
#copy_directory_into_template_storage_directory(dir) ⇒ Object
20 21 22 23 24 25 26 |
# File 'lib/poolparty/modules/file_writer.rb', line 20 def copy_directory_into_template_storage_directory(dir) path = make_template_directory(dir) Dir["#{dir}/*"].each do |file| FileUtils.cp file, path unless ::File.exists?(::File.join(path, ::File.basename(file))) end ::File.basename(path) end |
#copy_file_to_storage_directory(file, preceded = "") ⇒ Object
3 4 5 6 7 |
# File 'lib/poolparty/modules/file_writer.rb', line 3 def copy_file_to_storage_directory(file, preceded="") make_base_directory path = ::File.join( Default.storage_directory, preceded, ::File.basename(file) ) ::FileUtils.cp file, path unless file == path || ::File.file?(path) end |
#copy_template_to_storage_directory(file, force = false) ⇒ Object
11 12 13 14 15 16 17 18 19 |
# File 'lib/poolparty/modules/file_writer.rb', line 11 def copy_template_to_storage_directory(file, force=false) make_template_directory path = ::File.join( Default.tmp_path, Default.template_directory, ::File.basename(file) ) if force FileUtils.cp file, path else FileUtils.cp file, path unless file == path || ::File.exists?(path) end end |
#make_base_directory ⇒ Object
70 71 72 73 74 75 76 77 |
# File 'lib/poolparty/modules/file_writer.rb', line 70 def make_base_directory begin FileUtils.mkdir_p Default.storage_directory unless ::File.directory?(Default.storage_directory) rescue Errno::EEXIST FileUtils.rm Default.storage_directory make_base_directory end end |
#make_base_path(path) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/poolparty/modules/file_writer.rb', line 59 def make_base_path(path) unless FileTest.directory?(path) begin ::FileUtils.rm path if ::File.file?(path) ::FileUtils.mkdir_p path rescue Errno::ENOTDIR rescue Errno::EEXIST puts "There was an error" end end end |
#make_directory_in_storage_directory(dirname = "newdir") ⇒ Object
33 34 35 36 |
# File 'lib/poolparty/modules/file_writer.rb', line 33 def make_directory_in_storage_directory(dirname="newdir") path = ::File.join( Default.storage_directory, dirname ) make_base_path path end |
#make_template_directory(dir = nil) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/poolparty/modules/file_writer.rb', line 78 def make_template_directory(dir=nil) path = dir ? ::File.join(Default.tmp_path, Default.template_directory, ::File.basename(dir)) : ::File.join(Default.tmp_path, Default.template_directory) begin make_base_directory FileUtils.mkdir path unless ::File.directory?(path) rescue Errno::EEXIST FileUtils.rm path if ::File.exists?(path) make_template_directory(dir) end path end |
#write_to_file(file, str = "", preceded = "", &block) ⇒ Object
41 42 43 44 45 46 47 48 49 |
# File 'lib/poolparty/modules/file_writer.rb', line 41 def write_to_file(file, str="", preceded="", &block) path = ::File.join( Default.storage_directory, preceded, ::File.basename(file) ) make_base_path( Default.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 = "", preceded = "", &block) ⇒ Object
37 38 39 40 |
# File 'lib/poolparty/modules/file_writer.rb', line 37 def write_to_file_in_storage_directory(file, str="", preceded="", &block) path = ::File.join( Default.storage_directory, preceded, ::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
52 53 54 55 56 57 58 |
# File 'lib/poolparty/modules/file_writer.rb', line 52 def write_to_temp_file(str="", &block) returning Tempfile.new("#{Default.storage_directory}/PoolParty-#{str[0..10].chomp}-#{rand(1000)}") do |fp| fp.print str fp.flush block.call(fp) end end |