lib/ghost_writer.rb in ghost_writer-0.1.1 vs lib/ghost_writer.rb in ghost_writer-0.2.0

- old
+ new

@@ -8,48 +8,65 @@ module Format autoload "Markdown", "ghost_writer/format/markdown" end + DEFAULT_OUTPUT_DIR = "api_examples" DOCUMENT_INDEX_FILENAME = "document_index.markdown" class << self - attr_accessor :output_dir, :github_base_url + attr_writer :output_dir, :output_flag + attr_accessor :github_base_url - def documents - @documents ||= [] - @documents + def document_group + @document_group ||= {} + @document_group end def generate_api_doc - if ENV["GENERATE_API_DOC"] + if output_flag unless File.exist?(output_path) FileUtils.mkdir_p(output_path) end - document_index = GhostWriter::DocumentIndex.new(output_path + DOCUMENT_INDEX_FILENAME, documents) + document_index = GhostWriter::DocumentIndex.new(output_path + DOCUMENT_INDEX_FILENAME, document_group) document_index.write_file - @documents.each(&:write_file) - @documents.clear + document_group.each do |output, docs| + docs.sort_by!(&:description) + docs.shift.write_file(true) + docs.each(&:write_file) + end + + document_group.clear end end + def output_flag + !!(@output_flag ? @output_flag : ENV["GENERATE_API_DOC"]) + end + + def output_dir + @output_dir ? @output_dir : DEFAULT_OUTPUT_DIR + end + def output_path - output_dir ? Rails.root + "doc" + output_dir : Rails.root + "doc" + "api_examples" + Rails.root + "doc" + output_dir end end def collect_example - document = GhostWriter::Document.new(File.join(doc_dir, "#{doc_name}.markdown"), { + output = File.join(doc_dir, "#{doc_name}.markdown") + document = GhostWriter::Document.new(output, { title: "#{described_class} #{doc_name.titleize}", description: example.full_description.dup, location: example.location.dup, url_example: "#{request.env["REQUEST_METHOD"]} #{request.env["PATH_INFO"]}", param_example: controller.params.reject {|key, val| key == "controller" || key == "action"}, status_example: response.status.inspect, response_example: response.body, }) - GhostWriter.documents << document + GhostWriter.document_group[output] ||= [] + GhostWriter.document_group[output] << document end private def doc_dir GhostWriter.output_path + described_class.to_s.underscore @@ -64,10 +81,10 @@ end included do after do if example.metadata[:type] == :controller && example.metadata[:generate_api_doc] - collect_example if ENV["GENERATE_API_DOC"] + collect_example if GhostWriter.output_flag end end end end