Module: Dunlop::CsvBuilder
Instance Method Summary collapse
- #data ⇒ Object
- #date_number ⇒ Object
- #file_name ⇒ Object
- #headers ⇒ Object
- #initialize(resource, options = {}) ⇒ Object
-
#record_attribute(record, attribute_name) ⇒ Object
Do a lookup on a record of an attribute and map the value to the csv required version if appliccable.
- #serialize_row(record) ⇒ Object
- #serialized_rows ⇒ Object
-
#workflow_instances_in_batch ⇒ Object
TODO: this is a separate include, not part of the core CsvBuilder object.
Instance Method Details
#data ⇒ Object
38 39 40 41 42 43 44 45 46 |
# File 'lib/dunlop/csv_builder.rb', line 38 def data str = CSV.generate do |csv| csv << headers if headers.present? serialized_rows.each do |serialized_row| csv << serialized_row end end str end |
#date_number ⇒ Object
48 49 50 |
# File 'lib/dunlop/csv_builder.rb', line 48 def date_number Date.today.to_s(:number) end |
#file_name ⇒ Object
17 18 19 |
# File 'lib/dunlop/csv_builder.rb', line 17 def file_name [:file_name] || "#{Rails.application.config.application_name}-#{date_number}-data.csv" end |
#headers ⇒ Object
21 22 23 |
# File 'lib/dunlop/csv_builder.rb', line 21 def headers [] end |
#initialize(resource, options = {}) ⇒ Object
11 12 13 14 15 |
# File 'lib/dunlop/csv_builder.rb', line 11 def initialize(resource, = {}) @resource, @options = resource, @csv_options = .merge(.slice(:col_sep, :row_sep, :encoding, :field_size_limit, :quote_char)) @controller = [:controller] end |
#record_attribute(record, attribute_name) ⇒ Object
Do a lookup on a record of an attribute and map the value to the csv required version if appliccable. At the moment booleans are converted to zeros and ones
60 61 62 63 64 65 66 67 68 69 |
# File 'lib/dunlop/csv_builder.rb', line 60 def record_attribute(record, attribute_name) result = record.public_send(attribute_name) case result when TrueClass then 1 when FalseClass then 0 when String then result.strip.gsub(/\r\n?|\n/, " ") when DayTimeBase then result.number else result end end |
#serialize_row(record) ⇒ Object
34 35 36 |
# File 'lib/dunlop/csv_builder.rb', line 34 def serialize_row(record) [record.id] end |
#serialized_rows ⇒ Object
25 26 27 28 29 30 31 32 |
# File 'lib/dunlop/csv_builder.rb', line 25 def serialized_rows rows = [] iteration_method = resource.respond_to?(:find_each) ? :find_each : :each resource.public_send(iteration_method) do |record| rows.push serialize_row(record) end rows end |
#workflow_instances_in_batch ⇒ Object
TODO: this is a separate include, not part of the core CsvBuilder object
53 54 55 |
# File 'lib/dunlop/csv_builder.rb', line 53 def workflow_instances_in_batch resource.workflow_instances.for_filtered_export end |