Class: Potpourri::Report

Inherits:
Object
  • Object
show all
Includes:
FieldHelpers, ReportConfigs
Defined in:
lib/models/report.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from FieldHelpers

included

Methods included from ReportConfigs

#fields, included, #resource_class

Constructor Details

#initialize(path) ⇒ Report

Returns a new instance of Report.



10
11
12
# File 'lib/models/report.rb', line 10

def initialize(path)
  @path = path
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



8
9
10
# File 'lib/models/report.rb', line 8

def path
  @path
end

Instance Method Details

#export(collection) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/models/report.rb', line 30

def export(collection)
  CSV.open(path, 'w') do |csv|
    csv << headers

    collection.each do |item|
      csv << exportable_fields.map { |field| field.export item }
    end

    csv
  end
end

#headersObject



14
15
16
# File 'lib/models/report.rb', line 14

def headers
  fields.map &:header
end

#importObject



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/models/report.rb', line 18

def import
  CSV.read(path, headers: true, converters: :numeric).map do |row|
    resource = fetch_resource(row)

    importable_fields.each do |field|
      field.import resource, row[field.header]
    end

    resource
  end
end