class Brainstem::ApiDocs::Atlas

Attributes

controller_matches[RW]

Holds +Regexp+s which each controller name must match in order to be included in the list of endpoints.

controllers[RW]
endpoints[RW]
introspector[RW]

Set and read the introspector.

presenters[RW]
resolver[RW]

Public Class Methods

new(introspector, options = {}) click to toggle source
Calls superclass method Brainstem::Concerns::Optional::new
# File lib/brainstem/api_docs/atlas.rb, line 20
def initialize(introspector, options = {})
  self.endpoints          = EndpointCollection.new(self, options)
  self.controllers        = ControllerCollection.new(self, options)
  self.presenters         = ::Brainstem::ApiDocs::PresenterCollection.new(self, options)
  self.resolver           = Resolver.new(self)

  self.controller_matches = []
  self.introspector       = introspector

  super options

  parse_routes!
  extract_presenters!
  validate!
end

Private Instance Methods

allow_route?(route) click to toggle source

Returns whether a route's controller passes the limiting regexp passed to the generation command.

# File lib/brainstem/api_docs/atlas.rb, line 138
def allow_route?(route)
  introspector.controllers.include?(route[:controller]) &&
    controller_matches.all? { |regexp| route[:controller].to_s =~ regexp }
end
allowed_routes() click to toggle source

Returns a list of all routes that pass the user's filtering.

# File lib/brainstem/api_docs/atlas.rb, line 76
def allowed_routes
  introspector.routes.keep_if(&method(:allow_route?))
end
extract_presenters!() click to toggle source

Extracts declared presents for each endpoint and converts it into a Presenter wrapper object.

# File lib/brainstem/api_docs/atlas.rb, line 100
def extract_presenters!
  valid_presenter_pairs.each do |target_class, const|
    presenter = presenters.find_or_create_from_presenter_collection(target_class, const)

    endpoints
      .select do |ep|
        declared_presented_class = ep.declared_presented_class
        !declared_presented_class.nil? && declared_presented_class.to_s == target_class
      end
        .each {|ep| ep.presenter = presenter }
  end
end
parse_routes!() click to toggle source

Constructs Endpoint and +Controller wrappers per route.

# File lib/brainstem/api_docs/atlas.rb, line 83
def parse_routes!
  allowed_routes.each do |route|
    if (endpoint = endpoints.find_from_route(route))
      endpoint.merge_http_methods!(route[:http_methods])
    else
      controller  = controllers.find_or_create_from_route(route)
      endpoint    = endpoints.create_from_route(route, controller)

      controller.add_endpoint(endpoint)
    end
  end
end
valid?() click to toggle source

Whether this Atlas is valid (i.e. if it has at least one endpoint).

@return [Boolean] if the atlas is valid

# File lib/brainstem/api_docs/atlas.rb, line 130
def valid?
  endpoints.count > 0
end
valid_options() click to toggle source

Lists valid options that may be passed on instantiation.

# File lib/brainstem/api_docs/atlas.rb, line 50
def valid_options
  super | [ :controller_matches ]
end
valid_presenter_pairs() click to toggle source

Returns a list of valid +target_class_to_s => PresenterConst+ pairs, determining validity by whether they descend from the base presenter.

@return [Hash{String => Class}] valid pairs

# File lib/brainstem/api_docs/atlas.rb, line 119
def valid_presenter_pairs
  Brainstem.presenter_collection.presenters.select do |target_class, const|
    introspector.presenters.include? const
  end
end
validate!() click to toggle source

Ensures the atlas is valid before allowing consumers to make requests of it.

# File lib/brainstem/api_docs/atlas.rb, line 58
def validate!
  raise InvalidAtlasError, "Atlas is not valid." unless valid?
end