lib/polytrix/rspec/documentation_formatter.rb in polytrix-0.0.1 vs lib/polytrix/rspec/documentation_formatter.rb in polytrix-0.1.0.pre

- old
+ new

@@ -4,38 +4,63 @@ require 'fileutils' module Polytrix module RSpec class DocumentationFormatter < ::RSpec::Core::Formatters::BaseFormatter + include Polytrix::Core::FileSystemHelper + def initialize(output) + @templates_dir = 'doc-src' + @output_dir = 'docs' @results = Hashie::Mash.new + @summary_files = %w(index) super end def example_group_finished(example_group) - group_names = example_group.parent_groups.map{|g| g.description} - polytrix_challenges = example_group.examples.map { |e| e.metadata[:polytrix] } - produce_doc example_group.description, polytrix_challenges + polytrix_challenges = example_group.examples.map { |e| e.metadata[:polytrix_challenge] } + target_file = target_file_for example_group + template_file = template_for example_group + produce_doc template_file, target_file, example_group.description, polytrix_challenges if template_file end def dump_summary(duration, example_count, failure_count, pending_count) - doc_gen = Polytrix::DocumentationGenerator.new 'doc-src' - all_challenges = examples.map{|e| e.metadata[:polytrix]} + all_challenges = examples.map { |e| e.metadata[:polytrix_challenge] } grouped_challenges = all_challenges.compact.group_by(&:name) - produce_doc 'index', grouped_challenges + @summary_files.each do |summary_file| + template_file = template_for summary_file, use_default: false + next if template_file.nil? + target_file = target_file_for_summary(template_file) + produce_doc template_file, target_file, 'Summary', grouped_challenges + end end private - def produce_doc(name, data) - doc_gen = Polytrix::DocumentationGenerator.new 'doc-src' - doc = doc_gen.process(name, data) - target_file = doc_gen.template_file.to_s.gsub 'doc-src', 'docs' - unless target_file.empty? - FileUtils.mkdir_p File.dirname(target_file) - File.open(target_file, 'wb') do |f| - f.write doc - end + + def template_for(name, opts = { use_default: true }) + name = name.description if name.respond_to? :description + begin + find_file @templates_dir, name, '' + rescue Polytrix::Core::FileSystemHelper::FileNotFound + Polytrix.configuration.default_doc_template if opts[:use_default] == true end end + + def target_file_for(example_group) + names = [@output_dir].concat(example_group.parent_groups.reverse.map(&:description)) + # Markdown format by default, but will be overridden to match the template + slugify(names.join File::SEPARATOR) + '.md' + end + + def target_file_for_summary(template_file) + name = File.basename(template_file) + slugify("docs/#{name}") + end + + def produce_doc(template_file, target_file, scenario, data) + doc_gen = Polytrix::DocumentationGenerator.new template_file, scenario + doc_gen.process data + doc_gen.save target_file + end end end -end \ No newline at end of file +end