Class: Toggly::Rails::Middleware
- Inherits:
-
Object
- Object
- Toggly::Rails::Middleware
- Defined in:
- lib/toggly/rails/middleware.rb
Overview
Rack middleware for request-scoped Toggly context.
Stores the evaluation context in the request environment for access throughout the request lifecycle.
Constant Summary collapse
- CONTEXT_KEY =
Environment key for storing the context
"toggly.context"- USER_KEY =
"toggly.current_user"
Class Method Summary collapse
-
.context(env) ⇒ Toggly::Context?
Get the current context from the request environment.
-
.set_context(env, context) ⇒ Toggly::Context
Set the current context in the request environment.
-
.set_user(env, user) ⇒ Object
Set the current user for context building.
-
.user(env) ⇒ Object?
Get the current user from the request environment.
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#initialize(app) ⇒ Middleware
constructor
A new instance of Middleware.
Constructor Details
#initialize(app) ⇒ Middleware
Returns a new instance of Middleware.
14 15 16 |
# File 'lib/toggly/rails/middleware.rb', line 14 def initialize(app) @app = app end |
Class Method Details
.context(env) ⇒ Toggly::Context?
Get the current context from the request environment
35 36 37 |
# File 'lib/toggly/rails/middleware.rb', line 35 def context(env) env[CONTEXT_KEY] end |
.set_context(env, context) ⇒ Toggly::Context
Set the current context in the request environment
44 45 46 |
# File 'lib/toggly/rails/middleware.rb', line 44 def set_context(env, context) env[CONTEXT_KEY] = context end |
.set_user(env, user) ⇒ Object
Set the current user for context building
53 54 55 |
# File 'lib/toggly/rails/middleware.rb', line 53 def set_user(env, user) env[USER_KEY] = user end |
.user(env) ⇒ Object?
Get the current user from the request environment
61 62 63 |
# File 'lib/toggly/rails/middleware.rb', line 61 def user(env) env[USER_KEY] end |
Instance Method Details
#call(env) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/toggly/rails/middleware.rb', line 18 def call(env) # Clear any existing context at the start of the request env.delete(CONTEXT_KEY) env.delete(USER_KEY) @app.call(env) ensure # Clean up after request env.delete(CONTEXT_KEY) env.delete(USER_KEY) end |