Class: Grape::Validations::ParamScopeTracker

Inherits:
Object
  • Object
show all
Defined in:
lib/grape/validations/param_scope_tracker.rb

Overview

Holds per-request mutable state that must not live on shared ParamsScope instances. Both trackers are identity-keyed hashes so that ParamsScope objects can serve as keys without relying on value equality.

Lifecycle is managed by Endpoint#run_validators via .track. Use .current to access the instance for the running request.

Constant Summary collapse

FIBER_KEY =

Fiber-local key used to store the current tracker. Fiber[] (Ruby 3.0+) is used instead of Thread.current[] so that fiber-based servers (e.g. Falcon with async) isolate each request’s tracker within its own fiber rather than sharing state across all fibers running on the same thread.

:grape_param_scope_tracker
EMPTY_PARAMS =
[].freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeParamScopeTracker

Returns a new instance of ParamScopeTracker.



32
33
34
35
# File 'lib/grape/validations/param_scope_tracker.rb', line 32

def initialize
  @index_tracker = {}.compare_by_identity
  @qualifying_params_tracker = {}.compare_by_identity
end

Class Method Details

.currentObject



28
29
30
# File 'lib/grape/validations/param_scope_tracker.rb', line 28

def self.current
  Fiber[FIBER_KEY]
end

.trackObject



20
21
22
23
24
25
26
# File 'lib/grape/validations/param_scope_tracker.rb', line 20

def self.track
  previous = Fiber[FIBER_KEY]
  Fiber[FIBER_KEY] = new
  yield
ensure
  Fiber[FIBER_KEY] = previous
end

Instance Method Details

#index_for(scope) ⇒ Object



41
42
43
# File 'lib/grape/validations/param_scope_tracker.rb', line 41

def index_for(scope)
  @index_tracker[scope]
end

#qualifying_params(scope) ⇒ Object

Returns qualifying params for scope, or EMPTY_PARAMS if none were stored. Note: an explicitly stored empty array and “never stored” are treated identically by callers (both yield a blank result that falls through to the parent params).



48
49
50
# File 'lib/grape/validations/param_scope_tracker.rb', line 48

def qualifying_params(scope)
  @qualifying_params_tracker.fetch(scope, EMPTY_PARAMS)
end

#store_index(scope, index) ⇒ Object



37
38
39
# File 'lib/grape/validations/param_scope_tracker.rb', line 37

def store_index(scope, index)
  @index_tracker.store(scope, index)
end

#store_qualifying_params(scope, params) ⇒ Object



52
53
54
# File 'lib/grape/validations/param_scope_tracker.rb', line 52

def store_qualifying_params(scope, params)
  @qualifying_params_tracker.store(scope, params)
end