Class: Flame::Controller

Inherits:
Object
  • Object
show all
Defined in:
lib/flame/controller.rb

Overview

Class initialize when Dispatcher found route with it For new request and response

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dispatcher) ⇒ Controller

Initialize the controller for request execution

Parameters:



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

def initialize(dispatcher)
	@dispatcher = dispatcher
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args, &block) ⇒ Object

Call helpers methods from Flame::Dispatcher



71
72
73
74
# File 'lib/flame/controller.rb', line 71

def method_missing(m, *args, &block)
	return super unless @dispatcher.respond_to?(m)
	@dispatcher.send(m, *args, &block)
end

Class Method Details

.default_path(last = false) ⇒ Object

Default root path of the controller for requests



80
81
82
83
84
# File 'lib/flame/controller.rb', line 80

def default_path(last = false)
	(name.split('::').last.underscore.split('_') - %w(index controller ctrl))
	  .join('/').split('/')
	  .unshift(nil)[(last ? -1 : 0)..-1].join('/')
end

Instance Method Details

#execute(method) ⇒ Object

Execute the method of the controller with hooks (may be overloaded)

Parameters:

  • method (Symbol) —

    name of the controller method



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/flame/controller.rb', line 53

def execute(method)
	# send method
	body send(
		method,
		*params.values_at(
			*self.class.instance_method(method).parameters.map { |par| par[1] }
		)
	)
rescue => exception
	# p 'rescue from controller'
	status 500
	dump_error(exception)

	## Re-raise exception for inherited controllers or `Flame::Dispatcher`
	raise exception
end

#path_to(*args) ⇒ Object

Helpers



15
16
17
18
# File 'lib/flame/controller.rb', line 15

def path_to(*args)
	args.unshift self.class if args[0].is_a? Symbol
	@dispatcher.path_to(*args)
end

#redirect(path) ⇒ Object #redirect(*args) ⇒ Object

Redirect for response

Overloads:

  • #redirect(path) ⇒ Object

    Redirect to the string path

    Examples:

    Redirect to '/hello'

    redirect '/hello'

    Parameters:

    • path (String) —

      path

  • #redirect(*args) ⇒ Object

    Redirect to the path of path_to method

    Examples:

    Redirect to show method of ArticlesController with id = 2

    redirect ArticlesController, :show, id: 2

    Parameters:

    • args —

      arguments for path_to method



31
32
33
34
35
# File 'lib/flame/controller.rb', line 31

def redirect(*params)
	response.redirect(
		params[0].is_a?(String) ? params[0] : path_to(*params)
	)
end

#view(path = nil, options = {}) ⇒ String Also known as: render

Render a template with Flame::Render (based on Tilt-engine)

Parameters:

  • path (Symbol, nil) (defaults to: nil) —

    path to the template file

  • options (Hash) (defaults to: {}) —

    options for the Flame::Render rendering

Returns:

  • (String) —

    rendered template



41
42
43
44
45
46
47
48
# File 'lib/flame/controller.rb', line 41

def view(path = nil, options = {})
	template = Flame::Render.new(
		self,
		(path || caller_locations(1, 1)[0].label.to_sym),
		options
	)
	template.render(cache: config[:environment] == 'production')
end