Module: Brainstem

Defined in:
lib/brainstem.rb,
lib/brainstem/cli.rb,
lib/brainstem/version.rb,
lib/brainstem/api_docs.rb,
lib/brainstem/dsl/field.rb,
lib/brainstem/preloader.rb,
lib/brainstem/presenter.rb,
lib/brainstem/test_helpers.rb,
lib/brainstem/time_classes.rb,
lib/brainstem/api_docs/atlas.rb,
lib/brainstem/dsl/base_block.rb,
lib/brainstem/concerns/lookup.rb,
lib/brainstem/dsl/association.rb,
lib/brainstem/dsl/block_field.rb,
lib/brainstem/dsl/conditional.rb,
lib/brainstem/api_docs/builder.rb,
lib/brainstem/dsl/fields_block.rb,
lib/brainstem/api_docs/endpoint.rb,
lib/brainstem/api_docs/resolver.rb,
lib/brainstem/concerns/optional.rb,
lib/brainstem/dsl/configuration.rb,
lib/brainstem/api_docs/presenter.rb,
lib/brainstem/controller_methods.rb,
lib/brainstem/api_docs/controller.rb,
lib/brainstem/api_docs/exceptions.rb,
lib/brainstem/presenter_validator.rb,
lib/brainstem/cli/abstract_command.rb,
lib/brainstem/concerns/formattable.rb,
lib/brainstem/dsl/hash_block_field.rb,
lib/brainstem/presenter_collection.rb,
lib/brainstem/dsl/array_block_field.rb,
lib/brainstem/concerns/presenter_dsl.rb,
lib/brainstem/dsl/associations_block.rb,
lib/brainstem/dsl/conditionals_block.rb,
lib/brainstem/concerns/controller_dsl.rb,
lib/brainstem/search_unavailable_error.rb,
lib/brainstem/api_docs/sinks/stdout_sink.rb,
lib/brainstem/concerns/error_presentation.rb,
lib/brainstem/api_docs/abstract_collection.rb,
lib/brainstem/api_docs/endpoint_collection.rb,
lib/brainstem/api_docs/sinks/abstract_sink.rb,
lib/brainstem/api_docs/presenter_collection.rb,
lib/brainstem/cli/generate_api_docs_command.rb,
lib/brainstem/api_docs/controller_collection.rb,
lib/brainstem/query_strategies/base_strategy.rb,
lib/brainstem/query_strategies/filter_or_search.rb,
lib/brainstem/concerns/inheritable_configuration.rb,
lib/brainstem/query_strategies/filter_and_search.rb,
lib/brainstem/api_docs/formatters/markdown/helper.rb,
lib/brainstem/concerns/controller_param_management.rb,
lib/brainstem/api_docs/formatters/abstract_formatter.rb,
lib/brainstem/api_docs/introspectors/rails_introspector.rb,
lib/brainstem/api_docs/introspectors/abstract_introspector.rb,
lib/brainstem/api_docs/formatters/markdown/endpoint_formatter.rb,
lib/brainstem/api_docs/formatters/markdown/presenter_formatter.rb,
lib/brainstem/api_docs/formatters/markdown/controller_formatter.rb,
lib/brainstem/api_docs/sinks/controller_presenter_multifile_sink.rb,
lib/brainstem/api_docs/formatters/markdown/endpoint_collection_formatter.rb

Overview

Responsible for formatting each endpoint.

Defined Under Namespace

Modules: ApiDocs, CLI, Concerns, ControllerMethods, DSL, QueryStrategies, TestHelpers Classes: Cli, Preloader, Presenter, PresenterCollection, PresenterValidator, SearchUnavailableError

Constant Summary collapse

VERSION =
"1.3.0"

Class Method Summary collapse

Class Method Details

.add_presenter_class(presenter_class, namespace, *klasses) ⇒ Object

