trilby 0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/trilby.rb +82 -0
- metadata +77 -0
data/lib/trilby.rb
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'sinatra/base'
|
3
|
+
|
4
|
+
class Trilby < Sinatra::Base
|
5
|
+
|
6
|
+
class << self
|
7
|
+
alias :super_get :get
|
8
|
+
alias :super_put :put
|
9
|
+
alias :super_post :post
|
10
|
+
alias :super_delete :delete
|
11
|
+
end
|
12
|
+
|
13
|
+
|
14
|
+
@@routes = {}
|
15
|
+
|
16
|
+
def self.list_routes
|
17
|
+
@@routes
|
18
|
+
end
|
19
|
+
#
|
20
|
+
# Given a controller and methods (and optionally arguments) this generates
|
21
|
+
# a the route to get there. If there are named params, its tries to fill them
|
22
|
+
# in with the args passed. Failing that, it uses params already in the URL (so
|
23
|
+
# that on if the current request is /posts/:post_id (/posts/32), and you call
|
24
|
+
# url_for on a routes that is /posts/:post_id/comments, params[:post_id] is
|
25
|
+
# automagically filled in with 32. If you want to link to a different posts
|
26
|
+
# comment, simply pass :post_id in the args parameter.
|
27
|
+
#
|
28
|
+
def url_for controller, method_name, args={}
|
29
|
+
route = "#{controller}.#{method_name}"
|
30
|
+
route += ".#{args[:format]}" if args[:format]
|
31
|
+
|
32
|
+
path = @@routes[route]
|
33
|
+
|
34
|
+
raise "Could not find route for #{controller}.#{method_name}" unless path
|
35
|
+
path.gsub!(/:([a-z_])*/) { |p| args[p[1..-1].to_sym] || p }
|
36
|
+
path.gsub!(/:([a-z_])*/) { |p| params[p[1..-1]] || p}
|
37
|
+
path
|
38
|
+
end
|
39
|
+
|
40
|
+
def format
|
41
|
+
# TODO: Make this not lame
|
42
|
+
(params[:captures]) ? params[:captures][0] : nil
|
43
|
+
end
|
44
|
+
|
45
|
+
|
46
|
+
def self.modify_path http_method, path, controller, method_name, args
|
47
|
+
|
48
|
+
|
49
|
+
# Add the pretty route to the @@routes hash
|
50
|
+
if path.is_a? String
|
51
|
+
@@routes["#{controller}.#{method_name}"] = path
|
52
|
+
path += "/?"
|
53
|
+
end
|
54
|
+
|
55
|
+
if args[:formats]
|
56
|
+
args[:formats].each do |format|
|
57
|
+
@@routes["#{controller}.#{method_name}.#{format}"] = "#{path}.#{format}"
|
58
|
+
self.send(http_method, %r{#{path}.(#{format})}, controller, method_name, {})
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
path
|
63
|
+
end
|
64
|
+
|
65
|
+
|
66
|
+
def self.get path, controller, method_name, args={}
|
67
|
+
path = modify_path :get, path, controller, method_name, args
|
68
|
+
instance_eval("super_get(path) { controller.new(self).#{method_name} }")
|
69
|
+
end
|
70
|
+
def self.put path, controller, method_name, args={}
|
71
|
+
path = modify_path :put, path, controller, method_name, args
|
72
|
+
instance_eval("super_put(path) { controller.new(self).#{method_name} }")
|
73
|
+
end
|
74
|
+
def self.post path, controller, method_name, args={}
|
75
|
+
path = modify_path :post, path, controller, method_name, args
|
76
|
+
instance_eval("super_post(path) { controller.new(self).#{method_name} }")
|
77
|
+
end
|
78
|
+
def self.delete path, controller, method_name, args={}
|
79
|
+
path = modify_path :delete, path, controller, method_name, args
|
80
|
+
instance_eval("super_delete(path) { controller.new(self).#{method_name} }")
|
81
|
+
end
|
82
|
+
end
|
metadata
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: trilby
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 9
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
version: "0.1"
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Kaleb Pomeroy
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2013-03-04 00:00:00 Z
|
18
|
+
dependencies:
|
19
|
+
- !ruby/object:Gem::Dependency
|
20
|
+
name: sinatra
|
21
|
+
prerelease: false
|
22
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
23
|
+
none: false
|
24
|
+
requirements:
|
25
|
+
- - ">="
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
hash: 3
|
28
|
+
segments:
|
29
|
+
- 0
|
30
|
+
version: "0"
|
31
|
+
type: :runtime
|
32
|
+
version_requirements: *id001
|
33
|
+
description: Because its on top of sinatra
|
34
|
+
email: trilby@pomeroytx.com
|
35
|
+
executables: []
|
36
|
+
|
37
|
+
extensions: []
|
38
|
+
|
39
|
+
extra_rdoc_files: []
|
40
|
+
|
41
|
+
files:
|
42
|
+
- lib/trilby.rb
|
43
|
+
homepage: https://github.com/KalebPomeroy/trilby
|
44
|
+
licenses: []
|
45
|
+
|
46
|
+
post_install_message:
|
47
|
+
rdoc_options: []
|
48
|
+
|
49
|
+
require_paths:
|
50
|
+
- lib
|
51
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
52
|
+
none: false
|
53
|
+
requirements:
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
hash: 3
|
57
|
+
segments:
|
58
|
+
- 0
|
59
|
+
version: "0"
|
60
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
61
|
+
none: false
|
62
|
+
requirements:
|
63
|
+
- - ">="
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
hash: 3
|
66
|
+
segments:
|
67
|
+
- 0
|
68
|
+
version: "0"
|
69
|
+
requirements: []
|
70
|
+
|
71
|
+
rubyforge_project:
|
72
|
+
rubygems_version: 1.8.24
|
73
|
+
signing_key:
|
74
|
+
specification_version: 3
|
75
|
+
summary: Trilby
|
76
|
+
test_files: []
|
77
|
+
|