Class: Trilby
- Inherits:
-
Sinatra::Base
- Object
- Sinatra::Base
- Trilby
- Defined in:
- lib/trilby.rb
Constant Summary collapse
- @@routes =
{}
Class Method Summary collapse
- .before(path, controller, method_name, args = {}) ⇒ Object
- .delete(path, controller, method_name, args = {}) ⇒ Object
- .get(path, controller, method_name, args = {}) ⇒ Object
- .list_routes ⇒ Object
- .modify_path(http_method, path, controller, method_name, args) ⇒ Object
- .post(path, controller, method_name, args = {}) ⇒ Object
- .put(path, controller, method_name, args = {}) ⇒ Object
- .super_before ⇒ Object
- .super_delete ⇒ Object
- .super_get ⇒ Object
- .super_post ⇒ Object
- .super_put ⇒ Object
Instance Method Summary collapse
- #format ⇒ Object
-
#url_for(controller, method_name, args = {}) ⇒ Object
Given a controller and methods (and optionally arguments) this generates a the route to get there.
Class Method Details
.before(path, controller, method_name, args = {}) ⇒ Object
100 101 102 103 |
# File 'lib/trilby.rb', line 100 def self.before path, controller, method_name, args={} path = modify_path :before, path, controller, method_name, args instance_eval("super_before(path) {controller.new(self).#{method_name} }") end |
.delete(path, controller, method_name, args = {}) ⇒ Object
92 93 94 95 96 97 98 99 |
# File 'lib/trilby.rb', line 92 def self.delete path, controller, method_name, args={} path = modify_path :delete, path, controller, method_name, args instance_eval("super_delete(path) do c = controller.new(self) c.before_filter unless args[:skip_filter] c.#{method_name} end") end |
.get(path, controller, method_name, args = {}) ⇒ Object
68 69 70 71 72 73 74 75 |
# File 'lib/trilby.rb', line 68 def self.get path, controller, method_name, args={} path = modify_path :get, path, controller, method_name, args instance_eval("super_get(path) do c = controller.new(self) c.before_filter unless args[:skip_filter] c.#{method_name} end") end |
.list_routes ⇒ Object
18 19 20 |
# File 'lib/trilby.rb', line 18 def self.list_routes @@routes end |
.modify_path(http_method, path, controller, method_name, args) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/trilby.rb', line 48 def self.modify_path http_method, path, controller, method_name, args return unless path # Add the pretty route to the @@routes hash if path.is_a? String @@routes["#{controller}.#{method_name}"] = path path += "/?" end if args[:formats] args[:formats].each do |format| @@routes["#{controller}.#{method_name}.#{format}"] = "#{path}.#{format}" self.send(http_method, %r{#{path}.(#{format})}, controller, method_name, {}) end end path end |
.post(path, controller, method_name, args = {}) ⇒ Object
84 85 86 87 88 89 90 91 |
# File 'lib/trilby.rb', line 84 def self.post path, controller, method_name, args={} path = modify_path :post, path, controller, method_name, args instance_eval("super_post(path)do c = controller.new(self) c.before_filter unless args[:skip_filter] c.#{method_name} end") end |
.put(path, controller, method_name, args = {}) ⇒ Object
76 77 78 79 80 81 82 83 |
# File 'lib/trilby.rb', line 76 def self.put path, controller, method_name, args={} path = modify_path :put, path, controller, method_name, args instance_eval("super_put(path) do c = controller.new(self) c.before_filter unless args[:skip_filter] c.#{method_name} end") end |
.super_before ⇒ Object
12 |
# File 'lib/trilby.rb', line 12 alias :super_before :before |
.super_delete ⇒ Object
11 |
# File 'lib/trilby.rb', line 11 alias :super_delete :delete |
.super_get ⇒ Object
8 |
# File 'lib/trilby.rb', line 8 alias :super_get :get |
.super_post ⇒ Object
10 |
# File 'lib/trilby.rb', line 10 alias :super_post :post |
.super_put ⇒ Object
9 |
# File 'lib/trilby.rb', line 9 alias :super_put :put |
Instance Method Details
#format ⇒ Object
42 43 44 45 |
# File 'lib/trilby.rb', line 42 def format # TODO: Make this not lame (params[:captures]) ? params[:captures][0] : nil end |
#url_for(controller, method_name, args = {}) ⇒ Object
Given a controller and methods (and optionally arguments) this generates a the route to get there. If there are named params, its tries to fill them in with the args passed. Failing that, it uses params already in the URL (so that on if the current request is /posts/:post_id (/posts/32), and you call url_for on a routes that is /posts/:post_id/comments, params is automagically filled in with 32. If you want to link to a different posts comment, simply pass :post_id in the args parameter.
30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/trilby.rb', line 30 def url_for controller, method_name, args={} route = "#{controller}.#{method_name}" route += ".#{args[:format]}" if args[:format] path = @@routes[route].clone raise "Could not find route for #{controller}.#{method_name}" unless path path.gsub!(/:([a-z_])*/) { |p| args[p[1..-1].to_sym] || p } path.gsub!(/:([a-z_])*/) { |p| params[p[1..-1]] || p} path end |