Class: Racket::Utils::Routing::ActionCache

Inherits:
Object
  • Object
show all
Defined in:
lib/racket/utils/routing.rb

Overview

Class for caching actions

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(logger) ⇒ ActionCache

Returns a new instance of ActionCache.



38
39
40
41
# File 'lib/racket/utils/routing.rb', line 38

def initialize(logger)
  @items = {}
  @logger = logger
end

Instance Attribute Details

#itemsObject (readonly)

Returns the value of attribute items.



36
37
38
# File 'lib/racket/utils/routing.rb', line 36

def items
  @items
end

Class Method Details

.service(_options = {}) ⇒ Proc

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

Parameters:

  • _options (Hash) (defaults to: {})

    (unused)

Returns:

  • (Proc)


32
33
34
# File 'lib/racket/utils/routing.rb', line 32

def self.service(_options = {})
  ->(reg) { new(reg.application_logger) }
end

Instance Method Details

#add(controller_class) ⇒ nil

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

Parameters:

  • controller_class (Class)

Returns:

  • (nil)


58
59
60
61
62
63
64
65
# 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) ⇒ true|false

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

Parameters:

  • controller_class (Class)
  • action (Symbol)

Returns:

  • (true|false)


49
50
51
# File 'lib/racket/utils/routing.rb', line 49

def present?(controller_class, action)
  @items.fetch(controller_class, []).include?(action)
end