Class: ApkgToCsv::Csv

Inherits:
Object
  • Object
show all
Defined in:
lib/apkg_to_csv/csv.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash) ⇒ Csv

Returns a new instance of Csv.



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/apkg_to_csv/csv.rb', line 23

def initialize(hash)
  @csv = CSV.generate do |csv|
    first, *rest = hash.values

    first.each { |f| csv << f }
    rest.each do |fields_array|
      csv << []
      fields_array.each { |f| csv << f }
    end
  end
end

Instance Attribute Details

#csvObject (readonly)

Returns the value of attribute csv.



21
22
23
# File 'lib/apkg_to_csv/csv.rb', line 21

def csv
  @csv
end

Class Method Details

.from_notes(notes, models: []) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/apkg_to_csv/csv.rb', line 5

def self.from_notes(notes, models: [])
  csv_hash = {}

  models.each do |m|
    csv_hash[m.id] ||= []
    csv_hash[m.id] << m.fields
  end

  notes.each do |n|
    csv_hash[n.model_id] ||= []
    csv_hash[n.model_id] << n.fields
  end

  new(csv_hash)
end