Class: Qimport::Group

Inherits:
Object
  • Object
show all
Defined in:
lib/qimport/group.rb

Instance Method Summary collapse

Constructor Details

#initialize(*headers) ⇒ Group

Returns a new instance of Group.



3
4
5
6
7
8
# File 'lib/qimport/group.rb', line 3

def initialize(*headers)
  @headers = {}
  headers.each do |header|
    @headers[header] = {:lines => [], :columns => []}
  end
end

Instance Method Details

#line(data, header = nil) ⇒ Object



10
11
12
13
14
15
16
17
18
# File 'lib/qimport/group.rb', line 10

def line(data, header=nil)
  header ||= @headers.keys.first.to_sym

  @headers[header][:columns] =
    (@headers[header][:columns] + data.keys).uniq
  @headers[header][:lines] << data

  self
end

#to_sObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/qimport/group.rb', line 20

def to_s
  s = ''
  @headers.each do |k,v|
    header = k.to_s.upcase
    lines = v[:lines]
    columns = v[:columns]

    columns_to_s = columns.map{|c| c.to_s.upcase}.join("\t")

    s << "!#{header}\t#{columns_to_s}\r\n"

    lines.each do |line|
      empty_columns = array_to_empty_hash(columns)
      s << ([header] + empty_columns.merge(line).map{|k,v| v }).join("\t")
      s << "\r\n"
    end
  end
  s
end