Class: Kugiru::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/kugiru/builder.rb

Constant Summary collapse

UTF8_BOM =
"\xEF\xBB\xBF".freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(output = '', cols:, data:, utf8_bom: false) ⇒ Builder

Returns a new instance of Builder.



9
10
11
12
13
14
# File 'lib/kugiru/builder.rb', line 9

def initialize(output = '', cols:, data:, utf8_bom: false)
  @output = output
  @utf8_bom = utf8_bom
  @cols = cols
  @data = data
end

Instance Attribute Details

#colsObject (readonly)

Returns the value of attribute cols.



7
8
9
# File 'lib/kugiru/builder.rb', line 7

def cols
  @cols
end

#dataObject (readonly)

Returns the value of attribute data.



7
8
9
# File 'lib/kugiru/builder.rb', line 7

def data
  @data
end

#outputObject (readonly)

Returns the value of attribute output.



7
8
9
# File 'lib/kugiru/builder.rb', line 7

def output
  @output
end

#utf8_bomObject (readonly)

Returns the value of attribute utf8_bom.



7
8
9
# File 'lib/kugiru/builder.rb', line 7

def utf8_bom
  @utf8_bom
end

Class Method Details

.build(**args) ⇒ Object



26
27
28
# File 'lib/kugiru/builder.rb', line 26

def self.build(**args)
  self.new(args).build
end

.build_enumerator(**args) ⇒ Object



30
31
32
33
34
# File 'lib/kugiru/builder.rb', line 30

def self.build_enumerator(**args)
  Enumerator.new do |y|
    self.new(y, args).build
  end
end

Instance Method Details

#buildObject



16
17
18
19
20
21
22
23
24
# File 'lib/kugiru/builder.rb', line 16

def build
  output << UTF8_BOM if utf8_bom
  output << CSV.generate_line(cols.keys)
  data.each do |row|
    values = cols.values.map { |pr| pr.call(row) }
    output << CSV.generate_line(values)
  end
  output
end