class Orbit::Controller
Attributes
request[R]
response[R]
Public Class Methods
add_route(verb, action, &handler)
click to toggle source
# File lib/orbit/controller.rb, line 85 def self.add_route(verb, action, &handler) route = "#{verb.downcase}_#{action}" if routes.include?(route) update_method(verb, action, &handler) else routes.push(route) create_route(verb, action, &handler) end end
base_path()
click to toggle source
# File lib/orbit/controller.rb, line 57 def self.base_path @base_path ||= '/' end
create_route(verb, action, &handler)
click to toggle source
# File lib/orbit/controller.rb, line 96 def self.create_route(verb, action, &handler) method_name = create_method(verb, action, &handler) full_path = "#{@base_path}/#{action}" Orbit::Router.add(verb, full_path, self, method_name) end
delete(action, &handler)
click to toggle source
# File lib/orbit/controller.rb, line 77 def self.delete(action, &handler) add_route("DELETE", action, &handler) end
execute_action(request, action)
click to toggle source
# File lib/orbit/controller.rb, line 18 def self.execute_action(request, action) new(request).execute_action(action) end
get(action, &handler)
click to toggle source
# File lib/orbit/controller.rb, line 61 def self.get(action, &handler) add_route("GET", action, &handler) end
head(action, &handler)
click to toggle source
# File lib/orbit/controller.rb, line 81 def self.head(action, &handler) add_route("HEAD", action, &handler) end
layout(_layout = nil)
click to toggle source
# File lib/orbit/controller.rb, line 10 def self.layout(_layout = nil) if _layout @layout = "#{Dir.pwd}/#{_layout}" else @layout end end
new(request)
click to toggle source
# File lib/orbit/controller.rb, line 5 def initialize(request) @request = request @response = Orbit::Config.response_class.new end
patch(action, &handler)
click to toggle source
# File lib/orbit/controller.rb, line 73 def self.patch(action, &handler) add_route("PATCH", action, &handler) end
path(path)
click to toggle source
# File lib/orbit/controller.rb, line 48 def self.path(path) @base_path ||= superclass != Orbit::Controller ? superclass.base_path : '' @base_path += path end
post(action, &handler)
click to toggle source
# File lib/orbit/controller.rb, line 65 def self.post(action, &handler) add_route("POST", action, &handler) end
put(action, &handler)
click to toggle source
# File lib/orbit/controller.rb, line 69 def self.put(action, &handler) add_route("PUT", action, &handler) end
routes()
click to toggle source
# File lib/orbit/controller.rb, line 53 def self.routes @routes ||= [] end
Private Class Methods
create_method(verb, action, &handler)
click to toggle source
# File lib/orbit/controller.rb, line 141 def self.create_method(verb, action, &handler) method = (action == '/') ? "root" : parameterize(action.to_s.gsub("*", "splat")) method = "#{verb.downcase}_#{method}".to_sym method_name = method index = 0 while method_defined?(method_name) method_name = "#{method}_#{index}" index += 1 end define_method method_name, &handler method_name end
parameterize(string)
click to toggle source
# File lib/orbit/controller.rb, line 166 def self.parameterize(string) sep = '_' # Turn unwanted chars into the separator parameterized_string = string.to_s.gsub(/[^a-z0-9\-_]+/i, sep) re_sep = Regexp.escape(sep) # No more than one of the separator in a row. parameterized_string.gsub!(/#{re_sep}{2,}/, sep) # Remove leading/trailing separator. parameterized_string.gsub!(/^#{re_sep}|#{re_sep}$/i, '') parameterized_string.downcase end
update_method(verb, action, &handler)
click to toggle source
# File lib/orbit/controller.rb, line 157 def self.update_method(verb, action, &handler) method = (action == '/') ? "root" : parameterize(action.to_s.gsub("*", "splat")) method = "#{verb.downcase}_#{method}".to_sym define_method method, &handler method end
Public Instance Methods
execute_action(action)
click to toggle source
# File lib/orbit/controller.rb, line 22 def execute_action(action) result = send(action) result = result.to_s if result.respond_to? :to_s response.tap do |res| res.body = Array(result) end end
headers()
click to toggle source
# File lib/orbit/controller.rb, line 120 def headers request.headers end
last_request_update_allowed?()
click to toggle source
# File lib/orbit/controller.rb, line 136 def last_request_update_allowed? true end
locals()
click to toggle source
# File lib/orbit/controller.rb, line 44 def locals @_locals ||= TemplateBinding.new(params) end
params()
click to toggle source
# File lib/orbit/controller.rb, line 116 def params @request.params end
render(template)
click to toggle source
# File lib/orbit/controller.rb, line 32 def render(template) root_path = Dir.pwd template_location = "#{root_path}/#{template}" templates = [template_location, self.class.layout].compact templates.inject(nil) do | prev, template | _render(template) { prev } end end
responds_to_last_request_update_allowed?()
click to toggle source
# File lib/orbit/controller.rb, line 132 def responds_to_last_request_update_allowed? true end
session()
click to toggle source
# File lib/orbit/controller.rb, line 104 def session @request.session end
status()
click to toggle source
# File lib/orbit/controller.rb, line 124 def status response.status end
status=(code)
click to toggle source
# File lib/orbit/controller.rb, line 128 def status=(code) response.status = code end
Private Instance Methods
_render(template) { || ... }
click to toggle source
# File lib/orbit/controller.rb, line 180 def _render(template) file = File.read(template) ERB.new(file).result(locals.variables { yield }) end