Class: Origami::Importer::Writer

Inherits:
Object
  • Object
show all
Defined in:
lib/origami-importer/writer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(batch) ⇒ Writer

Returns a new instance of Writer.



7
8
9
# File 'lib/origami-importer/writer.rb', line 7

def initialize(batch)
  self.batch = batch
end

Instance Attribute Details

#batchObject

Returns the value of attribute batch.



6
7
8
# File 'lib/origami-importer/writer.rb', line 6

def batch
  @batch
end

Instance Method Details

#<<(records) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/origami-importer/writer.rb', line 11

def <<(records)
  records = [records].flatten.compact
  result = {}
  records.each do |record|
    key = record.keys.first
    value = record.values.first
    result[key] ||= []
    result[key] << value
  end
  save result
end

#save(records) ⇒ Object



23
24
25
26
27
# File 'lib/origami-importer/writer.rb', line 23

def save(records)
  unless records.empty?
    batch.save records
  end
end