Class: RenderCsv
- Inherits:
-
Object
- Object
- RenderCsv
- Includes:
- ActionView::Helpers::NumberHelper
- Defined in:
- app/lib/render_csv.rb
Direct Known Subclasses
ArticlesCsv, BankTransactionsCsv, FinancialTransactionsCsv, InvoicesCsv, OrderCsv, OrdergroupsCsv, UsersCsv
Instance Method Summary collapse
- #data {|[]| ... } ⇒ Object
- #header ⇒ Object
-
#initialize(object, options = {}) ⇒ RenderCsv
constructor
A new instance of RenderCsv.
-
#number_to_currency(number, options = {}) ⇒ Object
XXX disable unit to avoid encoding problems, both in unit and whitespace.
- #to_csv ⇒ Object
Constructor Details
#initialize(object, options = {}) ⇒ RenderCsv
Returns a new instance of RenderCsv.
6 7 8 9 10 11 12 13 |
# File 'app/lib/render_csv.rb', line 6 def initialize(object, = {}) @object = object @options = # defaults to please Microsoft Excel ... @options[:col_sep] ||= FoodsoftConfig[:csv_col_sep] || ';' @options[:row_sep] ||= FoodsoftConfig[:csv_row_sep] if FoodsoftConfig[:csv_row_sep] @options[:encoding] ||= FoodsoftConfig[:csv_encoding] || 'ISO-8859-15' end |
Instance Method Details
#data {|[]| ... } ⇒ Object
30 31 32 |
# File 'app/lib/render_csv.rb', line 30 def data yield [] end |
#header ⇒ Object
26 27 28 |
# File 'app/lib/render_csv.rb', line 26 def header nil end |
#number_to_currency(number, options = {}) ⇒ Object
XXX disable unit to avoid encoding problems, both in unit and whitespace. Also allows computations in spreadsheet.
35 36 37 |
# File 'app/lib/render_csv.rb', line 35 def number_to_currency(number, = {}) super(number, .merge({ unit: '' })) end |
#to_csv ⇒ Object
15 16 17 18 19 20 21 22 23 24 |
# File 'app/lib/render_csv.rb', line 15 def to_csv = @options.select { |k| %w[col_sep row_sep].include? k.to_s } ret = CSV.generate do |csv| if h = header csv << h end data { |d| csv << d } end ret.encode(@options[:encoding], invalid: :replace, undef: :replace) end |