Class: Defmastership::Export::Formatter

Inherits:
Object
  • Object
show all
Defined in:
lib/defmastership/export/formatter.rb

Overview

to export a CSV file

Defined Under Namespace

Modules: Helper

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(doc) ⇒ Formatter

Returns a new instance of Formatter.

Parameters:

  • doc (Document)

    the document to export



15
16
17
# File 'lib/defmastership/export/formatter.rb', line 15

def initialize(doc)
  @doc = doc
end

Instance Attribute Details

#docObject (readonly)

Returns the value of attribute doc.



12
13
14
# File 'lib/defmastership/export/formatter.rb', line 12

def doc
  @doc
end

Instance Method Details

#body(definition, column_list) ⇒ Array<String>

Build content of a row from the list of columns

Parameters:

  • definition (Definition)

    sourec of data

  • column_list (Array<Symbol>)

    List of columns to include

Returns:

  • (Array<String>)

    List of colums values



41
42
43
44
45
# File 'lib/defmastership/export/formatter.rb', line 41

def body(definition, column_list)
  body_formatter = BodyFormatter.new(doc, definition)
  body_line = column_list.map { |part| body_formatter.public_send(part) }
  body_line.flatten
end

#export_to(_output_file) ⇒ Object

Export the document to a CSV file

Parameters:

  • _output_file (String)

    filename for the export

Raises:

  • (NotImplementedError)


22
23
24
# File 'lib/defmastership/export/formatter.rb', line 22

def export_to(_output_file)
  raise(NotImplementedError, "#{self.class} has not implemented the 'export_to' method")
end

#header(column_list) ⇒ Array<String>

Build content of the header from the list of columns

Parameters:

  • column_list (Array<Symbol>)

    List of columns to include

Returns:

  • (Array<String>)

    List of colums values



30
31
32
33
34
# File 'lib/defmastership/export/formatter.rb', line 30

def header(column_list)
  header_formatter = HeaderFormatter.new(doc)
  header_line = column_list.map { |part| header_formatter.public_send(part) }
  header_line.flatten
end