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: [], **kwargs) ⇒ Builder

Returns a new instance of Builder.



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

def initialize(output = +'', items: [], **kwargs)
  @output = output
  @cols = Cols.new
  @items = items
  @utf8_bom = kwargs.fetch(:utf8_bom) { Csb.configuration.utf8_bom }
  @csv_options = kwargs.fetch(:csv_options) { Csb.configuration.csv_options }
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

#csv_optionsObject (readonly)

Returns the value of attribute csv_options.



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

def csv_options
  @csv_options
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



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

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

    raise error
  end
  output
end