Class: Csb::Builder

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

Constant Summary collapse

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(output = '', items: [], utf8_bom: false) ⇒ Builder

Returns a new instance of Builder.



11
12
13
14
15
16
# File 'lib/csb/builder.rb', line 11

def initialize(output = '', items: [], utf8_bom: false)
  @output = output
  @utf8_bom = utf8_bom
  @cols = Cols.new
  @items = items
end

Instance Attribute Details

#colsObject (readonly)

Returns the value of attribute cols.



8
9
10
# File 'lib/csb/builder.rb', line 8

def cols
  @cols
end

#itemsObject

Returns the value of attribute items.



8
9
10
# File 'lib/csb/builder.rb', line 8

def items
  @items
end

#outputObject (readonly)

Returns the value of attribute output.



8
9
10
# File 'lib/csb/builder.rb', line 8

def output
  @output
end

#utf8_bomObject (readonly)

Returns the value of attribute utf8_bom.



8
9
10
# File 'lib/csb/builder.rb', line 8

def utf8_bom
  @utf8_bom
end

Instance Method Details

#buildObject



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/csb/builder.rb', line 18

def build
  output << UTF8_BOM if utf8_bom
  output << CSV.generate_line(cols.headers)
  items.each do |item|
    output << CSV.generate_line(cols.values_by_item(item))
  rescue => error
    break if Csb.configuration.ignore_class_names.include?(error.class.name)

    raise error
  end
  output
end