Class: Dcmgr::Rack::RunInitializer

Inherits:
Object
  • Object
show all
Defined in:
lib/dcmgr/rack/run_initializer.rb

Overview

Rack middleware for running initialization/setup procedure. Case 1: only when the HTTP request came first time. Case 2: every time when the HTTP request comes.

ex. use InitializeFirstRequest, proc

# run setup codes. for example, establish database connection etc..

Instance Method Summary collapse

Constructor Details

#initialize(app, run_once, run_every = nil) ⇒ RunInitializer

Returns a new instance of RunInitializer.

Raises:

  • (ArgumentError)


15
16
17
18
19
20
21
# File 'lib/dcmgr/rack/run_initializer.rb', line 15

def initialize(app, run_once, run_every=nil)
  raise ArgumentError unless run_once.nil? || run_once.is_a?(Proc)
  raise ArgumentError unless run_every.nil? || run_every.is_a?(Proc)
  @app = app
  @run_once_block = run_once
  @run_every_block = run_every
end

Instance Method Details

#call(env) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/dcmgr/rack/run_initializer.rb', line 23

def call(env)
  def call(env)
    if @run_every_block
      @run_every_block.arity == 1 ? @run_every_block.call(env) : @run_every_block.call
    end
    @app.call(env)
  end

  if @run_once_block
    @run_once_block.arity == 1 ? @run_once_block.call(env) : @run_once_block.call
  end
  if @run_every_block
    @run_every_block.arity == 1 ? @run_every_block.call(env) : @run_every_block.call
  end
  @app.call(env)
end