Class: Potpourri::Report
- Inherits:
-
Object
- Object
- Potpourri::Report
- Includes:
- FieldHelpers, ReportConfigs
- Defined in:
- lib/models/report.rb
Instance Attribute Summary collapse
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Instance Method Summary collapse
- #export(collection) ⇒ Object
- #headers ⇒ Object
- #import ⇒ Object
-
#initialize(path) ⇒ Report
constructor
A new instance of Report.
Methods included from FieldHelpers
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
#path ⇒ Object (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 |
#headers ⇒ Object
14 15 16 |
# File 'lib/models/report.rb', line 14 def headers fields.map &:header end |
#import ⇒ Object
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 |