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



8
9
10
# File 'lib/flame/controller.rb', line 8

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

Helpers from Flame::Dispatcher



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

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



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

def self.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

#path_to(*args) ⇒ Object

Helpers



13
14
15
16
# File 'lib/flame/controller.rb', line 13

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

#redirect(*params) ⇒ Object



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

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

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



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

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