class Racket::Utils::Routing::ActionCache

Class for caching actions

Attributes

items[R]

Public Class Methods

new(logger) click to toggle source
# File lib/racket/utils/routing.rb, line 38
def initialize(logger)
  @items = {}
  @logger = logger
end
service(_options = {}) click to toggle source

Returns a service proc that can be used by the registry.

@param [Hash] _options (unused) @return [Proc]

# File lib/racket/utils/routing.rb, line 32
def self.service(_options = {})
  ->(reg) { new(reg.application_logger) }
end

Public Instance Methods

add(controller_class) click to toggle source

Caches all actions for a controller class. This is used on every request to quickly decide whether an action is valid or not.

@param [Class] controller_class @return [nil]

# File lib/racket/utils/routing.rb, line 58
def add(controller_class)
  __add(controller_class)
  actions = @items[controller_class].to_a
  @items[controller_class] = actions
  @logger.inform_dev(
    "Registering actions #{actions} for #{controller_class}."
  ) && nil
end
present?(controller_class, action) click to toggle source

Returns whether controller_class is in the cache and that it contains the action action.

@param [Class] controller_class @param [Symbol] action @return [true|false]

# File lib/racket/utils/routing.rb, line 49
def present?(controller_class, action)
  @items.fetch(controller_class, []).include?(action)
end

Private Instance Methods

__add(controller_class) click to toggle source

Internal handler for adding actions to the cache.

@param [Class] controller_class @return [nil]

# File lib/racket/utils/routing.rb, line 73
def __add(controller_class)
  return if controller_class == Controller
  actions = @items.fetch(controller_class, SortedSet.new)
  @items[controller_class] = actions.merge(controller_class.public_instance_methods(false))
  __add(controller_class.superclass) && nil
end