Class: Rango::Controller

Included Modules

Rango::HttpExceptions, Rango::Helpers, Rango::ControllerMixin, Rango::Templates::TemplateHelpers

Attributes

Instance Attributes

session [RW] public

Returns the value of attribute session.

Constructor Summary

public initialize(request, params)
[View source]


102
103
104
105
106
107
108
# File 'lib/rango/mvc/controller.rb', line 102

def initialize(request, params)
  @request = request
  @params  = params
  @cookies = request.cookies
  @session = request.session
  Rango.logger.inspect(params: params, cookies: cookies, session: session)
end

Public Visibility

Public Class Method Summary

after(action = nil, options = Hash.new, &block)
before(action = nil, options = Hash.new, &block)

before :login before :login, actions: [:send].

call(env)
master
Change Merb::Controller to respond to #call and return a Rack Array.
controller?
get_filters(type)
inherited(subclass)
proceed_value(value)
route_to(env, action, params = Hash.new)

Public Class Methods Included from Rango::Helpers

load, load_helpers

Public Instance Method Summary

#route_to(action, params = Hash.new)
#run_filters(name, method)

Public Instance Methods Inherited from Object

try

Public Class Method Details

after

public after(action = nil, options = Hash.new, &block)

Meta Tags

Parameters:

Since:

0.0.2

[View source]


40
41
42
# File 'lib/rango/mvc/controller.rb', line 40

def after(action = nil, options = Hash.new, &block)
  self.after_filters[action || block] = options
end

before

public before(action = nil, options = Hash.new, &block)

before :login before :login, actions: [:send]

Meta Tags

Parameters:

Since:

0.0.2

[View source]


35
36
37
# File 'lib/rango/mvc/controller.rb', line 35

def before(action = nil, options = Hash.new, &block)
  self.before_filters[action || block] = options
end

call

public call(env)
master
Change Merb::Controller to respond to #call and return a Rack Array. (wycats)rubyurl.com/BhoY

Meta Tags

Parameters:

Since:

0.0.2

[View source]


46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/rango/mvc/controller.rb', line 46

def call(env)
  self.set_rack_env(env)
  request = Rango::Request.new(env)
  options = env["rango.router.params"]
  method = options[:action] || :index
  response = Rack::Response.new
  controller = self.new(request, options.merge(request.params))
  controller.response = response
  Rango.logger.info("#{self.name}.call(env) with method #{method}")
  # Rango.logger.inspect(before: ::Application.get_filters(:before), after: ::Application.get_filters(:after))
  # Rango.logger.inspect(before: get_filters(:before), after: get_filters(:after))
  controller.run_filters(:before, method.to_sym)
  # If you don't care about arguments or if you prefer usage of params.
  args = controller.params.map { |key, value| value }
  if controller.method(method).arity.eql?(0)
    Rango.logger.info("Calling method #{self.name}##{method} without arguments")
    value = controller.method(method).call
  else
    Rango.logger.info("Calling method #{self.name}##{method} with arguments #{args.inspect}")
    value = controller.method(method).call(*args)
  end
  controller.autorender if self.autorendering
  controller.run_filters(:after, method)
  response.body = proceed_value(value)
  response.status = controller.status if controller.status
  response.headers.merge!(controller.headers)
  return response.finish
end

controller?

public controller?
[View source]


92
93
94
# File 'lib/rango/mvc/controller.rb', line 92

def controller?
  true
end

get_filters

public get_filters(type)

Meta Tags

Parameters:

Since:

0.0.2

[View source]


97
98
99
# File 'lib/rango/mvc/controller.rb', line 97

def get_filters(type)
  self.send("#{type}_filters")
end

inherited

public inherited(subclass)
[View source]


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

def inherited(subclass)
  Rango.logger.debug("Inheritting filters from #{self.inspect} to #{subclass.inspect}")
  subclass.before_filters = self.before_filters
  subclass.after_filters = self.after_filters
  subclass.autorendering = self.autorendering
end

proceed_value

public proceed_value(value)
[View source]


84
85
86
87
88
89
90
# File 'lib/rango/mvc/controller.rb', line 84

def proceed_value(value)
  case value
  when true, false then value.to_s
  when nil then String.new
  else value
  end
end

route_to

public route_to(env, action, params = Hash.new)
[View source]


76
77
78
79
80
81
82
# File 'lib/rango/mvc/controller.rb', line 76

def route_to(env, action, params = Hash.new)
  env["rango.controller"] = self
  env["rango.controller.action"] = action
  env["rango.router.params"] = params
  env["rango.action"] = action
  Rango::Router::Dispatcher.route_to(env, self)
end

Public Instance Method Details

route_to

public route_to(action, params = Hash.new)
[View source]


111
112
113
# File 'lib/rango/mvc/controller.rb', line 111

def route_to(action, params = Hash.new)
  self.class.route_to(request.env, action, params)
end

run_filters

public run_filters(name, method)

Meta Tags

Parameters:

Since:

0.0.2

[View source]


116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/rango/mvc/controller.rb', line 116

def run_filters(name, method)
  # Rango.logger.debug(self.class.instance_variables)
  # Rango.logger.inspect(name: name, method: method)
  self.class.get_filters(name).each do |filter_method, options|
    begin
      unless options[:except] && options[:except].include?(method)
        if filter_method.is_a?(Symbol) && self.respond_to?(filter_method)
          Rango.logger.info("Calling filter #{filter_method} for controller #{self}")
          self.send(filter_method)
        elsif filter_method.respond_to?(:call)
          Rango.logger.info("Calling filter #{filter_method.inspect} for controller #{self}")
          self.instance_eval(&filter_method)
        else
          Rango.logger.error("Filter #{filter_method} doesn't exists!")
        end
      end
    rescue SkipFilter
      Rango.logger.info("Skipping #{name} filter")
      next
    end
  end
end