Class: Praxis::Dispatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/praxis/dispatcher.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(application: Application.instance) ⇒ Dispatcher

Returns a new instance of Dispatcher.



32
33
34
35
36
# File 'lib/praxis/dispatcher.rb', line 32

def initialize(application: Application.instance)
  @stages = []
  @application = application
  setup_stages!
end

Class Attribute Details

.deferred_callbacksObject (readonly)

Returns the value of attribute deferred_callbacks.



17
18
19
# File 'lib/praxis/dispatcher.rb', line 17

def deferred_callbacks
  @deferred_callbacks
end

Instance Attribute Details

#actionObject (readonly)

Returns the value of attribute action.



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

def action
  @action
end

#applicationObject (readonly)

Returns the value of attribute application.



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

def application
  @application
end

#controllerObject (readonly)

Returns the value of attribute controller.



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

def controller
  @controller
end

#requestObject (readonly)

Returns the value of attribute request.



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

def request
  @request
end

Class Method Details

.after(*stage_path, **conditions, &block) ⇒ Object



24
25
26
# File 'lib/praxis/dispatcher.rb', line 24

def self.after(*stage_path, **conditions, &block)
  @deferred_callbacks[:after] << [conditions, block]
end

.before(*stage_path, **conditions, &block) ⇒ Object



20
21
22
# File 'lib/praxis/dispatcher.rb', line 20

def self.before(*stage_path, **conditions, &block)
  @deferred_callbacks[:before] << [conditions, block]
end

.current(thread: Thread.current, application: Application.instance) ⇒ Object



28
29
30
# File 'lib/praxis/dispatcher.rb', line 28

def self.current(thread: Thread.current, application: Application.instance)
  thread[:praxis_dispatcher] ||= self.new(application: application)
end

Instance Method Details

#after(*stage_path, &block) ⇒ Object



63
64
65
66
# File 'lib/praxis/dispatcher.rb', line 63

def after(*stage_path, &block)
  stage_name = stage_path.shift
  stages.find { |stage| stage.name == stage_name }.after(*stage_path, &block)
end

#before(*stage_path, &block) ⇒ Object



58
59
60
61
# File 'lib/praxis/dispatcher.rb', line 58

def before(*stage_path, &block)
  stage_name = stage_path.shift
  stages.find { |stage| stage.name == stage_name }.before(*stage_path, &block)
end

#dispatch(controller_class, action, request) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/praxis/dispatcher.rb', line 68

def dispatch(controller_class, action, request)
  @controller = controller_class.new(request)
  @action = action
  @request = request

  @stages.each do |stage|
    result = stage.run
    case result
    when Response
      return result.finish
    end
  end

  controller.response.finish
rescue => e
  response = Responses::InternalServerError.new(error: e)
  response.request = controller.request
  response.finish
ensure
  @controller = nil
  @action = nil
  @request = nil
end

#reset_cache!Object



93
94
95
96
97
98
99
# File 'lib/praxis/dispatcher.rb', line 93

def reset_cache!
  return unless Praxis::Blueprint.caching_enabled?

  Praxis::Blueprint.cache = Hash.new do |hash, key|
    hash[key] = Hash.new
  end
end

#setup_deferred_callbacks!Object



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/praxis/dispatcher.rb', line 46

def setup_deferred_callbacks!
  self.class.deferred_callbacks.each do |stage_name, callbacks|
    callbacks[:before].each do |(*stage_path, block)|
      self.before(stage_name, *stage_path, &block)
    end

    callbacks[:after].each do |(*stage_path, block)|
      self.after(stage_name, *stage_path, &block)
    end
  end
end

#setup_stages!Object



38
39
40
41
42
43
44
# File 'lib/praxis/dispatcher.rb', line 38

def setup_stages!
  @stages << RequestStages::LoadRequest.new(:load_request, self)
  @stages << RequestStages::Validate.new(:validate, self)
  @stages << RequestStages::Action.new(:action, self)
  @stages << RequestStages::Response.new(:response, self)
  setup_deferred_callbacks!
end