lib/rspec_api_docs/formatter.rb in rspec-api-docs-0.9.0 vs lib/rspec_api_docs/formatter.rb in rspec-api-docs-0.10.0
- old
+ new
@@ -1,8 +1,9 @@
require 'rspec/core/formatters/base_formatter'
require 'rspec_api_docs'
+require 'rspec_api_docs/resource_collection'
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'
@@ -16,37 +17,39 @@
#
# rspec --format=RspecApiDocs::Formatter
class Formatter < RSpec::Core::Formatters::BaseFormatter
RSpec::Core::Formatters.register self, :example_passed, :close
- attr_reader :resources
+ attr_reader :renderer
- def initialize(*args)
- @resources = {}
+ def initialize(*args, renderer: default_renderer)
+ @renderer = renderer
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)
+ resource_collection.add_example(rspec_example)
end
# Calls the configured renderer with the stored {Resource}s.
#
# @return [void]
def close(null_notification)
- renderer.new(resources.values.sort_by { |resource| [resource.precedence, resource.name].join }).render
+ renderer.new(resource_collection.all).render
end
private
- def renderer
+ def resource_collection
+ @resource_collection ||= ResourceCollection.new
+ end
+
+ def default_renderer
value = RspecApiDocs.configuration.renderer
case value
when :json
Renderer::JSONRenderer