Class: ActionFramework::Routes
- Inherits:
-
Object
- Object
- ActionFramework::Routes
- Defined in:
- lib/actionframework/routes.rb
Instance Attribute Summary collapse
-
#models ⇒ Object
Returns the value of attribute models.
-
#redirects ⇒ Object
Returns the value of attribute redirects.
-
#routes ⇒ Object
Returns the value of attribute routes.
Instance Method Summary collapse
- #build_regex(string) ⇒ Object
- #delete(hash) ⇒ Object
- #get(hash) ⇒ Object
-
#initialize ⇒ Routes
constructor
A new instance of Routes.
- #model(name) ⇒ Object
- #patch(hash) ⇒ Object
- #post(hash) ⇒ Object
- #redirect(hash) ⇒ Object
- #redirect?(req) ⇒ Boolean
- #route(path, method) ⇒ Object
- #update(hash) ⇒ Object
Constructor Details
#initialize ⇒ Routes
Returns a new instance of Routes.
13 14 15 16 17 |
# File 'lib/actionframework/routes.rb', line 13 def initialize @routes = {:get => {}, :post => {}, :update => {}, :delete => {}, :patch => {}} @models = [] @redirects = [] end |
Instance Attribute Details
#models ⇒ Object
Returns the value of attribute models.
10 11 12 |
# File 'lib/actionframework/routes.rb', line 10 def models @models end |
#redirects ⇒ Object
Returns the value of attribute redirects.
11 12 13 |
# File 'lib/actionframework/routes.rb', line 11 def redirects @redirects end |
#routes ⇒ Object
Returns the value of attribute routes.
9 10 11 |
# File 'lib/actionframework/routes.rb', line 9 def routes @routes end |
Instance Method Details
#build_regex(string) ⇒ Object
19 20 21 22 23 24 25 26 |
# 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) ⇒ Object
40 41 42 |
# 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) ⇒ Object
28 29 30 |
# 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) ⇒ Object
48 49 50 51 |
# 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) ⇒ Object
44 45 46 |
# 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) ⇒ Object
32 33 34 |
# 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 |
#redirect(hash) ⇒ Object
64 65 66 67 |
# File 'lib/actionframework/routes.rb', line 64 def redirect(hash) hash[:from] = build_regex(hash[:from]) @redirects << hash end |
#redirect?(req) ⇒ Boolean
69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/actionframework/routes.rb', line 69 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) ⇒ Object
53 54 55 56 57 58 59 60 61 62 |
# File 'lib/actionframework/routes.rb', line 53 def route(path,method) @routes[method.downcase.to_sym].each do |regex,controller| puts regex.inspect if(matched = path.match(regex)) return [controller,matched] end end return nil end |
#update(hash) ⇒ Object
36 37 38 |
# 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 |