Module: Bundler::Audit::CLI::Formats

Defined in:
lib/bundler/audit/cli/formats.rb,
lib/bundler/audit/cli/formats/json.rb,
lib/bundler/audit/cli/formats/text.rb,
lib/bundler/audit/cli/formats/junit.rb

Overview

Bundler::Audit::CLI supports outputting it's audit results as text or JSON, by default.

API

Bundler::Audit::CLI formats are really just modules defined under Formats, that define a #print_report entry method. When the #print_report method is called it will be passed a report object and an optional output stream, which may be either $stdout or a File object.

Bundler::Audit::CLI will load a format by first calling Formats.load, which attempts to require the bundler/audit/cli/formats/#{format}.rb file, then gets the registered format module using Formats.[]. If the format module has been successfully loaded, it will be extended into the Bundler::Audit::CLI instance method access to Bundler::Audit::CLI's instance methods.

Custom Formats

To define a custom format, it...

  • MUST be defined in a file in a lib/bundler/audit/cli/formats/ directory.
  • MUST define a print_report(report,output=$stdout) instance method.
  • MUST register themselves by calling Formats.register at the end of the file.

Example

# lib/bundler/audit/cli/formats/my_format.rb
module Bundler
  module Audit
    class CLI < ::Thor
      module Formats
        module MyFormat
          def print_report(report,output=$stdout)
            # ...
          end
        end

        Formats.register :my_format, MyFormat
      end
    end
  end
end

Defined Under Namespace

Modules: JSON, Junit, Text Classes: FormatNotFound

Constant Summary collapse

DIR =

Directory where format modules are loaded from.

'bundler/audit/cli/formats'

Class Method Summary collapse

Class Method Details

.[](name) ⇒ Module#print_results?

Retrieves the format by name.

Parameters:

  • name (String, Symbol)

Returns:

  • (Module#print_results, nil)

    The format registered with the given name or nil.



111
112
113
# File 'lib/bundler/audit/cli/formats.rb', line 111

def self.[](name)
  @registry[name.to_sym]
end

.load(name) ⇒ Module#print_results

Loads the format with the given name by attempting to require bundler/audit/cli/formats/#{name} and returning the registered format using [].

Parameters:

  • name (#to_s)

Returns:

  • (Module#print_results)

Raises:



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/bundler/audit/cli/formats.rb', line 127

def self.load(name)
  name = name.to_s
  path = File.join(DIR,File.basename(name))

  begin
    require path
  rescue LoadError
    raise(FormatNotFound,"could not load format #{name.inspect}")
  end

  unless (format = self[name])
    raise(FormatNotFound,"unknown format #{name.inspect}")
  end

  return  format
end

.register(name, format) ⇒ Object

Registers a format with the given format name.

Parameters:

  • name (Symbol, String)
  • format (Module#print_results)

    The format object.

Raises:

  • (NotImplementedError)

    The format object does not respond to #call.



95
96
97
98
99
100
101
# File 'lib/bundler/audit/cli/formats.rb', line 95

def self.register(name,format)
  unless format.instance_methods.include?(:print_report)
    raise(NotImplementedError,"#{format.inspect} does not define #print_report")
  end

  @registry[name.to_sym] = format
end