Helper method to quickly add presenter classes that are in a namespace. For example, add_presenter_class(Api::V1::UserPresenter, “User”) would add UserPresenter to the PresenterCollection for the :v1 namespace as the presenter for the User class.

Parameters:

  • presenter_class (Brainstem::Presenter)

    The presenter class that is being registered.

  • klasses (Array<String, Class>)

    Classes that will be presented by the given presenter.



50
51
52
# File 'lib/brainstem.rb', line 50

def self.add_presenter_class(presenter_class, namespace, *klasses)
  presenter_collection(namespace).add_presenter_class(presenter_class, *klasses)
end

.default_namespaceString

The namespace that will be used by presenter_collection and add_presenter_class if none is given or implied.

Returns:

  • (String)

    the default namespace



20
21
22
# File 'lib/brainstem.rb', line 20

def self.default_namespace
  @default_namespace || "none"
end

.default_namespace=(namespace) ⇒ String

Sets default_namespace to a new value.

Parameters:

  • namespace (String)

Returns:

  • (String)

    the new default namespace



14
15
16
# File 'lib/brainstem.rb', line 14

def self.default_namespace=(namespace)
  @default_namespace = namespace
end

.loggerLogger

Returns The Brainstem logger. If Rails is loaded, defaults to the Rails logger. If Rails is not loaded, defaults to a STDOUT logger.

Returns:

  • (Logger)

    The Brainstem logger. If Rails is loaded, defaults to the Rails logger. If Rails is not loaded, defaults to a STDOUT logger.



55
56
57
58
59
60
61
62
63
64
# File 'lib/brainstem.rb', line 55

def self.logger
  @logger ||= begin
    if defined?(Rails)
      Rails.logger
    else
      require "logger"
      Logger.new(STDOUT)
    end
  end
end

.logger=(logger) ⇒ Logger

Sets a new Brainstem logger.

Parameters:

  • logger (Logger)

    A new Brainstem logger.

Returns:

  • (Logger)

    The new Brainstem logger.



69
70
71
# File 'lib/brainstem.rb', line 69

def self.logger=(logger)
  @logger = logger
end

.mysql_use_calc_found_rowsBoolean

Whether or not to use MYSQL_CALC_FOUND_ROWS to calculate the result set count instead of issuing two queries.

Returns:

  • (Boolean)

    the mysql_use_calc_found_rows setting



33
34
35
# File 'lib/brainstem.rb', line 33

def self.mysql_use_calc_found_rows
  @mysql_use_calc_found_rows || false
end

.mysql_use_calc_found_rows=(bool) ⇒ Boolean

Sets mysql_use_calc_found_rows to a new value.

Parameters:

  • bool (Boolean)

Returns:

  • (Boolean)

    the new mysql_use_calc_found_rows setting



27
28
29
# File 'lib/brainstem.rb', line 27

def self.mysql_use_calc_found_rows=(bool)
  @mysql_use_calc_found_rows = bool
end

.presenter_collection(namespace = nil) ⇒ PresenterCollection

Returns the PresenterCollection for the given namespace.

Parameters:

  • namespace (String) (defaults to: nil)

Returns:



39
40
41
42
43
# File 'lib/brainstem.rb', line 39

def self.presenter_collection(namespace = nil)
  namespace ||= default_namespace
  @presenter_collection ||= {}
  @presenter_collection[namespace.to_s.downcase] ||= PresenterCollection.new
end

.reset!Object

Reset all PresenterCollection’s Presenters, clear the known collections, and reset the default namespace. This is mostly intended for resetting between tests.



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/brainstem.rb', line 75

def self.reset!
  if @presenter_collection
    @presenter_collection.each do |namespace, collection|
      collection.presenters.each do |klass, presenter|
        presenter.reset! if presenter.respond_to?(:reset!)
      end
    end
  end

  Brainstem::Presenter.reset!
  Brainstem::Presenter.reset_configuration!

  @presenter_collection = {}
  @default_namespace = nil
  @mysql_use_calc_found_rows = false
end