class Yaframework::Base
Attributes
env[R]
req[R]
request[R]
res[R]
response[R]
routes[R]
settings[R]
Public Class Methods
new()
click to toggle source
# File lib/yaframework/base.rb, line 14 def initialize @routes = Hash.new([]) @before_hooks = [] @after_hooks = [] @settings = {} @inbox = {} end
Public Instance Methods
after(&block)
click to toggle source
# File lib/yaframework/base.rb, line 51 def after(&block) @after_hooks << block end
before(&block)
click to toggle source
# File lib/yaframework/base.rb, line 47 def before(&block) @before_hooks << block end
call(env)
click to toggle source
# File lib/yaframework/base.rb, line 36 def call(env) @env = env @request = Yaframework::Request.new @env @response = Yaframework::Response.new catch(:halt) { route_eval } end
configure(&block)
click to toggle source
# File lib/yaframework/base.rb, line 32 def configure(&block) exec(block) if block_given? end
halt(response)
click to toggle source
# File lib/yaframework/base.rb, line 43 def halt(response) throw :halt, response end
handle(status, &block)
click to toggle source
# File lib/yaframework/base.rb, line 59 def handle(status, &block) @inbox[status] = block end
helpers(&block)
click to toggle source
# File lib/yaframework/base.rb, line 28 def helpers(&block) exec(block) if block_given? end
listen(port = 5000)
click to toggle source
# File lib/yaframework/base.rb, line 63 def listen(port = 5000) Rack::Handler::WEBrick.run self, Port: port end
params()
click to toggle source
# File lib/yaframework/base.rb, line 55 def params request.params end
Private Instance Methods
add_route(verb, path, &handler)
click to toggle source
# File lib/yaframework/base.rb, line 69 def add_route(verb, path, &handler) path = "/#{path}" unless path[0] == "/" @routes[verb] << compile(path, &handler) end
compile(path, &handler)
click to toggle source
# File lib/yaframework/base.rb, line 74 def compile(path, &handler) route = { handler: handler, compiled_path: nil, extra_params: [], path: path } compiled_path = path.gsub(/:\w+/) do |match| route[:extra_params] << match.gsub(":", "").to_sym "([^/?#]+)" end route[:compiled_path] = /^#{compiled_path}$/ route end
exec(action)
click to toggle source
# File lib/yaframework/base.rb, line 115 def exec(action) if action.respond_to? :to_sym send(action) else instance_exec(&action) end end
exec_after_hooks()
click to toggle source
# File lib/yaframework/base.rb, line 127 def exec_after_hooks exec_hooks @after_hooks end
exec_before_hooks()
click to toggle source
# File lib/yaframework/base.rb, line 123 def exec_before_hooks exec_hooks @before_hooks end
exec_hooks(hooks)
click to toggle source
# File lib/yaframework/base.rb, line 131 def exec_hooks(hooks) return true if hooks.nil? hooks.each do |hook| return false if exec(hook) == false end end
find_route()
click to toggle source
# File lib/yaframework/base.rb, line 101 def find_route route = routes[request.request_method].detect do |r| r[:compiled_path] =~ request.path_info end if route $~.captures.each_with_index do |value, index| param = route[:extra_params][index] request.params[param] = value end end route end
route_eval()
click to toggle source
# File lib/yaframework/base.rb, line 85 def route_eval route = find_route response.status = 404 unless route if @inbox[response.status] response.write exec(@inbox[response.status]) return response.finish end exec_before_hooks response.write exec(route[:handler]) if route exec_after_hooks response.finish end