class Brainstem::CLI::GenerateApiDocsCommand

Attributes

builder[RW]

Public Instance Methods

call() click to toggle source
# File lib/brainstem/cli/generate_api_docs_command.rb, line 27
def call
  ensure_sink_specified!
  construct_builder!
  present_atlas!
end
default_options() click to toggle source
# File lib/brainstem/cli/generate_api_docs_command.rb, line 37
def default_options
  {
    sink: {
      method: default_sink_method,
      options: {}
    },

    builder: {
      args_for_atlas: { controller_matches: [] },
      args_for_introspector: {
        base_presenter_class:  ::Brainstem::ApiDocs.method(:base_presenter_class),
        base_controller_class: ::Brainstem::ApiDocs.method(:base_controller_class),
        base_application_class: ::Brainstem::ApiDocs.method(:base_application_class),
      },
    },
  }
end
default_sink_method() click to toggle source
# File lib/brainstem/cli/generate_api_docs_command.rb, line 33
def default_sink_method
  Brainstem::ApiDocs::Sinks::ControllerPresenterMultifileSink.method(:new)
end

Private Instance Methods

builder_options() click to toggle source

Utility method for retrieving builder options.

# File lib/brainstem/cli/generate_api_docs_command.rb, line 92
def builder_options
  @builder_options ||= options[:builder]
end
construct_builder!() click to toggle source

Instantiates a builder, passing the relevant options to it.

# File lib/brainstem/cli/generate_api_docs_command.rb, line 64
def construct_builder!
  @builder = Brainstem::ApiDocs::Builder.new(builder_options)
end
ensure_sink_specified!() click to toggle source

Raises an error unless the user specified a destination for the output.

# File lib/brainstem/cli/generate_api_docs_command.rb, line 78
def ensure_sink_specified!
  raise Brainstem::ApiDocs::NoSinkSpecifiedException unless sink_method
end
option_parser() click to toggle source

Defines the option parser for this command.

@return [OptionParser] the option parser that should mutate the

+options+ hash.
# File lib/brainstem/cli/generate_api_docs_command.rb, line 109
def option_parser
  OptionParser.new do |opts|
    opts.banner = "Usage: generate [options]"

    opts.on('--host-env-file=PATH', "path to host app's entry file") do |o|
      options[:builder][:args_for_introspector][:rails_environment_file] = o
    end

    opts.on('-o RELATIVE_DIR', '--output-dir=RELATIVE_DIR',
            'specifies directory which to output if relevant') do |o|
      options[:sink][:options][:write_path] = o
    end

    opts.on('--base-presenter-class=CLASS', "which class to look up presenters on") do |o|
      options[:builder][:args_for_introspector][:base_presenter_class] = o
    end

    opts.on('--base-controller-class=CLASS', "which class to look up controllers on") do |o|
      options[:builder][:args_for_introspector][:base_controller_class] = o
    end

    opts.on('--base-application-class=CLASS', "which class to look up routes on") do |o|
      options[:builder][:args_for_introspector][:base_application_class] = o
    end

    opts.on('--controller-matches=MATCH',
            'a case-sensitive regexp used to winnow the list of '\
            'controllers. It is matched against the constant, not '\
            'underscored name of the controller. Specifying multiple '\
            'performs a logical AND between Regexen.') do |o|
      # Trim slashes on passed MATCH.
      matcher = Regexp.new(o.gsub(/(\A\/)|(\/\z)/, ''), 'i')
      options[:builder][:args_for_atlas][:controller_matches].push(matcher)
    end

    opts.on('--markdown', 'use markdown format') do |o|
      options[:sink][:options][:format] = :markdown
    end

    opts.on('-m', '--multifile-presenters-and-controllers',
      'dumps presenters and controllers to separate files (default)') do |o|
      if options[:sink][:options][:format] == :oas_v2
        raise NotImplementedError.new("Multi File support for Open API Specification is not supported yet")
      else
        options[:sink][:method] = Brainstem::ApiDocs::Sinks::ControllerPresenterMultifileSink.method(:new)
      end
    end

    opts.on('--output-extension=EXTENSION', 'extension that should be used for output files') do |extension|
      options[:sink][:options][:output_extension] = extension
    end

    opts.on('--include-internal', 'Generate docs for all `internal!` flagged presenters/controllers') do |_|
      options[:builder][:args_for_atlas][:include_internal] = true
    end

    #########################################################
    #                                                       #
    # Open API Specification generation specific commands:  #
    #                                                       #
    #########################################################

    # Future proofing for different Open API Specification versions.
    opts.on('--open-api-specification=VERSION',
            'dumps an Open API Specification for presenters and controllers in a single file') do |oas_version|
      case oas_version.to_i
        when 2
          options[:sink][:options][:format] = :oas_v2
        else
          raise NotImplementedError.new(
            "Please specify the version of Open API Specification to be generated e.g. --open-api-specification=2"
          )
      end
      options[:sink][:method] = Brainstem::ApiDocs::Sinks::OpenApiSpecificationSink.method(:new)
    end

    opts.on('--api-version=API_VERSION',
            'sets the version of the generated documentation') do |api_version|
      options[:sink][:options][:api_version] = api_version
    end

    opts.on('--ignore-tagging',
            'does not add the tag definitions in the Open API Specification') do |api_version|
      options[:sink][:options][:ignore_tagging] = true
    end

    opts.on('--oas-filename-pattern=PATTERN',
            'defines the naming pattern of the Open API Specification file') do |pattern|
      options[:sink][:options][:oas_filename_pattern] = pattern
    end
  end
end
present_atlas!() click to toggle source

Hands the atlas over to the sink.

# File lib/brainstem/cli/generate_api_docs_command.rb, line 71
def present_atlas!
  sink_method.call(sink_options) << builder.atlas
end
sink_method() click to toggle source

Utility method for retrieving the sink.

# File lib/brainstem/cli/generate_api_docs_command.rb, line 85
def sink_method
  @sink_method ||= options[:sink][:method]
end
sink_options() click to toggle source

Utility method for retrieving sink options.

# File lib/brainstem/cli/generate_api_docs_command.rb, line 99
def sink_options
  @sink_options ||= options[:sink][:options]
end