| Class | GridFile |
| In: |
lib/grid_file.rb
|
| Parent: | Grid |
Read and write Grid to files. To be replaced by Tabular.
source can be String or File accept Tempfile for testing header_row: start_at_row is header FIXME Only honors start_at_row, header_row for Excel FIXME Assumes all non-.txt files are Excel
# File lib/grid_file.rb, line 84
84: def initialize(source, *options)
85: case source
86: when File, Tempfile
87: raise "#{source} does not exist" unless File.exists?(source.path)
88: @file = source
89: if excel?
90: lines = GridFile.read_excel(@file)
91: else
92: lines = source.readlines
93: end
94: super(lines, options)
95:
96: when String
97: super(source, options)
98:
99: when Array
100: super(source, options)
101:
102: else
103: raise(ArgumentError, "Expected String or File, but was #{source.class}")
104: end
105: end
# File lib/grid_file.rb, line 4
4: def GridFile.read_excel(file)
5: Rails.logger.debug("GridFile (#{Time.zone.now}) read_excel #{file}")
6: if File::Stat.new(file.path).size == 0
7: Rails.logger.warn("#{file.path} is empty")
8: return []
9: end
10:
11: excel_rows = []
12: Spreadsheet.open(file.path)
13: Spreadsheet.open(file.path).worksheets.each do |worksheet|
14: worksheet.each do |row|
15: if Rails.logger.debug? && debug?
16: Rails.logger.debug("---------------------------------")
17: Rails.logger.debug("GridFile #{Time.zone.now} row #{row.to_a.join(', ')}")
18: row.each_with_index do |cell, index|
19: Rails.logger.debug("number_format pattern to_s to_f #{row.format(index).number_format} #{row.format(index).pattern} #{cell.to_s} #{cell.to_f if cell.respond_to?(:to_f)} #{cell.class}")
20: end
21: Rails.logger.debug("---------------------------------")
22: end
23: line = []
24: for cell in row
25: case cell
26: when NilClass
27: line << ""
28: when String
29: line << cell.strip
30: when Date, DateTime, Time
31: line << cell.to_s(:db)
32: when Float
33: if cell.to_f == cell.to_i
34: line << cell.to_i.to_s
35: else
36: line << cell.to_s
37: end
38: when Numeric
39: line << cell.to_s
40: else
41: if cell.respond_to?(:to_s)
42: line << cell.to_s.strip
43: else
44: line << ""
45: end
46: end
47: end
48: if !line.empty? && !line.all? { |cell| cell.nil? || (cell.respond_to?(:blank?) && cell.blank?) }
49: excel_rows << line
50: end
51: end
52: end
53: Rails.logger.debug("GridFile (#{Time.zone.now}) read #{excel_rows.size} rows")
54: excel_rows
55: end
TODO Dupe method Time in hh:mm:ss.00 format. E.g., 1:20:59.75 This method doesn‘t handle some typical edge cases very well
# File lib/grid_file.rb, line 64
64: def GridFile.s_to_time(string)
65: if string.to_s.blank?
66: 0.0
67: else
68: string.gsub!(',', '.')
69: parts = string.to_s.split(':')
70: parts.reverse!
71: t = 0.0
72: parts.each_with_index do |part, index|
73: t = t + (part.to_f) * (60.0 ** index)
74: end
75: t
76: end
77: end
# File lib/grid_file.rb, line 123
123: def excel?
124: return false if @file.nil?
125: @file.path.include?('.xls')
126: end
# File lib/grid_file.rb, line 107
107: def save
108: begin
109: out = File.open(@file.path, File::WRONLY)
110: for row in rows
111: for index in 0..(column_count - 1)
112: out.write(row[index])
113: out.write("\t") unless index == (column_count - 1)
114: end
115: out.write("\n")
116: end
117: out.flush
118: ensure
119: out.close unless out == nil || out.closed?
120: end
121: end