Module: RequestStore

Defined in:
lib/request_store/middleware.rb,
lib/request_store.rb,
lib/request_store/railtie.rb,
lib/request_store/version.rb

Overview

A middleware that ensures the RequestStore stays around until the last part of the body is rendered. This is useful when using streaming.

Uses Rack::BodyProxy, adapted from Rack::Lock’s usage of the same pattern.

Defined Under Namespace

Classes: Middleware, Railtie

Constant Summary collapse

VERSION =
"1.4.1"

Class Method Summary collapse

Class Method Details

.[](key) ⇒ Object



30
31
32
# File 'lib/request_store.rb', line 30

def self.[](key)
  store[key]
end

.[]=(key, value) ⇒ Object



38
39
40
# File 'lib/request_store.rb', line 38

def self.[]=(key, value)
  store[key] = value
end

.active?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/request_store.rb', line 22

def self.active?
  Thread.current[:request_store_active] || false
end

.begin!Object



14
15
16
# File 'lib/request_store.rb', line 14

def self.begin!
  Thread.current[:request_store_active] = true
end

.clear!Object



10
11
12
# File 'lib/request_store.rb', line 10

def self.clear!
  Thread.current[:request_store] = {}
end

.delete(key, &block) ⇒ Object



51
52
53
# File 'lib/request_store.rb', line 51

def self.delete(key, &block)
  store.delete(key, &block)
end

.end!Object



18
19
20
# File 'lib/request_store.rb', line 18

def self.end!
  Thread.current[:request_store_active] = false
end

.exist?(key) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/request_store.rb', line 42

def self.exist?(key)
  store.key?(key)
end

.fetch(key) ⇒ Object



46
47
48
49
# File 'lib/request_store.rb', line 46

def self.fetch(key)
  store[key] = yield unless exist?(key)
  store[key]
end

.read(key) ⇒ Object



26
27
28
# File 'lib/request_store.rb', line 26

def self.read(key)
  store[key]
end

.storeObject



6
7
8
# File 'lib/request_store.rb', line 6

def self.store
  Thread.current[:request_store] ||= {}
end

.write(key, value) ⇒ Object



34
35
36
# File 'lib/request_store.rb', line 34

def self.write(key, value)
  store[key] = value
end