Class: Deas::Router
- Inherits:
-
Object
show all
- Defined in:
- lib/deas/router.rb
Defined Under Namespace
Classes: HandlerProxies, RequestType
Constant Summary
collapse
- DEFAULT_REQUEST_TYPE_NAME =
'default'.freeze
- ALLOW_TRAILING_SLASHES =
'allow'.freeze
- REMOVE_TRAILING_SLASHES =
'remove'.freeze
- SLASH =
'/'.freeze
- VALID_TRAILING_SLASHES_VALUES =
[
ALLOW_TRAILING_SLASHES,
REMOVE_TRAILING_SLASHES
].freeze
Instance Attribute Summary collapse
Instance Method Summary
collapse
-
#add_request_type(name, &proc) ⇒ Object
-
#allow_trailing_slashes ⇒ Object
-
#allow_trailing_slashes_set? ⇒ Boolean
-
#apply_definitions! ⇒ Object
-
#base_url(value = nil) ⇒ Object
-
#default_request_type_name(value = nil) ⇒ Object
-
#delete(path, *args) ⇒ Object
-
#escape_query_value(&block) ⇒ Object
-
#get(path, *args) ⇒ Object
-
#initialize(&block) ⇒ Router
constructor
A new instance of Router.
-
#not_found(from_path, body = nil) ⇒ Object
-
#patch(path, *args) ⇒ Object
-
#post(path, *args) ⇒ Object
-
#prepend_base_url(url_path) ⇒ Object
-
#put(path, *args) ⇒ Object
-
#redirect(from_path, to_path = nil, &block) ⇒ Object
-
#remove_trailing_slashes ⇒ Object
-
#remove_trailing_slashes_set? ⇒ Boolean
-
#request_type_name(request) ⇒ Object
ideally someday the request should just know its request type.
-
#route(http_method, from_path, *args) ⇒ Object
-
#set_base_url(value) ⇒ Object
-
#trailing_slashes_set? ⇒ Boolean
-
#url(name, path, options = nil) ⇒ Object
-
#url_for(name, *args) ⇒ Object
-
#validate! ⇒ Object
-
#validate_trailing_slashes! ⇒ Object
-
#view_handler_ns(value = nil) ⇒ Object
Constructor Details
#initialize(&block) ⇒ Router
Returns a new instance of Router.
22
23
24
25
26
27
28
|
# File 'lib/deas/router.rb', line 22
def initialize(&block)
@request_types = []
@urls, @routes, @definitions = {}, [], []
default_request_type_name(DEFAULT_REQUEST_TYPE_NAME)
escape_query_value{ |v| Rack::Utils.escape(v) }
self.instance_eval(&block) if !block.nil?
end
|
Instance Attribute Details
#definitions ⇒ Object
Returns the value of attribute definitions.
19
20
21
|
# File 'lib/deas/router.rb', line 19
def definitions
@definitions
end
|
#escape_query_value_proc ⇒ Object
Returns the value of attribute escape_query_value_proc.
20
21
22
|
# File 'lib/deas/router.rb', line 20
def escape_query_value_proc
@escape_query_value_proc
end
|
#request_types ⇒ Object
Returns the value of attribute request_types.
19
20
21
|
# File 'lib/deas/router.rb', line 19
def request_types
@request_types
end
|
#routes ⇒ Object
Returns the value of attribute routes.
19
20
21
|
# File 'lib/deas/router.rb', line 19
def routes
@routes
end
|
#trailing_slashes ⇒ Object
Returns the value of attribute trailing_slashes.
20
21
22
|
# File 'lib/deas/router.rb', line 20
def trailing_slashes
@trailing_slashes
end
|
#urls ⇒ Object
Returns the value of attribute urls.
19
20
21
|
# File 'lib/deas/router.rb', line 19
def urls
@urls
end
|
Instance Method Details
#add_request_type(name, &proc) ⇒ Object
102
103
104
|
# File 'lib/deas/router.rb', line 102
def add_request_type(name, &proc)
@request_types << RequestType.new(name, proc)
end
|
#allow_trailing_slashes ⇒ Object
35
36
37
|
# File 'lib/deas/router.rb', line 35
def allow_trailing_slashes
@trailing_slashes = ALLOW_TRAILING_SLASHES
end
|
#allow_trailing_slashes_set? ⇒ Boolean
39
40
41
|
# File 'lib/deas/router.rb', line 39
def allow_trailing_slashes_set?
@trailing_slashes == ALLOW_TRAILING_SLASHES
end
|
#apply_definitions! ⇒ Object
132
133
134
135
136
137
138
|
# File 'lib/deas/router.rb', line 132
def apply_definitions!
self.definitions.each do |(type, args, block)|
self.send("apply_#{type}", *args, &block)
end
self.definitions.clear
true
end
|
#base_url(value = nil) ⇒ Object
60
61
62
63
|
# File 'lib/deas/router.rb', line 60
def base_url(value = nil)
set_base_url(value) if !value.nil?
@base_url
end
|
#default_request_type_name(value = nil) ⇒ Object
97
98
99
100
|
# File 'lib/deas/router.rb', line 97
def default_request_type_name(value = nil)
@default_request_type = RequestType.new(value) if !value.nil?
@default_request_type.name
end
|
#delete(path, *args) ⇒ Object
115
|
# File 'lib/deas/router.rb', line 115
def delete(path, *args); self.route(:delete, path, *args); end
|
#escape_query_value(&block) ⇒ Object
55
56
57
58
|
# File 'lib/deas/router.rb', line 55
def escape_query_value(&block)
raise(ArgumentError, "no block given") unless block
@escape_query_value_proc = block
end
|
#get(path, *args) ⇒ Object
111
|
# File 'lib/deas/router.rb', line 111
def get(path, *args); self.route(:get, path, *args); end
|
#not_found(from_path, body = nil) ⇒ Object
127
128
129
130
|
# File 'lib/deas/router.rb', line 127
def not_found(from_path, body = nil)
respond_with_args = [404, {}, body || 'Not Found']
self.definitions.push([:respond_with, [from_path, respond_with_args], nil])
end
|
#patch(path, *args) ⇒ Object
114
|
# File 'lib/deas/router.rb', line 114
def patch(path, *args); self.route(:patch, path, *args); end
|
#post(path, *args) ⇒ Object
112
|
# File 'lib/deas/router.rb', line 112
def post(path, *args); self.route(:post, path, *args); end
|
#prepend_base_url(url_path) ⇒ Object
69
70
71
|
# File 'lib/deas/router.rb', line 69
def prepend_base_url(url_path)
"#{base_url}#{url_path}"
end
|
#put(path, *args) ⇒ Object
113
|
# File 'lib/deas/router.rb', line 113
def put(path, *args); self.route(:put, path, *args); end
|
#redirect(from_path, to_path = nil, &block) ⇒ Object
122
123
124
125
|
# File 'lib/deas/router.rb', line 122
def redirect(from_path, to_path = nil, &block)
self.definitions.push([:redirect, [from_path, to_path], block])
true
end
|
#remove_trailing_slashes ⇒ Object
43
44
45
|
# File 'lib/deas/router.rb', line 43
def remove_trailing_slashes
@trailing_slashes = REMOVE_TRAILING_SLASHES
end
|
#remove_trailing_slashes_set? ⇒ Boolean
47
48
49
|
# File 'lib/deas/router.rb', line 47
def remove_trailing_slashes_set?
@trailing_slashes == REMOVE_TRAILING_SLASHES
end
|
#request_type_name(request) ⇒ Object
ideally someday the request should just know its request type
107
108
109
|
# File 'lib/deas/router.rb', line 107
def request_type_name(request)
(self.request_types.detect{ |rt| rt.proc.call(request) } || @default_request_type).name
end
|
#route(http_method, from_path, *args) ⇒ Object
117
118
119
120
|
# File 'lib/deas/router.rb', line 117
def route(http_method, from_path, *args)
self.definitions.push([:route, [http_method, from_path, *args], nil])
true
end
|
#set_base_url(value) ⇒ Object
65
66
67
|
# File 'lib/deas/router.rb', line 65
def set_base_url(value)
@base_url = value
end
|
#trailing_slashes_set? ⇒ Boolean
#url(name, path, options = nil) ⇒ Object
73
74
75
76
77
78
79
80
81
82
83
|
# File 'lib/deas/router.rb', line 73
def url(name, path, options = nil)
if !name.kind_of?(::Symbol)
raise ArgumentError, "invalid name `#{name.inspect}` - "\
"named urls must be defined with Symbol names"
end
if !path.kind_of?(::String)
raise ArgumentError, "invalid path `#{path.inspect}` - "\
"named urls must be defined with String paths"
end
add_url(name, path, options || {})
end
|
#url_for(name, *args) ⇒ Object
85
86
87
88
89
90
91
92
93
94
95
|
# File 'lib/deas/router.rb', line 85
def url_for(name, *args)
url = self.urls[name.to_sym]
raise ArgumentError, "no route named `#{name.to_sym.inspect}`" unless url
begin
prepend_base_url(url.path_for(*args))
rescue Deas::Url::NonHashParamsError => err
raise ArgumentError, "url param values must be passed as a Hash"
rescue Deas::Url::EmptyNamedValueError => err
raise ArgumentError, err.message
end
end
|
#validate! ⇒ Object
156
157
158
159
160
161
|
# File 'lib/deas/router.rb', line 156
def validate!
self.apply_definitions!
self.routes.each(&:validate!)
self.validate_trailing_slashes!
true
end
|
#validate_trailing_slashes! ⇒ Object
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
|
# File 'lib/deas/router.rb', line 140
def validate_trailing_slashes!
if self.trailing_slashes == REMOVE_TRAILING_SLASHES
paths = []
all_missing = self.routes.inject(true) do |result, route|
paths << route.path if route.path[-1..-1] == SLASH
result && route.path[-1..-1] != SLASH
end
if !all_missing
msg = "all route paths must *not* end with a \"/\", but these do:\n"\
"#{paths.join("\n")}"
raise TrailingSlashesError, msg
end
end
true
end
|
#view_handler_ns(value = nil) ⇒ Object
30
31
32
33
|
# File 'lib/deas/router.rb', line 30
def view_handler_ns(value = nil)
@view_handler_ns = value if !value.nil?
@view_handler_ns
end
|