Class: Knj::Table_writer
Instance Method Summary collapse
- #close ⇒ Object
- #ext ⇒ Object
- #ftype ⇒ Object
-
#initialize(args = {}) ⇒ Table_writer
constructor
A new instance of Table_writer.
- #write_row(arr) ⇒ Object
Constructor Details
#initialize(args = {}) ⇒ Table_writer
Returns a new instance of Table_writer.
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/knj/table_writer.rb', line 2 def initialize(args = {}) @args = args if !@args["filepath"] raise "No filepath was given." end if @args["format"] == "csv" @fp = File.open(@args["filepath"], "w") elsif @args["format"] == "excel5" require "spreadsheet" @wb = Spreadsheet::Workbook.new @ws = @wb.create_worksheet @row = 0 else raise "Unsupported format: '#{@args["format"]}'." end end |
Instance Method Details
#close ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/knj/table_writer.rb', line 61 def close if @args["format"] == "csv" @fp.close elsif @args["format"] == "excel5" dirname = File.dirname(@args["filepath"]) basename = File.basename(@args["filepath"], File.extname(@args["filepath"])) temp_path = "#{dirname}/#{basename}.xls" @wb.write(temp_path) @wb = nil @ws = nil FileUtils.mv(temp_path, @args["filepath"]) else raise "Unsupported format: '#{@args["format"]}'." end return nil end |
#ext ⇒ Object
82 83 84 85 86 87 88 89 90 |
# File 'lib/knj/table_writer.rb', line 82 def ext if @args["format"] == "csv" return "csv" elsif @args["format"] == "excel5" return "xls" else raise "Unsupported format: '#{@args["format"]}'." end end |
#ftype ⇒ Object
92 93 94 95 96 97 98 99 100 |
# File 'lib/knj/table_writer.rb', line 92 def ftype if @args["format"] == "csv" return "text/csv" elsif @args["format"] == "excel5" return "application/ms-excel" else raise "Unsupported format: '#{@args["format"]}'." end end |
#write_row(arr) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/knj/table_writer.rb', line 22 def write_row(arr) if @args["format"] == "csv" arr.each_index do |key| val = arr[key] if val.is_a?(Hash) and val["type"] == "decimal" arr[key] = Knj::Php.number_format(val["value"], @args["amount_decimals"], @args["amount_dsep"], @args["amount_tsep"]) elsif val.is_a?(Hash) and val["type"] == "date" arr[key] = Knj::Php.date(@args["date_format"], val["value"]) end end line_str = Knj::Csv.arr_to_csv(arr, @args["expl"], @args["surr"]) #line_str = line_str.encode("iso8859-1") if @args["encoding"] == "iso8859-1" @fp.write(line_str) elsif @args["format"] == "excel5" col_count = 0 arr.each do |val| if val.is_a?(Hash) and val["type"] == "decimal" @ws[@row, col_count] = Knj::Php.number_format(val["value"], @args["amount_decimals"], @args["amount_dsep"], @args["amount_tsep"]) elsif val.is_a?(Hash) and val["type"] == "date" @ws[@row, col_count] = Knj::Php.date(@args["date_format"], val["value"]) else @ws[@row, col_count] = val end col_count += 1 end @row += 1 else raise "Unsupported format: '#{@args["format"]}'." end return nil end |