Class: MagicReport::GeneralReport

Inherits:
Object
  • Object
show all
Defined in:
lib/magic_report/general_report.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input, report_klass, options = {}) ⇒ GeneralReport

Returns a new instance of GeneralReport.



7
8
9
10
11
12
# File 'lib/magic_report/general_report.rb', line 7

def initialize(input, report_klass, options = {})
  @models = input.is_a?(Enumerable) ? input : [input]
  @report_klass = report_klass
  @csv = ::MagicReport::Report::Csv.new(self)
  @fill = options[:fill] || false
end

Instance Attribute Details

#csvObject (readonly)

Returns the value of attribute csv.



5
6
7
# File 'lib/magic_report/general_report.rb', line 5

def csv
  @csv
end

#fillObject (readonly)

Returns the value of attribute fill.



5
6
7
# File 'lib/magic_report/general_report.rb', line 5

def fill
  @fill
end

#modelsObject (readonly)

Returns the value of attribute models.



5
6
7
# File 'lib/magic_report/general_report.rb', line 5

def models
  @models
end

#report_klassObject (readonly)

Returns the value of attribute report_klass.



5
6
7
# File 'lib/magic_report/general_report.rb', line 5

def report_klass
  @report_klass
end

Instance Method Details

#as_attachmentObject



23
24
25
26
27
28
# File 'lib/magic_report/general_report.rb', line 23

def as_attachment
  @as_attachment ||= {
    mime_type: "text/csv",
    content: csv.io.read
  }
end

#generateObject



14
15
16
17
18
19
20
21
# File 'lib/magic_report/general_report.rb', line 14

def generate
  models.find_each.with_index do |model, index|
    report = report_klass.new(model, fill)

    csv.add_headings(report) if index.zero?
    report.rows.each { |row| csv.add_row(row) }
  end
end