Class: GrapeDocs::Exporter
- Inherits:
-
Object
- Object
- GrapeDocs::Exporter
- Defined in:
- lib/grape_docs/exporter.rb
Instance Attribute Summary collapse
-
#api ⇒ Object
readonly
Returns the value of attribute api.
-
#root ⇒ Object
readonly
Returns the value of attribute root.
-
#template ⇒ Object
readonly
Returns the value of attribute template.
Instance Method Summary collapse
- #document_url(target) ⇒ Object
- #export(api) ⇒ Object
-
#initialize(export_path) ⇒ Exporter
constructor
A new instance of Exporter.
- #json(data) ⇒ Object
- #table(params, *fields, &block) ⇒ Object
Constructor Details
#initialize(export_path) ⇒ Exporter
Returns a new instance of Exporter.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/grape_docs/exporter.rb', line 5 def initialize(export_path) @root = Workspace.dir(File.(export_path)) # load template filename = "#{GrapeDocs.config[:template]}.md.erb" current_dir = Workspace.dir(Dir.pwd) template_file = current_dir.file(filename) unless template_file.exists? spec = Gem::Specification.find_by_name("grape-docs") assets_dir = Workspace::Dir.new(spec.gem_dir).dir("assets") template_file = assets_dir.file(filename) unless template_file.exists? template_file = assets_dir.file("default.md.erb") end end @template = ERB.new(template_file.read, nil, "%") end |
Instance Attribute Details
#api ⇒ Object (readonly)
Returns the value of attribute api.
3 4 5 |
# File 'lib/grape_docs/exporter.rb', line 3 def api @api end |
#root ⇒ Object (readonly)
Returns the value of attribute root.
3 4 5 |
# File 'lib/grape_docs/exporter.rb', line 3 def root @root end |
#template ⇒ Object (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
57 58 59 60 |
# File 'lib/grape_docs/exporter.rb', line 57 def document_url(target) path = target.path.sub(%r{^/}, '') "#{path}.md" end |
#export(api) ⇒ Object
23 24 25 26 27 28 29 30 |
# File 'lib/grape_docs/exporter.rb', line 23 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 |
#json(data) ⇒ Object
53 54 55 |
# File 'lib/grape_docs/exporter.rb', line 53 def json(data) "```json\n#{JSON.pretty_generate(data)}\n```\n" end |
#table(params, *fields, &block) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/grape_docs/exporter.rb', line 32 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 |