Class: Defmastership::Export::JSON::Formatter

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

Overview

to export a JSON file

Instance Attribute Summary

Attributes inherited from Formatter

#doc

Instance Method Summary collapse

Methods inherited from Formatter

#body, #header

Constructor Details

#initialize(doc, _options = {}) ⇒ Formatter

Returns a new instance of Formatter.

Parameters:

  • doc (Document)

    the document to export

  • _options (Hash{Symbol => Object}) (defaults to: {})

    unused options



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

def initialize(doc, _options = {})
  super(doc)
end

Instance Method Details

#export_to(output_file) ⇒ Object

Export the document to a JSON file

Parameters:

  • output_file (String)

    filename for the export



22
23
24
25
26
27
28
29
30
# File 'lib/defmastership/export/json/formatter.rb', line 22

def export_to(output_file)
  column_list = build_column_list
  header_column_list = header(column_list).map(&:to_sym)
  definitions =
    doc.definitions.map do |definition|
      header_column_list.zip(body(definition, column_list)).to_h
    end
  File.write(output_file, ::JSON.pretty_generate(definitions))
end