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:



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

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`



89
90
91
92
# File 'lib/flame/controller.rb', line 89

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

Class Method Details

.default_pathObject

Default root path of the controller for requests



105
106
107
108
109
110
# File 'lib/flame/controller.rb', line 105

def default_path
	modules = name.underscore.split('/')
	parts = modules[-1].split('_') - %w(index controller ctrl)
	return modules[-2] if parts.empty?
	parts.join('_')
end

Instance Method Details

#attachment(filename = nil, disposition = :attachment) ⇒ Object

Set the Content-Disposition to “attachment” with the specified filename, instructing the user agents to prompt to save.



46
47
48
49
50
51
52
53
# File 'lib/flame/controller.rb', line 46

def attachment(filename = nil, disposition = :attachment)
	content_dis = 'Content-Disposition'.freeze
	response[content_dis] = disposition.to_s
	return unless filename
	response[content_dis] << "; filename=\"#{File.basename(filename)}\""
	ext = File.extname(filename)
	content_type(ext) unless response[Rack::CONTENT_TYPE] || ext.empty?
end

#execute(method) ⇒ Object

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

Parameters:

  • method (Symbol)

    name of the controller method



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/flame/controller.rb', line 71

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



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

def path_to(*args)
	add_controller_class(args)
	@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



38
39
40
41
42
# File 'lib/flame/controller.rb', line 38

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

#url_to(*args) ⇒ Object

Build a URI to the given controller and action, or path



22
23
24
25
# File 'lib/flame/controller.rb', line 22

def url_to(*args)
	path = args.first.is_a?(String) ? args.first : path_to(*args)
	"#{request.scheme}://#{request.host_with_port}#{path}"
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



59
60
61
62
63
64
65
66
# File 'lib/flame/controller.rb', line 59

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