class ApiTommy::Generator

Public Class Methods

setup_options(options) click to toggle source
# File lib/api_tommy/generator.rb, line 5
def self.setup_options(options)
  options.dry_run = true
  op = options.option_parser

  op.on("--filename FILENAME", String, "The output filename") do |filename|
    options.filename = filename.gsub(/\s+/, "-")
    unless options.filename.end_with?(".md")
      options.filename = "#{options.filename}.md"
    end
  end

  op.on("--header HEADER", String, "The header filename") do |header|
    options.header = header
  end

  op.on("--footer FOOTER", String, "The footer filename") do |footer|
    options.footer = footer
  end
end

Private Instance Methods

arguments(title = "Parameters") click to toggle source
# File lib/api_tommy/generator.rb, line 52
def arguments(title = "Parameters")
  return if @tomdoc.arguments.empty?
  @content << @h.h3(title)
  @content << @h.th("Name", "Description")
  @tomdoc.arguments.each do |a|
    @content << @h.tr(a.name.to_s, a.description)
  end
end
comment(object) click to toggle source
# File lib/api_tommy/generator.rb, line 84
def comment(object)
  result = object.comment
  return result if result.is_a?(String)
  result.text
end
examples() click to toggle source
# File lib/api_tommy/generator.rb, line 61
def examples
  return if @tomdoc.examples.empty?
  @content << @h.h3("Examples")
  @tomdoc.examples.each do |e|
    @content << @h.code(e.to_s)
  end
end
finalize_content() click to toggle source
# File lib/api_tommy/generator.rb, line 94
def finalize_content
  in_doc_folder do
    if @options.header
      @content = "#{File.read(@options.header)}\n#{@content}"
    end
    @content << File.read(@options.footer) if @options.footer
  end
end
generate_class_doc(clazz) click to toggle source
# File lib/api_tommy/generator.rb, line 27
def generate_class_doc(clazz)
  generate_class_header(clazz)
  clazz.instance_method_list.each { |method| generate_method_doc(method) }
end
generate_class_header(clazz) click to toggle source
# File lib/api_tommy/generator.rb, line 32
def generate_class_header(clazz)
  @content << @h.h1(clazz.name.gsub(/Controller/, ""))
  @tomdoc = TomParse.parse(comment(clazz).split("---").first)
  @content << @h.p(@tomdoc.description)

  arguments("Fields")
  examples
end
generate_method_doc(method) click to toggle source
# File lib/api_tommy/generator.rb, line 41
def generate_method_doc(method)
  @tomdoc = TomParse.parse(comment(method))
  @content << @h.h2(@tomdoc.description.split(".").first)
  @content << @h.p(@tomdoc.description)

  returns
  arguments
  examples
  raises
end
in_doc_folder() { || ... } click to toggle source
# File lib/api_tommy/generator.rb, line 117
def in_doc_folder
  FileUtils.cd(Dir.pwd.end_with?("/doc") ? ".." : Dir.pwd) do
    yield
  end
end
log(message, level = :info) click to toggle source
# File lib/api_tommy/generator.rb, line 90
def log(message, level = :info)
  puts "[#{level}] #{message}"
end
raises() click to toggle source
# File lib/api_tommy/generator.rb, line 76
def raises
  return if @tomdoc.raises.empty?
  @content << @h.h3("Errors")
  @tomdoc.raises.each do |r|
    @content << @h.p(r.to_s.gsub(/Raises\s/, ""))
  end
end
returns() click to toggle source
# File lib/api_tommy/generator.rb, line 69
def returns
  return if @tomdoc.returns.empty?
  @tomdoc.returns.each do |r|
    @content << @h.p(r.to_s)
  end
end
update_wiki() click to toggle source
# File lib/api_tommy/generator.rb, line 103
def update_wiki
  in_doc_folder do
    if $DEBUG_RDOC
      filepath = File.join(%W(doc #{@options.filename || "api_tommy.md"}))
      log("Writing to local file: #{filepath}", :warning)
      File.open(filepath, "w") { |f| f.write(@content) }
    else
      log("Updating Github wiki...")
      Github.new.update_wiki(@options.filename || "API.md", @content)
    end
    log("Done.")
  end
end