Module: ODFReport::FileOps
- Included in:
- Report
- Defined in:
- lib/odf-report/file_ops.rb
Instance Method Summary collapse
- #add_dir_to_zip(zip_file, dir, entry) ⇒ Object
- #add_files_to_dir(files, dir) ⇒ Object
- #create_new_file(dest) ⇒ Object
- #random_filename(opts = {}) ⇒ Object
- #update_file_from_zip(zip_file, content_file, &block) ⇒ Object
Instance Method Details
#add_dir_to_zip(zip_file, dir, entry) ⇒ Object
38 39 40 41 42 |
# File 'lib/odf-report/file_ops.rb', line 38 def add_dir_to_zip(zip_file, dir, entry) Zip::ZipFile.open(zip_file, true) do |z| Dir["#{dir}/**/*"].each { |f| z.add("#{entry}/#{File.basename(f)}", f) } end end |
#add_files_to_dir(files, dir) ⇒ Object
31 32 33 34 35 36 |
# File 'lib/odf-report/file_ops.rb', line 31 def add_files_to_dir(files, dir) FileUtils.mkdir(dir) files.each do |path| FileUtils.cp(path, File.join(dir, File.basename(path))) end end |
#create_new_file(dest) ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 |
# File 'lib/odf-report/file_ops.rb', line 5 def create_new_file(dest) if dest FileUtils.cp(@template, dest) new_file = dest else FileUtils.cp(@template, @tmp_dir) new_file = "#{@tmp_dir}/#{File.basename(@template)}" end return new_file end |
#random_filename(opts = {}) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/odf-report/file_ops.rb', line 18 def random_filename(opts={}) opts = {:chars => ('0'..'9').to_a + ('A'..'F').to_a + ('a'..'f').to_a, :length => 24, :prefix => '', :suffix => '', :verify => true, :attempts => 10}.merge(opts) opts[:attempts].times do filename = '' opts[:length].times { filename << opts[:chars][rand(opts[:chars].size)] } filename = opts[:prefix] + filename + opts[:suffix] return filename unless opts[:verify] && File.exists?(filename) end nil end |
#update_file_from_zip(zip_file, content_file, &block) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/odf-report/file_ops.rb', line 44 def update_file_from_zip(zip_file, content_file, &block) Zip::ZipFile.open(zip_file) do |z| cont = "#{@tmp_dir}/#{content_file}" z.extract(content_file, cont) txt = '' File.open(cont, "r") do |f| txt = f.read end yield(txt) File.open(cont, "w") do |f| f.write(txt) end z.replace(content_file, cont) end end |