spreadsheet_erb 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/spreadsheet_erb.rb +114 -0
  3. metadata +44 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: fd6359c8c5a03ef4b0617e8397698eaaeed4ce06
4
+ data.tar.gz: 9b5fe71a858de5f7bf81d377aaec3aac4250ae1f
5
+ SHA512:
6
+ metadata.gz: 16908742b088b7394365aacabf2549ff498cdebba3f1cf1e0e915c07f93b8bd444ac82509357396107279fde75dc68e0bf21616ea224a99367c996a262048f70
7
+ data.tar.gz: 281fd0c9b5ff63e1bf999b743d48962999ce5a11460cd27d59055974a34126018c5e60ba173e15781cb90c4ade90c2d8a3ab4e1fb2bc59afeabef6ae6fee7e81
@@ -0,0 +1,114 @@
1
+ require 'rubygems'
2
+ require 'erb'
3
+ require 'zip'
4
+
5
+ class SpreadsheetErb
6
+
7
+ def initialize(template_path:, context:, temp_path: './tmp')
8
+ @template_path = template_path
9
+ @context = context
10
+ @temp_path = temp_path
11
+ end
12
+
13
+ def render
14
+ copy_template()
15
+ xml_paths = find_xml_paths(@temp_path)
16
+ xml_paths.each do |xml_path|
17
+ result = render_file(xml_path)
18
+ save_file(xml_path, result)
19
+ end
20
+ end
21
+
22
+ def write(result_path)
23
+ File.delete(result_path) if File.exist?(result_path)
24
+ zip("#{@temp_path}/template", result_path)
25
+ remove_template()
26
+ end
27
+
28
+ private
29
+
30
+ def render_file(file_path)
31
+ template = File.read(file_path)
32
+ renderer = ERB.new(template)
33
+ result = renderer.result(@context)
34
+ result
35
+ end
36
+
37
+ def save_file(file_path, value)
38
+ File.write(file_path, value)
39
+ end
40
+
41
+ def find_xml_paths(template_path)
42
+ Dir.glob("#{template_path}/**/*.xml")
43
+ end
44
+
45
+ def copy_template
46
+ FileUtils.cp_r(@template_path, @temp_path)
47
+ end
48
+
49
+ def remove_template
50
+ FileUtils.rm_r(Dir.glob("#{@temp_path}/*"))
51
+ end
52
+
53
+ def zip(directory_to_zip, result_path)
54
+ zf = ZipFileGenerator.new(directory_to_zip, result_path)
55
+ zf.write()
56
+ end
57
+ end
58
+
59
+ # This is a simple example which uses rubyzip to
60
+ # recursively generate a zip file from the contents of
61
+ # a specified directory. The directory itself is not
62
+ # included in the archive, rather just its contents.
63
+ #
64
+ # Usage:
65
+ # directory_to_zip = "/tmp/input"
66
+ # output_file = "/tmp/out.zip"
67
+ # zf = ZipFileGenerator.new(directory_to_zip, output_file)
68
+ # zf.write()
69
+
70
+ class ZipFileGenerator
71
+ # Initialize with the directory to zip and the location of the output archive.
72
+ def initialize(input_dir, output_file)
73
+ @input_dir = input_dir
74
+ @output_file = output_file
75
+ end
76
+
77
+ # Zip the input directory.
78
+ def write
79
+ entries = Dir.entries(@input_dir) - %w(. ..)
80
+
81
+ ::Zip::File.open(@output_file, ::Zip::File::CREATE) do |zipfile|
82
+ write_entries entries, '', zipfile
83
+ end
84
+ end
85
+
86
+ private
87
+
88
+ # A helper method to make the recursion work.
89
+ def write_entries(entries, path, zipfile)
90
+ entries.each do |e|
91
+ zipfile_path = path == '' ? e : File.join(path, e)
92
+ disk_file_path = File.join(@input_dir, zipfile_path)
93
+ #puts "Deflating #{disk_file_path}"
94
+
95
+ if File.directory? disk_file_path
96
+ recursively_deflate_directory(disk_file_path, zipfile, zipfile_path)
97
+ else
98
+ put_into_archive(disk_file_path, zipfile, zipfile_path)
99
+ end
100
+ end
101
+ end
102
+
103
+ def recursively_deflate_directory(disk_file_path, zipfile, zipfile_path)
104
+ zipfile.mkdir zipfile_path
105
+ subdir = Dir.entries(disk_file_path) - %w(. ..)
106
+ write_entries subdir, zipfile_path, zipfile
107
+ end
108
+
109
+ def put_into_archive(disk_file_path, zipfile, zipfile_path)
110
+ zipfile.get_output_stream(zipfile_path) do |f|
111
+ f.write(File.open(disk_file_path, 'rb').read)
112
+ end
113
+ end
114
+ end
metadata ADDED
@@ -0,0 +1,44 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: spreadsheet_erb
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Marcin Wierzbicki
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-05-16 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Spreadsheet ERB Template for Reports
14
+ email: marcin.wierzbicki@andoncloud.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/spreadsheet_erb.rb
20
+ homepage: http://rubygems.org/gems/spreadsheet_erb
21
+ licenses:
22
+ - MIT
23
+ metadata: {}
24
+ post_install_message:
25
+ rdoc_options: []
26
+ require_paths:
27
+ - lib
28
+ required_ruby_version: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ required_rubygems_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ">="
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ requirements: []
39
+ rubyforge_project:
40
+ rubygems_version: 2.5.2
41
+ signing_key:
42
+ specification_version: 4
43
+ summary: Spreadsheet ERB Template for Reports
44
+ test_files: []