app-routes 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/app-routes.rb +58 -0
- metadata +55 -0
data/lib/app-routes.rb
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
#!/usr/bin/ruby
|
2
|
+
|
3
|
+
# file: app-routes.rb
|
4
|
+
|
5
|
+
class AppRoutes
|
6
|
+
|
7
|
+
def initialize(params={})
|
8
|
+
super()
|
9
|
+
@route = {}
|
10
|
+
@params = params
|
11
|
+
end
|
12
|
+
|
13
|
+
def routes()
|
14
|
+
yield
|
15
|
+
end
|
16
|
+
|
17
|
+
def run(request)
|
18
|
+
|
19
|
+
result = nil
|
20
|
+
|
21
|
+
@route.each do |key, block|
|
22
|
+
match = request.match(key)
|
23
|
+
|
24
|
+
if match then
|
25
|
+
|
26
|
+
args = match.captures
|
27
|
+
@params[:captures] = *args
|
28
|
+
|
29
|
+
if @route[key][:s] then
|
30
|
+
raw_params = @route[key][:s].gsub(':','').match(key).captures
|
31
|
+
@params.merge!(Hash[raw_params.map(&:to_sym).zip(args)])
|
32
|
+
end
|
33
|
+
|
34
|
+
result = @route[key][:block].call *args
|
35
|
+
|
36
|
+
break
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
result
|
41
|
+
end
|
42
|
+
|
43
|
+
def get(arg,&block)
|
44
|
+
methodx = {String: :string_get, Regexp: :regex_get}
|
45
|
+
send (methodx[arg.class.to_s.to_sym]), arg, &block
|
46
|
+
end
|
47
|
+
|
48
|
+
def string_get(raw_s, &block)
|
49
|
+
s = raw_s.gsub(/\:\w+/,'(\w+)')
|
50
|
+
@route[Regexp.new s] = {s: raw_s, block: block}
|
51
|
+
end
|
52
|
+
|
53
|
+
def regex_get(r, &block)
|
54
|
+
@route[r] = {block: block}
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
58
|
+
|
metadata
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: app-routes
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors: []
|
7
|
+
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2010-07-06 00:00:00 +01:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description:
|
17
|
+
email:
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files: []
|
23
|
+
|
24
|
+
files:
|
25
|
+
- lib/app-routes.rb
|
26
|
+
has_rdoc: true
|
27
|
+
homepage:
|
28
|
+
licenses: []
|
29
|
+
|
30
|
+
post_install_message:
|
31
|
+
rdoc_options: []
|
32
|
+
|
33
|
+
require_paths:
|
34
|
+
- lib
|
35
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - ">="
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: "0"
|
40
|
+
version:
|
41
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
42
|
+
requirements:
|
43
|
+
- - ">="
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: "0"
|
46
|
+
version:
|
47
|
+
requirements: []
|
48
|
+
|
49
|
+
rubyforge_project:
|
50
|
+
rubygems_version: 1.3.5
|
51
|
+
signing_key:
|
52
|
+
specification_version: 3
|
53
|
+
summary: app-routes
|
54
|
+
test_files: []
|
55
|
+
|