Class: Json2xslx::Generator
- Inherits:
-
Object
- Object
- Json2xslx::Generator
- Defined in:
- lib/json2xslx/generator.rb
Instance Method Summary collapse
-
#build_headers(json_objects, sheet) ⇒ Object
building headers for .XLS file.
-
#build_sheet ⇒ Object
Builds the sheets for a Worksheet.
-
#build_values(json_objects, excel_headers, key, sheet) ⇒ Object
building result set row values for .XLS file.
-
#build_xslx(json_objects, key, sheet) ⇒ Object
Building the .XLS files build_headers method build the headers for .XLS file build_values method ensures the values get generated for .XLS file.
-
#file_loader(path) ⇒ Object
Reads the given file and convert the JSON provided into the sheet to Ruby object.
- #generate_keys(obj, prefixed_header = nil) ⇒ Object
- #generate_value(json_objects, head) ⇒ Object
-
#initialize(paths, options = {}) ⇒ Generator
constructor
A new instance of Generator.
-
#json_loader(paths) ⇒ Object
Loads the file from given path and initiate the processing of the JSON object.
-
#process_json(json_objects) ⇒ Object
After processing of the objects .XLS file is build.
- #untangled_key(key) ⇒ Object
-
#write(file) ⇒ Object
writing to .XLS file.
Constructor Details
#initialize(paths, options = {}) ⇒ Generator
Returns a new instance of Generator.
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/json2xslx/generator.rb', line 16 def initialize(paths, ={}) @name = [:name] || 'Worksheet' @path = [:path] || ((Dir.exists?("#{ENV['HOME']}/Json2xslx/#{Time.now.to_s}")) ? "#{ENV['HOME']}/Json2xslx/" : FileUtils::mkdir_p("#{ENV['HOME']}/Json2xslx/#{Time.now.to_s}").first) @header_row = [:header_row] || 0 @data_row = @header_row + 1 @current_data_row = @data_row @header_format = [:header_format] || (Spreadsheet::Format.new :color => :black, :weight => :bold, :pattern => 1, :pattern_fg_color => :orange, :bottom => :double, :top => :double) @bold_format = Spreadsheet::Format.new :weight => :bold, :color => :black @wrap_text_format = Spreadsheet::Format.new :text_wrap => true, :vertical_align => 'top' @book = Spreadsheet::Workbook.new json_loader(paths) end |
Instance Method Details
#build_headers(json_objects, sheet) ⇒ Object
building headers for .XLS file
116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/json2xslx/generator.rb', line 116 def build_headers json_objects, sheet excel_headers = generate_keys json_objects excel_headers.unshift("Identifier") (excel_headers).length.times do |index| sheet[@header_row,index] = excel_headers[index].to_s sheet.column(index).width = 30 sheet.row(@header_row).set_format(index, @header_format) end excel_headers end |
#build_sheet ⇒ Object
Builds the sheets for a Worksheet
37 38 39 40 |
# File 'lib/json2xslx/generator.rb', line 37 def build_sheet sheet = @book.create_worksheet :name => @name sheet end |
#build_values(json_objects, excel_headers, key, sheet) ⇒ Object
building result set row values for .XLS file
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/json2xslx/generator.rb', line 134 def build_values json_objects, excel_headers, key, sheet contained_values = excel_headers.map do |head| if(head.eql?'Identifier') value = key.to_s else value = generate_value json_objects, head end value end (excel_headers).length.times do |index| sheet[@current_data_row,index] = contained_values[index].to_s sheet.column(index).width = 30 end @current_data_row = @current_data_row +1 end |
#build_xslx(json_objects, key, sheet) ⇒ Object
Building the .XLS files build_headers method build the headers for .XLS file build_values method ensures the values get generated for .XLS file
102 103 104 105 106 107 108 109 |
# File 'lib/json2xslx/generator.rb', line 102 def build_xslx json_objects, key, sheet excel_headers = build_headers json_objects, sheet build_values json_objects, excel_headers, key, sheet write "#{@path}/#{@name}.xls" end |
#file_loader(path) ⇒ Object
Reads the given file and convert the JSON provided into the sheet to Ruby object
75 76 77 78 79 80 81 82 |
# File 'lib/json2xslx/generator.rb', line 75 def file_loader path raw_data = IO.read path begin return MultiJson.load raw_data rescue Exception => e end end |
#generate_keys(obj, prefixed_header = nil) ⇒ Object
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/json2xslx/generator.rb', line 162 def generate_keys(obj, prefixed_header=nil) keys = obj.keys result_set = keys.map do |key| untangled_key = untangled_key(key) value = obj[key] if value.is_a?Hash generate_keys(obj[key], (prefixed_header ? "#{prefixed_header} #{untangled_key}" : untangled_key)) else if !prefixed_header.nil? "#{prefixed_header} #{untangled_key}" else untangled_key end end end result_set.compact.flatten.uniq end |
#generate_value(json_objects, head) ⇒ Object
153 154 155 156 157 158 159 160 |
# File 'lib/json2xslx/generator.rb', line 153 def generate_value json_objects, head segmented_heads = head.split(' ') segmented_heads.each do |obj| return nil if obj.nil? json_objects = json_objects[obj] end json_objects end |
#json_loader(paths) ⇒ Object
Loads the file from given path and initiate the processing of the JSON object
58 59 60 61 62 63 64 65 66 67 |
# File 'lib/json2xslx/generator.rb', line 58 def json_loader paths paths = [paths] unless paths.is_a?(Array) paths.map do |path| @current_data_row = @data_row json_objects = file_loader path process_json json_objects end end |
#process_json(json_objects) ⇒ Object
After processing of the objects .XLS file is build
89 90 91 92 93 94 |
# File 'lib/json2xslx/generator.rb', line 89 def process_json json_objects sheet = build_sheet json_objects.keys.each do |key| build_xslx json_objects[key], key, sheet end end |
#untangled_key(key) ⇒ Object
185 186 187 |
# File 'lib/json2xslx/generator.rb', line 185 def untangled_key key key end |
#write(file) ⇒ Object
writing to .XLS file
48 49 50 |
# File 'lib/json2xslx/generator.rb', line 48 def write file @book.write file end |