class RSpec::Core::Formatters::Loader
interface.
not expected to be used directly, but only through the configuration
managing formatters used by a particular configuration. It is
`RSpec::Core::Formatters::Loader` is an internal class for
@api private
def add(formatter_to_use, *paths)
- Private: -
def add(formatter_to_use, *paths) formatter_class = find_formatter(formatter_to_use) args = paths.map { |p| p.respond_to?(:puts) ? p : file_at(p) } formatter = formatter_class.new(*args) if @setup @reporter.register_listener formatter, *RSpec::Core::Reporter::NOTIFICATIONS end @formatters << formatter unless duplicate_formatter_exists?(formatter) formatter end
def built_in_formatter(key)
def built_in_formatter(key) case key.to_s when 'd', 'doc', 'documentation' DocumentationFormatter when 's', 'n', 'spec', 'nested' RSpec.deprecate "Using `#{key.to_s}` as a shortcut for the DocumentationFormatter", :replacement => "`d`, `doc`, or `documentation`" DocumentationFormatter when 'h', 'html' HtmlFormatter when 'p', 'progress' ProgressFormatter when 'j', 'json' JsonFormatter when 't', 'textmate' if defined?(::RSpec::Mate::Formatters::TextMateFormatter) RSpec.deprecate "Using the text`#{key.to_s}` as a shortcut for the TextMateFormatter", :replacement => "`::RSpec::Mate::Formatters::TextMateFormatter`" else RSpec.deprecate "Using rspec-core's `::RSpec::Core::TextMateFormatter`", :replacement => "the `rspec-tmbundle` gem and it's `::RSpec::Mate::Formatters::TextMateFormatter`" end TextMateFormatter end end
def custom_formatter(formatter_ref)
def custom_formatter(formatter_ref) if Class === formatter_ref formatter_ref elsif string_const?(formatter_ref) begin formatter_ref.gsub(/^::/,'').split('::').inject(Object) { |const,string| const.const_get string } rescue NameError require( path_for(formatter_ref) ) ? retry : raise end end end
def duplicate_formatter_exists?(new_formatter)
def duplicate_formatter_exists?(new_formatter) @formatters.any? do |formatter| formatter.class === new_formatter && formatter.output == new_formatter.output end end
def file_at(path)
def file_at(path) FileUtils.mkdir_p(File.dirname(path)) File.new(path, 'w') end
def find_formatter(formatter_to_use)
def find_formatter(formatter_to_use) built_in_formatter(formatter_to_use) || custom_formatter(formatter_to_use) || (raise ArgumentError, "Formatter '#{formatter_to_use}' unknown - maybe you meant 'documentation' or 'progress'?.") end
def initialize(reporter)
- Api: - private
def initialize(reporter) @formatters = [] @reporter = reporter @setup = false @default_formatter = 'progress' end
def path_for(const_ref)
def path_for(const_ref) underscore_with_fix_for_non_standard_rspec_naming(const_ref) end
def setup_default(output_stream, deprecation_stream)
- Private: -
def setup_default(output_stream, deprecation_stream) if @formatters.empty? add @default_formatter, output_stream end unless @formatters.any? { |formatter| DeprecationFormatter === formatter } add DeprecationFormatter, deprecation_stream, output_stream end @formatters.each do |formatter| @reporter.register_listener formatter, *RSpec::Core::Reporter::NOTIFICATIONS end @setup = true end
def string_const?(str)
def string_const?(str) str.is_a?(String) && /\A[A-Z][a-zA-Z0-9_:]*\z/ =~ str end
def underscore(camel_cased_word)
def underscore(camel_cased_word) word = camel_cased_word.to_s.dup word.gsub!(/::/, '/') word.gsub!(/([A-Z]+)([A-Z][a-z])/,'\1_\2') word.gsub!(/([a-z\d])([A-Z])/,'\1_\2') word.tr!("-", "_") word.downcase! word end
def underscore_with_fix_for_non_standard_rspec_naming(string)
def underscore_with_fix_for_non_standard_rspec_naming(string) underscore(string).sub(%r{(^|/)r_spec($|/)}, '\\1rspec\\2') end