class ActionFramework::Routes
Attributes
models[RW]
redirects[RW]
routes[RW]
Public Class Methods
new()
click to toggle source
# File lib/actionframework/routes.rb, line 13 def initialize @routes = {:get => {}, :post => {}, :update => {}, :delete => {}, :patch => {},:realtime => {}} @models = [] @redirects = [] end
Public Instance Methods
add_next(method,path,index,action)
click to toggle source
# File lib/actionframework/routes.rb, line 87 def add_next(method,path,index,action) @routes[method][build_regex(path)].insert(index,action); end
build_regex(string)
click to toggle source
# File lib/actionframework/routes.rb, line 19 def build_regex string string = string.gsub("{{","(?<") string = string.gsub("}}",">(.*))") string.insert(0,"^") string = string+"$" regex = Regexp.new (string) regex end
delete(hash)
click to toggle source
# File lib/actionframework/routes.rb, line 40 def delete hash @routes[:delete][build_regex(hash.keys.first.to_s)] = hash[hash.keys.first.to_s] end
get(hash)
click to toggle source
# File lib/actionframework/routes.rb, line 28 def get hash @routes[:get][build_regex(hash.keys.first.to_s)] = hash[hash.keys.first.to_s] end
model(name)
click to toggle source
# File lib/actionframework/routes.rb, line 48 def model name # @models[name of the class of the model] = name of class of the access policy of the model @models << name end
patch(hash)
click to toggle source
# File lib/actionframework/routes.rb, line 44 def patch hash @routes[:patch][build_regex(hash.keys.first.to_s)] = hash[hash.keys.first.to_s] end
post(hash)
click to toggle source
# File lib/actionframework/routes.rb, line 32 def post hash @routes[:post][build_regex(hash.keys.first.to_s)] = hash[hash.keys.first.to_s] end
realtime(hash)
click to toggle source
# File lib/actionframework/routes.rb, line 53 def realtime hash @routes[:realtime][build_regex(hash.keys.first.to_s)] = hash[hash.keys.first.to_s] end
redirect(hash)
click to toggle source
# File lib/actionframework/routes.rb, line 67 def redirect(hash) hash[:from] = build_regex(hash[:from]) @redirects << hash end
redirect?(req)
click to toggle source
# File lib/actionframework/routes.rb, line 72 def redirect? (req) @redirects.each do |redirect| if(matched = req.path.match(redirect[:from])) begin matched.names.each do |key| redirect[:to] = redirect[:to].gsub("{{#{key}}}",matched[key]) end rescue Exception => e end return OpenStruct.new(redirect) end end nil end
route(path,method)
click to toggle source
# File lib/actionframework/routes.rb, line 57 def route(path,method) @routes[method.downcase.to_sym].each do |regex,controller| if(matched = path.match(regex)) return [controller,matched] end end return nil end
update(hash)
click to toggle source
# File lib/actionframework/routes.rb, line 36 def update hash @routes[:update][build_regex(hash.keys.first.to_s)] = hash[hash.keys.first.to_s] end