Class: Flame::Dispatcher

Inherits:
Object
  • Object
show all
Includes:
Static
Defined in:
lib/flame/dispatcher.rb,
lib/flame/static.rb

Overview

Helpers for dispatch Flame::Application#call

Defined Under Namespace

Modules: Static

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Static

#return_static, #static_cached?, #try_static

Constructor Details

#initialize(app, env) ⇒ Dispatcher

Returns a new instance of Dispatcher.



17
18
19
20
21
22
# File 'lib/flame/dispatcher.rb', line 17

def initialize(app, env)
	@app = app
	@env = env
	@request = Flame::Request.new(env)
	@response = Flame::Response.new
end

Instance Attribute Details

#requestObject (readonly)

Returns the value of attribute request.



11
12
13
# File 'lib/flame/dispatcher.rb', line 11

def request
  @request
end

#responseObject (readonly)

Returns the value of attribute response.



11
12
13
# File 'lib/flame/dispatcher.rb', line 11

def response
  @response
end

Instance Method Details

#configObject



52
53
54
# File 'lib/flame/dispatcher.rb', line 52

def config
	@app.config
end

#cookiesObject



48
49
50
# File 'lib/flame/dispatcher.rb', line 48

def cookies
	@cookies ||= Cookies.new(request.cookies, response)
end

#halt(new_status = nil, body = nil, new_headers = {}) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
# File 'lib/flame/dispatcher.rb', line 63

def halt(new_status = nil, body = nil, new_headers = {})
	case new_status
	when String then body = new_status
	when Integer then status new_status
	end
	# new_status.is_a?(String) ? () : (status new_status)
	body = default_body if body.nil? && response.body.empty?
	response.body = body if body
	response.headers.merge!(new_headers)
	throw :halt
end

#paramsObject



40
41
42
# File 'lib/flame/dispatcher.rb', line 40

def params
	@params ||= request.params.merge(request.params.keys_to_sym)
end

#path_to(ctrl, action = :index, args = {}) ⇒ Object



56
57
58
59
60
61
# File 'lib/flame/dispatcher.rb', line 56

def path_to(ctrl, action = :index, args = {})
	route = @app.class.router.find_route(controller: ctrl, action: action)
	fail RouteNotFoundError.new(ctrl, action) unless route
	path = route.assign_arguments(args)
	path.empty? ? '/' : path
end

#run!Object



24
25
26
27
28
29
30
31
32
# File 'lib/flame/dispatcher.rb', line 24

def run!
	catch :halt do
		try_route ||
		  try_static ||
		  try_static(File.join(__dir__, '..', '..', 'public')) ||
		  try_error(404)
	end
	response.finish
end

#sessionObject



44
45
46
# File 'lib/flame/dispatcher.rb', line 44

def session
	request.session
end

#status(value = nil) ⇒ Object



34
35
36
37
38
# File 'lib/flame/dispatcher.rb', line 34

def status(value = nil)
	response.status ||= 200
	response.headers['X-Cascade'] = 'pass' if value == 404
	value ? response.status = value : response.status
end