Class: Ruby2xlsx::Document
Instance Method Summary collapse
- #columns_names ⇒ Object
- #each_with_index ⇒ Object
- #filename ⇒ Object
- #human_columns_names ⇒ Object
- #render ⇒ Object
- #worksheet_name ⇒ Object
Methods inherited from Base
#add_worksheet, #initialize, #method_missing, #workbook, #worksheet
Constructor Details
This class inherits a constructor from Ruby2xlsx::Base
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class Ruby2xlsx::Base
Instance Method Details
#columns_names ⇒ Object
32 33 34 |
# File 'lib/ruby2xlsx/document.rb', line 32 def columns_names @columns_names ||= (@options[:columns] || @klass.column_names) end |
#each_with_index ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/ruby2xlsx/document.rb', line 48 def each_with_index count = 0 if defined?(ActiveRecord) && @source.is_a?(::ActiveRecord::Relation) @klass ||= @source.klass @source.find_each do |item| yield item, count count += 1 end else items = Array.wrap(@source) @klass ||= items.first.class unless items.empty? @klass ||= Default items.each do |item| yield item, count count += 1 end end end |
#filename ⇒ Object
44 45 46 |
# File 'lib/ruby2xlsx/document.rb', line 44 def filename @filename ||= [(@options[:filename] || @klass.model_name.plural || "document"), ".xls"].join end |
#human_columns_names ⇒ Object
36 37 38 |
# File 'lib/ruby2xlsx/document.rb', line 36 def human_columns_names @human_columns_names ||= columns_names.map { |column| @klass.human_attribute_name(column.to_s) } end |
#render ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/ruby2xlsx/document.rb', line 3 def render date_format = workbook.add_format(:num_format => 'dd.mm.yyyy') time_format = workbook.add_format(:num_format => 'dd.mm.yyyy HH:MM') each_with_index do |record, index| row = index + 1 columns_names.each_with_index do |column, num| value = record.send(column) case value when Date then worksheet.write_string(row, num, value.strftime("%Y-%m-%dT"), date_format) when DateTime, Time then worksheet.write_date_time(row, num, value.strftime("%Y-%m-%dT%H:%M:%S.%L"), time_format) when String then worksheet.write_string(row, num, value) else worksheet.write(row, num, value) end end end bold = workbook.add_format(:bold => 1) worksheet.write('A1', human_columns_names, bold) super end |
#worksheet_name ⇒ Object
40 41 42 |
# File 'lib/ruby2xlsx/document.rb', line 40 def worksheet_name @worksheet_name ||= (@options[:worksheet_name] || @klass.model_name.human) end |