Class: GrapeDocs::Exporter

Inherits:
Object
  • Object
show all
Defined in:
lib/grape_docs/exporter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(export_path) ⇒ Exporter

Returns a new instance of Exporter.



5
6
7
8
9
10
11
# File 'lib/grape_docs/exporter.rb', line 5

def initialize(export_path)
  @root = Workspace.dir(File.expand_path(export_path))

  # load template
  filename = "#{GrapeDocs.config[:template]}.md.erb"
  @template = load_template(filename)
end

Instance Attribute Details

#apiObject (readonly)

Returns the value of attribute api.



3
4
5
# File 'lib/grape_docs/exporter.rb', line 3

def api
  @api
end

#rootObject (readonly)

Returns the value of attribute root.



3
4
5
# File 'lib/grape_docs/exporter.rb', line 3

def root
  @root
end

#templateObject (readonly)

Returns the value of attribute template.



3
4
5
# File 'lib/grape_docs/exporter.rb', line 3

def template
  @template
end

Instance Method Details

#document_url(target) ⇒ Object



59
60
61
62
# File 'lib/grape_docs/exporter.rb', line 59

def document_url(target)
  path = target.path.sub(%r{^/}, '')
  "#{path}.md"
end

#export(api) ⇒ Object



25
26
27
28
29
30
31
32
# File 'lib/grape_docs/exporter.rb', line 25

def export(api)
  @api = api
  result = template.result(binding).gsub(/\n\n\n/, "\n\n")
  root.file("#{api.path}.md").write(result)
  api.children.each do |child|
    export(child)
  end
end

#export_readme(api) ⇒ Object



19
20
21
22
23
# File 'lib/grape_docs/exporter.rb', line 19

def export_readme(api)
  readme_template = load_template("generic/README.md.erb")
  result = readme_template.result(binding).gsub(/\n\n\n/, "\n\n")
  root.file("README.md").write(result)
end

#export_summary(api) ⇒ Object



13
14
15
16
17
# File 'lib/grape_docs/exporter.rb', line 13

def export_summary(api)
  summary_template = load_template("generic/SUMMARY.md.erb")
  result = summary_template.result(binding).gsub(/\n\n\n/, "\n\n")
  root.file("SUMMARY.md").write(result)
end

#json(data) ⇒ Object



55
56
57
# File 'lib/grape_docs/exporter.rb', line 55

def json(data)
  "```json\n#{JSON.pretty_generate(data)}\n```\n"
end

#table(params, *fields, &block) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/grape_docs/exporter.rb', line 34

def table(params, *fields, &block)
  params.each do |param|
    yield param if block_given?
  end
  rows = []
  rows.push(fields.map { |f| f.to_s.titlecase })
  params.each do |param|
    rows.push(fields.map { |f| param[f]&.to_s || "-" })
  end
  seperator = []
  fields.each_with_index do |field, index|
    field_max = rows.map { |row| row[index] }.max_by(&:length).length
    seperator.push(":#{'-' * (field_max - 1)}")
    rows.each_with_index do |row, i|
      rows[i][index] = rows[i][index] + " " * (field_max - rows[i][index].length)
    end
  end
  rows.insert(1, seperator)
  rows.map { |row| "| #{row.join(' | ')} |" }.join("\n")
end