Sha256: b84e48d31c1e6021f210b6639d5ce9f3b98690c40cd0511684e9f70f2e7d8935

Contents?: true

Size: 1.66 KB

Versions: 1

Compression:

Stored size: 1.66 KB

Contents

require 'rspec/core/formatters/base_formatter'

require 'rspec_api_docs'
require 'rspec_api_docs/formatter/resource'
require 'rspec_api_docs/formatter/renderer/json_renderer'
require 'rspec_api_docs/formatter/renderer/raddocs_renderer'
require 'rspec_api_docs/formatter/renderer/slate_renderer'

module RspecApiDocs
  # Unknown renderer configured.
  UnknownRenderer = Class.new(BaseError)

  # The RSpec formatter.
  #
  # Usage:
  #
  #     rspec --format=RspecApiDocs::Formatter
  class Formatter < RSpec::Core::Formatters::BaseFormatter
    RSpec::Core::Formatters.register self, :example_passed, :close

    attr_reader :resources

    def initialize(*args)
      @resources = {}
      super args
    end

    # Initializes and stores {Resource}s.
    #
    # @return [void]
    def example_passed(example_notification)
      rspec_example = example_notification.example
      return unless rspec_example.metadata[METADATA_NAMESPACE]
      resource = Resource.new(rspec_example)
      resources[resource.name] ||= resource
      resources[resource.name].add_example Resource::Example.new(rspec_example)
    end

    # Calls the configured renderer with the stored {Resource}s.
    #
    # @return [void]
    def close(null_notification)
      renderer.new(resources.values.sort_by(&:name)).render
    end

    private

    def renderer
      value = RspecApiDocs.configuration.renderer

      case value
      when :json
        Renderer::JSONRenderer
      when :raddocs
        Renderer::RaddocsRenderer
      when :slate
        Renderer::SlateRenderer
      when Class
        value
      else
        raise UnknownRenderer, "unknown renderer #{value.inspect}"
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rspec-api-docs-0.8.0 lib/rspec_api_docs/formatter.rb