Class: IDRegistry::RegistryMiddleware::ClearRegistry

Inherits:
Object
  • Object
show all
Defined in:
lib/idregistry/middleware.rb

Overview

A registry task that clears a registry at the end of a request.

You may also provide an optional condition block, which is called and passed the Rack env to determine whether the registry should be cleared. If no condition block is provided, the registry is always cleared.

Instance Method Summary collapse

Constructor Details

#initialize(registry_, opts_ = {}, &condition_) ⇒ ClearRegistry

Create a new ClearRegistry task. You must provide the registry and an optional condition block. If you set the :before_request option to true, the registry clearing will take place at the beginning of the request rather than the end.



67
68
69
70
71
# File 'lib/idregistry/middleware.rb', line 67

def initialize(registry_, opts_={}, &condition_)
  @condition = condition_
  @registry = registry_
  @before = opts_[:before_request]
end

Instance Method Details

#post(env_) ⇒ Object

The pre method applies if :before_request is not set.



81
82
83
84
85
# File 'lib/idregistry/middleware.rb', line 81

def post(env_)
  if !@before && (!@condition || @condition.call(env_))
    @registry.clear
  end
end

#pre(env_) ⇒ Object

The pre method applies if :before_request is set.



74
75
76
77
78
# File 'lib/idregistry/middleware.rb', line 74

def pre(env_)
  if @before && (!@condition || @condition.call(env_))
    @registry.clear
  end
end