sinatra-namespace 0.6.1 → 1.0
Sign up to get free protection for your applications and to get access to all the features.
- metadata +31 -94
- data/LICENSE +0 -27
- data/README.md +0 -166
- data/lib/sinatra/namespace.rb +0 -157
- data/sinatra-namespace.gemspec +0 -20
- data/spec/sinatra/namespace_spec.rb +0 -300
- data/spec/spec_helper.rb +0 -3
metadata
CHANGED
@@ -1,118 +1,55 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: sinatra-namespace
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
5
|
-
prerelease:
|
6
|
-
segments:
|
7
|
-
- 0
|
8
|
-
- 6
|
9
|
-
- 1
|
10
|
-
version: 0.6.1
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: '1.0'
|
5
|
+
prerelease:
|
11
6
|
platform: ruby
|
12
|
-
authors:
|
7
|
+
authors:
|
13
8
|
- Konstantin Haase
|
14
9
|
autorequire:
|
15
10
|
bindir: bin
|
16
11
|
cert_chain: []
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
name: sinatra
|
23
|
-
prerelease: false
|
24
|
-
requirement: &id001 !ruby/object:Gem::Requirement
|
12
|
+
date: 2011-10-28 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: sinatra-contrib
|
16
|
+
requirement: &2156732360 !ruby/object:Gem::Requirement
|
25
17
|
none: false
|
26
|
-
requirements:
|
27
|
-
- -
|
28
|
-
- !ruby/object:Gem::Version
|
29
|
-
|
30
|
-
segments:
|
31
|
-
- 1
|
32
|
-
- 1
|
33
|
-
version: "1.1"
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
34
22
|
type: :runtime
|
35
|
-
version_requirements: *id001
|
36
|
-
- !ruby/object:Gem::Dependency
|
37
|
-
name: sinatra-test-helper
|
38
23
|
prerelease: false
|
39
|
-
|
40
|
-
|
41
|
-
requirements:
|
42
|
-
- - ~>
|
43
|
-
- !ruby/object:Gem::Version
|
44
|
-
hash: 11
|
45
|
-
segments:
|
46
|
-
- 0
|
47
|
-
- 5
|
48
|
-
- 0
|
49
|
-
version: 0.5.0
|
50
|
-
type: :development
|
51
|
-
version_requirements: *id002
|
52
|
-
- !ruby/object:Gem::Dependency
|
53
|
-
name: rspec
|
54
|
-
prerelease: false
|
55
|
-
requirement: &id003 !ruby/object:Gem::Requirement
|
56
|
-
none: false
|
57
|
-
requirements:
|
58
|
-
- - ~>
|
59
|
-
- !ruby/object:Gem::Version
|
60
|
-
hash: 27
|
61
|
-
segments:
|
62
|
-
- 1
|
63
|
-
- 3
|
64
|
-
- 0
|
65
|
-
version: 1.3.0
|
66
|
-
type: :development
|
67
|
-
version_requirements: *id003
|
68
|
-
description: Adds namespaces to Sinatra, allows namespaces to have local helpers.
|
24
|
+
version_requirements: *2156732360
|
25
|
+
description: ! 'Dummy gem, sinatra-namespace is now part of sinatra-contrib: http://www.sinatrarb.com/contrib/'
|
69
26
|
email: konstantin.mailinglists@googlemail.com
|
70
27
|
executables: []
|
71
|
-
|
72
28
|
extensions: []
|
73
|
-
|
74
29
|
extra_rdoc_files: []
|
75
|
-
|
76
|
-
|
77
|
-
- LICENSE
|
78
|
-
- README.md
|
79
|
-
- lib/sinatra/namespace.rb
|
80
|
-
- sinatra-namespace.gemspec
|
81
|
-
- spec/sinatra/namespace_spec.rb
|
82
|
-
- spec/spec_helper.rb
|
83
|
-
has_rdoc: yard
|
84
|
-
homepage: http://github.com/rkh/sinatra-namespace
|
30
|
+
files: []
|
31
|
+
homepage: http://github.com/sinatra/sinatra-contrib
|
85
32
|
licenses: []
|
86
|
-
|
87
33
|
post_install_message:
|
88
34
|
rdoc_options: []
|
89
|
-
|
90
|
-
require_paths:
|
35
|
+
require_paths:
|
91
36
|
- lib
|
92
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
37
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
93
38
|
none: false
|
94
|
-
requirements:
|
95
|
-
- -
|
96
|
-
- !ruby/object:Gem::Version
|
97
|
-
|
98
|
-
|
99
|
-
- 0
|
100
|
-
version: "0"
|
101
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
39
|
+
requirements:
|
40
|
+
- - ! '>='
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: '0'
|
43
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
102
44
|
none: false
|
103
|
-
requirements:
|
104
|
-
- -
|
105
|
-
- !ruby/object:Gem::Version
|
106
|
-
|
107
|
-
segments:
|
108
|
-
- 0
|
109
|
-
version: "0"
|
45
|
+
requirements:
|
46
|
+
- - ! '>='
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '0'
|
110
49
|
requirements: []
|
111
|
-
|
112
50
|
rubyforge_project:
|
113
|
-
rubygems_version: 1.
|
51
|
+
rubygems_version: 1.8.10
|
114
52
|
signing_key:
|
115
53
|
specification_version: 3
|
116
|
-
summary:
|
54
|
+
summary: Dummy gem, sinatra-namespace is now part of sinatra-contrib.
|
117
55
|
test_files: []
|
118
|
-
|
data/LICENSE
DELETED
@@ -1,27 +0,0 @@
|
|
1
|
-
copyright (c) 2010 Konstantin Haase. All rights reserved.
|
2
|
-
|
3
|
-
Developed by: Konstantin Haase
|
4
|
-
http://github.com/rkh/big_band
|
5
|
-
|
6
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
7
|
-
of this software and associated documentation files (the "Software"), to
|
8
|
-
deal with the Software without restriction, including without limitation the
|
9
|
-
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
10
|
-
sell copies of the Software, and to permit persons to whom the Software is
|
11
|
-
furnished to do so, subject to the following conditions:
|
12
|
-
1. Redistributions of source code must retain the above copyright notice,
|
13
|
-
this list of conditions and the following disclaimers.
|
14
|
-
2. Redistributions in binary form must reproduce the above copyright
|
15
|
-
notice, this list of conditions and the following disclaimers in the
|
16
|
-
documentation and/or other materials provided with the distribution.
|
17
|
-
3. Neither the name of Konstantin Haase, nor the names of other contributors
|
18
|
-
may be used to endorse or promote products derived from this Software without
|
19
|
-
specific prior written permission.
|
20
|
-
|
21
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
22
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
23
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
24
|
-
CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
25
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
26
|
-
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
27
|
-
WITH THE SOFTWARE.
|
data/README.md
DELETED
@@ -1,166 +0,0 @@
|
|
1
|
-
# Sinatra::Namespace
|
2
|
-
|
3
|
-
Adds namespaces to [Sinatra](http://sinatrarb.com). Allows namespaces to have local helpers.
|
4
|
-
|
5
|
-
## Features
|
6
|
-
|
7
|
-
### Nesting by prefix
|
8
|
-
|
9
|
-
require "sinatra"
|
10
|
-
require "sinatra/namespace"
|
11
|
-
|
12
|
-
namespace '/blog' do
|
13
|
-
get { haml :index }
|
14
|
-
|
15
|
-
get '/:entry_id' do |id|
|
16
|
-
@entry = Entry.find(id)
|
17
|
-
haml :entry
|
18
|
-
end
|
19
|
-
end
|
20
|
-
|
21
|
-
### Nesting by condition
|
22
|
-
|
23
|
-
require "sinatra"
|
24
|
-
require "sinatra/namespace"
|
25
|
-
|
26
|
-
namespace :host_name => "www.example.com" do
|
27
|
-
# ...
|
28
|
-
end
|
29
|
-
|
30
|
-
namespace :host_name => "api.example.com" do
|
31
|
-
# ...
|
32
|
-
end
|
33
|
-
|
34
|
-
### Local helpers, filters and error handling
|
35
|
-
|
36
|
-
require "sinatra"
|
37
|
-
require "sinatra/namespace"
|
38
|
-
|
39
|
-
helpers do
|
40
|
-
def title
|
41
|
-
"foo bar"
|
42
|
-
end
|
43
|
-
|
44
|
-
def posts
|
45
|
-
Post.all
|
46
|
-
end
|
47
|
-
end
|
48
|
-
|
49
|
-
get "/" do
|
50
|
-
haml :index
|
51
|
-
end
|
52
|
-
|
53
|
-
namspace "/ruby" do
|
54
|
-
before { @an_important_note = "filters work, too" }
|
55
|
-
|
56
|
-
after do
|
57
|
-
@an_important_note = nil # don't tell
|
58
|
-
end
|
59
|
-
|
60
|
-
not_found do
|
61
|
-
"ruby does not know this ditty"
|
62
|
-
end
|
63
|
-
|
64
|
-
helpers do
|
65
|
-
def posts
|
66
|
-
super.where :topic => "ruby"
|
67
|
-
end
|
68
|
-
end
|
69
|
-
|
70
|
-
get { haml :index }
|
71
|
-
|
72
|
-
get "/new" do
|
73
|
-
haml :new, {}, :object => Post.new(:topic => "ruby")
|
74
|
-
end
|
75
|
-
end
|
76
|
-
|
77
|
-
### With modules
|
78
|
-
|
79
|
-
Modular style (you can of course use the `namespace` method there, too):
|
80
|
-
|
81
|
-
require "sinatra/base"
|
82
|
-
require "sinatra/namespace"
|
83
|
-
|
84
|
-
class Application < Sinatra::Base
|
85
|
-
def title
|
86
|
-
"foo bar"
|
87
|
-
end
|
88
|
-
|
89
|
-
def posts
|
90
|
-
Post.all
|
91
|
-
end
|
92
|
-
|
93
|
-
module RubyPosts
|
94
|
-
# If you would have called that module Ruby, you would not have to set
|
95
|
-
# your prefix by hand, ain't that cool, huh?
|
96
|
-
prefix "/ruby"
|
97
|
-
|
98
|
-
def posts
|
99
|
-
super.where :topic => "ruby"
|
100
|
-
end
|
101
|
-
|
102
|
-
get { haml :index }
|
103
|
-
|
104
|
-
get "/new" do
|
105
|
-
haml :new, {}, :object => Post.new(:topic => "ruby")
|
106
|
-
end
|
107
|
-
end
|
108
|
-
end
|
109
|
-
|
110
|
-
Wait, did that module turn into a namespace all by itself? No, actually it got turned into one by `Application` when it
|
111
|
-
tried to call `prefix`, which is not defined.
|
112
|
-
|
113
|
-
You can influence that behavior by setting `auto_namespace`:
|
114
|
-
|
115
|
-
class Application < Sinatra::Base
|
116
|
-
# enables auto namespacing, is default
|
117
|
-
enable :auto_namespace
|
118
|
-
|
119
|
-
# disables auto namespacing
|
120
|
-
disable :auto_namespace
|
121
|
-
|
122
|
-
# triggers auto namespaceing only on prefix and get
|
123
|
-
set :auto_namespace, :only => [:prefix, :get]
|
124
|
-
|
125
|
-
# triggers auto namespacing on all public methods of Sinatra::Namspace::NestedMethods except prefix
|
126
|
-
set :auto_namespace, :except => :prefix
|
127
|
-
end
|
128
|
-
|
129
|
-
So, how does one create a namespace from a module without that auto detection? Simple:
|
130
|
-
|
131
|
-
Application.make_namespace SomeModule, :prefix => "/somewhere"
|
132
|
-
|
133
|
-
|
134
|
-
Installation
|
135
|
-
------------
|
136
|
-
|
137
|
-
gem install sinatra-namespace
|
138
|
-
|
139
|
-
Alternatives
|
140
|
-
------------
|
141
|
-
|
142
|
-
Sinatra::Namespace is made for sharing some state/helpers.
|
143
|
-
If that is no what you are looking for, you have two alternative directions.
|
144
|
-
|
145
|
-
Simple prefixing, shares all state/helpers:
|
146
|
-
|
147
|
-
require "sinatra/base"
|
148
|
-
|
149
|
-
admin_prefix = "/this/is/the/admin/prefix"
|
150
|
-
get(admin_prefix) { haml :admin_index }
|
151
|
-
get("#{admin_prefix}/new_user") { haml :new_user }
|
152
|
-
get("#{admin_prefix}/admin_stuff") { haml :admin_stuff }
|
153
|
-
|
154
|
-
Middleware, shares no state/helpers:
|
155
|
-
|
156
|
-
require "sinatra/base"
|
157
|
-
|
158
|
-
class Application < Sinatra::Base
|
159
|
-
class AdminNamespace < Sinatra::Base
|
160
|
-
get("admin/prefix") { haml :admin_index }
|
161
|
-
get("admin/new_user") { haml :new_user }
|
162
|
-
get("admin/admin_stuff") { haml :admin_stuff }
|
163
|
-
end
|
164
|
-
|
165
|
-
use AdminNamespace
|
166
|
-
end
|
data/lib/sinatra/namespace.rb
DELETED
@@ -1,157 +0,0 @@
|
|
1
|
-
require 'sinatra/base'
|
2
|
-
|
3
|
-
module Sinatra
|
4
|
-
module Namespace
|
5
|
-
module NestedMethods
|
6
|
-
DONT_FORWARD = %w[call configure disable enable new register reset! run! set use template layout]
|
7
|
-
attr_reader :prefix, :options, :base
|
8
|
-
|
9
|
-
def get(name = nil, options = {}, &block) prefixed(:get, name, options, &block) end
|
10
|
-
def put(name = nil, options = {}, &block) prefixed(:put, name, options, &block) end
|
11
|
-
def post(name = nil, options = {}, &block) prefixed(:post, name, options, &block) end
|
12
|
-
def delete(name = nil, options = {}, &block) prefixed(:delete, name, options, &block) end
|
13
|
-
def head(name = nil, options = {}, &block) prefixed(:head, name, options, &block) end
|
14
|
-
def before(name = "*", &block) prefixed(:before, name, &block) end
|
15
|
-
def after(name = "*", &block) prefixed(:after, name, &block) end
|
16
|
-
|
17
|
-
def helpers(*list, &block)
|
18
|
-
include(*list) unless list.empty?
|
19
|
-
class_eval(&block) if block
|
20
|
-
end
|
21
|
-
|
22
|
-
def settings
|
23
|
-
return base if base.is_a? Class
|
24
|
-
base.settings
|
25
|
-
end
|
26
|
-
|
27
|
-
def respond_to?(*args)
|
28
|
-
return true if super
|
29
|
-
base.respond_to?(*args) and forward(args.first)
|
30
|
-
end
|
31
|
-
|
32
|
-
def errors
|
33
|
-
@errors ||= {}
|
34
|
-
end
|
35
|
-
|
36
|
-
def not_found(&block)
|
37
|
-
error(404, &block)
|
38
|
-
end
|
39
|
-
|
40
|
-
def error(codes = Exception, &block)
|
41
|
-
[*codes].each { |c| errors[c] = block }
|
42
|
-
end
|
43
|
-
|
44
|
-
private
|
45
|
-
|
46
|
-
def prefixed_path(name)
|
47
|
-
if prefix.is_a? Regexp or name.is_a? Regexp
|
48
|
-
path = /#{prefix}#{name}/
|
49
|
-
path = /^#{path}$/ if base.is_a? Class
|
50
|
-
path
|
51
|
-
else
|
52
|
-
prefix.to_s + name.to_s
|
53
|
-
end
|
54
|
-
end
|
55
|
-
|
56
|
-
def prefixed(method, name, *args, &block)
|
57
|
-
if name.respond_to? :key?
|
58
|
-
args.unshift name
|
59
|
-
name = nil
|
60
|
-
end
|
61
|
-
options.each { |o, a| settings.send(o, *a ) }
|
62
|
-
base.send(method, prefixed_path(name), *args, &block)
|
63
|
-
end
|
64
|
-
|
65
|
-
def forward?(name)
|
66
|
-
not DONT_FORWARD.include? name.to_s
|
67
|
-
end
|
68
|
-
|
69
|
-
def method_missing(name, *args, &block)
|
70
|
-
return super unless base.respond_to? name and forward? name
|
71
|
-
base.send(name, *args, &block)
|
72
|
-
end
|
73
|
-
end
|
74
|
-
|
75
|
-
module ClassMethods
|
76
|
-
def namespace(prefix = nil, options = {}, &block)
|
77
|
-
Namespace.setup(self, prefix, options, Module.new, &block)
|
78
|
-
end
|
79
|
-
|
80
|
-
def make_namespace(mod, options = {})
|
81
|
-
options[:base] ||= self
|
82
|
-
Namespace.make_namespace(mod, options)
|
83
|
-
end
|
84
|
-
|
85
|
-
def make_namespace?(klass, meth)
|
86
|
-
return false if !auto_namespace? or klass.is_a? NestedMethods
|
87
|
-
meths = NestedMethods.instance_methods.map { |m| m.to_s }
|
88
|
-
if auto_namespace != true
|
89
|
-
meths = [auto_namespace[:only]].flatten.map { |m| m.to_s } if auto_namespace.include? :only
|
90
|
-
[auto_namespace[:except]].flatten.each { |m| meths.delete m.to_s } if auto_namespace.include? :except
|
91
|
-
end
|
92
|
-
meths.include? meth.to_s
|
93
|
-
end
|
94
|
-
end
|
95
|
-
|
96
|
-
module ModularMethods
|
97
|
-
def setup(base, prefix = nil, options = {}, mixin = nil, &block)
|
98
|
-
prefix, options = nil, prefix if options.empty? and prefix.respond_to? :key?
|
99
|
-
prefix ||= ""
|
100
|
-
mixin ||= self
|
101
|
-
mixin.class_eval { @prefix, @options, @base = prefix, options, base }
|
102
|
-
mixin.extend ClassMethods, NestedMethods
|
103
|
-
mixin.send(:define_method, :error_block!) do |*keys|
|
104
|
-
if block = keys.inject(nil) { |b,k| b ||= mixin.errors[k] }
|
105
|
-
instance_eval(&block)
|
106
|
-
else
|
107
|
-
super(*keys)
|
108
|
-
end
|
109
|
-
end
|
110
|
-
mixin.before { extend mixin }
|
111
|
-
mixin.class_eval(&block) if block
|
112
|
-
mixin
|
113
|
-
end
|
114
|
-
end
|
115
|
-
|
116
|
-
module NamespaceDetector
|
117
|
-
Module.send(:include, self)
|
118
|
-
def method_missing(meth, *args, &block)
|
119
|
-
return super if is_a? Class or !name
|
120
|
-
base = Object
|
121
|
-
detected = name.split('::').any? do |name|
|
122
|
-
base = base.const_get(name)
|
123
|
-
base < Sinatra::Base
|
124
|
-
end
|
125
|
-
if detected and base.make_namespace?(self, meth)
|
126
|
-
Sinatra::Namespace.make_namespace self, :base => base
|
127
|
-
send(meth, *args, &block)
|
128
|
-
else
|
129
|
-
super
|
130
|
-
end
|
131
|
-
end
|
132
|
-
end
|
133
|
-
|
134
|
-
extend ModularMethods
|
135
|
-
|
136
|
-
def self.make_namespace(mod, options = {})
|
137
|
-
from = caller[0] =~ /make_namespace/ ? caller[1] : caller[0]
|
138
|
-
base = options.delete(:base) || options.delete(:for)
|
139
|
-
options[:prefix] ||= '/' << mod.name.gsub(/^#{base.name}::/, '').
|
140
|
-
gsub(/::/, '/').gsub(/([a-z\d]+)([A-Z][a-z])/,'\1_\2').downcase
|
141
|
-
setup base, options.delete(:prefix), options, mod
|
142
|
-
end
|
143
|
-
|
144
|
-
def self.included(klass)
|
145
|
-
klass.extend ModularMethods
|
146
|
-
super
|
147
|
-
end
|
148
|
-
|
149
|
-
def self.registered(klass)
|
150
|
-
klass.extend ClassMethods
|
151
|
-
klass.enable :auto_namespace
|
152
|
-
end
|
153
|
-
end
|
154
|
-
|
155
|
-
Delegator.delegate :namespace
|
156
|
-
register Namespace
|
157
|
-
end
|
data/sinatra-namespace.gemspec
DELETED
@@ -1,20 +0,0 @@
|
|
1
|
-
Gem::Specification.new do |s|
|
2
|
-
# Get the facts.
|
3
|
-
s.name = "sinatra-namespace"
|
4
|
-
s.version = "0.6.1"
|
5
|
-
s.description = "Adds namespaces to Sinatra, allows namespaces to have local helpers."
|
6
|
-
|
7
|
-
# Dependencies
|
8
|
-
s.add_dependency "sinatra", "~> 1.1"
|
9
|
-
s.add_development_dependency "sinatra-test-helper", "~> 0.5.0"
|
10
|
-
s.add_development_dependency "rspec", "~> 1.3.0"
|
11
|
-
|
12
|
-
# Those should be about the same in any BigBand extension.
|
13
|
-
s.authors = ["Konstantin Haase"]
|
14
|
-
s.email = "konstantin.mailinglists@googlemail.com"
|
15
|
-
s.files = `git ls-files`.split("\n")
|
16
|
-
s.has_rdoc = 'yard'
|
17
|
-
s.homepage = "http://github.com/rkh/#{s.name}"
|
18
|
-
s.require_paths = ["lib"]
|
19
|
-
s.summary = s.description
|
20
|
-
end
|
@@ -1,300 +0,0 @@
|
|
1
|
-
require File.expand_path("../../spec_helper", __FILE__)
|
2
|
-
|
3
|
-
describe Sinatra::Namespace do
|
4
|
-
it_should_behave_like 'sinatra'
|
5
|
-
|
6
|
-
it "should delegate namespace" do
|
7
|
-
Sinatra::Delegator.private_instance_methods.map(&:to_s).should include("namespace")
|
8
|
-
end
|
9
|
-
|
10
|
-
[:get, :head, :post, :put, :delete].each do |verb|
|
11
|
-
describe "HTTP #{verb.to_s.upcase}" do
|
12
|
-
before :each do
|
13
|
-
Object.send :remove_const, :App if Object.const_defined? :App
|
14
|
-
class ::App < Sinatra::Base
|
15
|
-
register Sinatra::Namespace
|
16
|
-
end
|
17
|
-
app App
|
18
|
-
end
|
19
|
-
|
20
|
-
describe :namespace do
|
21
|
-
it "should add routes including prefix to the base app" do
|
22
|
-
app.namespace "/foo" do
|
23
|
-
send(verb, "/bar") { "baz" }
|
24
|
-
end
|
25
|
-
browse_route(verb, "/foo/bar").should be_ok
|
26
|
-
browse_route(verb, "/foo/bar").body.should == "baz" unless verb == :head
|
27
|
-
end
|
28
|
-
|
29
|
-
it "should allows adding routes with no path" do
|
30
|
-
app.namespace "/foo" do
|
31
|
-
send(verb) { "bar" }
|
32
|
-
end
|
33
|
-
browse_route(verb, "/foo").should be_ok
|
34
|
-
browse_route(verb, "/foo").body.should == "bar" unless verb == :head
|
35
|
-
end
|
36
|
-
|
37
|
-
it "allows nesting" do
|
38
|
-
app.namespace "/foo" do
|
39
|
-
namespace "/bar" do
|
40
|
-
namespace "/baz" do
|
41
|
-
send(verb) { 'foobarbaz' }
|
42
|
-
end
|
43
|
-
end
|
44
|
-
end
|
45
|
-
browse_route(verb, "/foo/bar/baz").should be_ok
|
46
|
-
browse_route(verb, "/foo/bar/baz").body.should == "foobarbaz" unless verb == :head
|
47
|
-
end
|
48
|
-
|
49
|
-
it "allows regular expressions" do
|
50
|
-
app.namespace %r{/\d\d} do
|
51
|
-
send(verb) { "foo" }
|
52
|
-
namespace %r{/\d\d} do
|
53
|
-
send(verb) { "bar" }
|
54
|
-
end
|
55
|
-
namespace "/0000" do
|
56
|
-
send(verb) { "baz" }
|
57
|
-
end
|
58
|
-
end
|
59
|
-
browse_route(verb, '/20').should be_ok
|
60
|
-
browse_route(verb, '/20').body.should == "foo" unless verb == :head
|
61
|
-
browse_route(verb, '/20/20').should be_ok
|
62
|
-
browse_route(verb, '/20/20').body.should == "bar" unless verb == :head
|
63
|
-
browse_route(verb, '/20/0000').should be_ok
|
64
|
-
browse_route(verb, '/20/0000').body.should == "baz" unless verb == :head
|
65
|
-
browse_route(verb, '/20/200').should_not be_ok
|
66
|
-
end
|
67
|
-
end
|
68
|
-
|
69
|
-
describe :filters do
|
70
|
-
it 'should trigger before filters for namespaces' do
|
71
|
-
app.before { settings.set :foo, 0 }
|
72
|
-
app.namespace('/foo') do
|
73
|
-
before { settings.set :foo, settings.foo + 1 }
|
74
|
-
send(verb) { }
|
75
|
-
end
|
76
|
-
browse_route(verb, '/foo').should be_ok
|
77
|
-
app.foo.should == 1
|
78
|
-
end
|
79
|
-
it 'should trigger after filters for namespaces' do
|
80
|
-
$foo = 0
|
81
|
-
app.after { $foo += 2 }
|
82
|
-
app.namespace('/foo') do
|
83
|
-
after { $foo += 1 }
|
84
|
-
send(verb) { }
|
85
|
-
end
|
86
|
-
browse_route(verb, '/foo').should be_ok
|
87
|
-
$foo.should == 3
|
88
|
-
end
|
89
|
-
end
|
90
|
-
|
91
|
-
describe :make_namespace do
|
92
|
-
it "extends modules make_namespace is called on" do
|
93
|
-
mod = Module.new
|
94
|
-
mod.should_not respond_to(verb)
|
95
|
-
app.make_namespace(mod, :prefix => "/foo")
|
96
|
-
mod.should respond_to(verb)
|
97
|
-
end
|
98
|
-
|
99
|
-
it "returns the module" do
|
100
|
-
mod = Module.new
|
101
|
-
app.make_namespace(mod, :prefix => "/foo").should == mod
|
102
|
-
end
|
103
|
-
|
104
|
-
it "sets base" do
|
105
|
-
app.make_namespace(Module.new, :prefix => "/foo").base.should == app
|
106
|
-
end
|
107
|
-
|
108
|
-
it "sets prefix" do
|
109
|
-
app.make_namespace(Module.new, :prefix => "/foo").prefix.should == "/foo"
|
110
|
-
end
|
111
|
-
|
112
|
-
it "automatically sets a prefix based on module name if none is given" do
|
113
|
-
# FooBar = Module.new <= does not work in Ruby 1.9
|
114
|
-
module ::FooBar; end
|
115
|
-
app.make_namespace ::FooBar
|
116
|
-
::FooBar.prefix.should == "/foo_bar"
|
117
|
-
end
|
118
|
-
|
119
|
-
it "does not add the application name to auto-generated prefixes" do
|
120
|
-
#App::FooBar = Module.new <= does not work in Ruby 1.9
|
121
|
-
class ::App < Sinatra::Base; module FooBar; end; end
|
122
|
-
app.make_namespace App::FooBar
|
123
|
-
App::FooBar.prefix.should == "/foo_bar"
|
124
|
-
end
|
125
|
-
end
|
126
|
-
|
127
|
-
describe :auto_namespace do
|
128
|
-
before do
|
129
|
-
class ::App < Sinatra::Base; module Foo; end; end
|
130
|
-
end
|
131
|
-
|
132
|
-
it "detects #{verb}" do
|
133
|
-
App::Foo.should_not respond_to(verb)
|
134
|
-
App::Foo.send(verb, "/bar") { "baz" }
|
135
|
-
App::Foo.should respond_to(verb)
|
136
|
-
browse_route(verb, "/foo/bar").should be_ok
|
137
|
-
browse_route(verb, "/foo/bar").body.should == "baz" unless verb == :head
|
138
|
-
end
|
139
|
-
|
140
|
-
it "ignores #{verb} if auto namespaceing is disabled" do
|
141
|
-
app.disable :auto_namespace
|
142
|
-
App::Foo.should_not respond_to(verb)
|
143
|
-
proc { App::Foo.send(verb, "/bar") { "baz" } }.should raise_error(NameError)
|
144
|
-
App::Foo.should_not respond_to(verb)
|
145
|
-
end
|
146
|
-
|
147
|
-
it "ignores #{verb} if told to via :except" do
|
148
|
-
app.set :auto_namespace, :except => verb
|
149
|
-
App::Foo.should_not respond_to(verb)
|
150
|
-
proc { App::Foo.send(verb, "/bar") { "baz" } }.should raise_error(NameError)
|
151
|
-
App::Foo.should_not respond_to(verb)
|
152
|
-
end
|
153
|
-
|
154
|
-
it "does not ignore #{verb} if not included in :except" do
|
155
|
-
app.set :auto_namespace, :except => ["prefix"]
|
156
|
-
App::Foo.should_not respond_to(verb)
|
157
|
-
App::Foo.send(verb, "/bar") { "baz" }
|
158
|
-
App::Foo.should respond_to(verb)
|
159
|
-
end
|
160
|
-
|
161
|
-
it "does ignore #{verb} if not included in :only" do
|
162
|
-
app.set :auto_namespace, :only => "prefix"
|
163
|
-
App::Foo.should_not respond_to(verb)
|
164
|
-
proc { App::Foo.send(verb, "/bar") { "baz" } }.should raise_error(NameError)
|
165
|
-
App::Foo.should_not respond_to(verb)
|
166
|
-
end
|
167
|
-
|
168
|
-
it "does not ignore #{verb} if included in :only" do
|
169
|
-
app.set :auto_namespace, :only => ["prefix", verb]
|
170
|
-
App::Foo.should_not respond_to(verb)
|
171
|
-
App::Foo.send(verb, "/bar") { "baz" }
|
172
|
-
App::Foo.should respond_to(verb)
|
173
|
-
end
|
174
|
-
|
175
|
-
it "detects prefix" do
|
176
|
-
App::Foo.should_not respond_to(:prefix)
|
177
|
-
App::Foo.prefix.should == "/foo"
|
178
|
-
end
|
179
|
-
end
|
180
|
-
|
181
|
-
describe :helpers do
|
182
|
-
it "makes helpers defined inside a namespace not available to routes outside that namespace" do
|
183
|
-
helpers { define_method(:foo) { 42 } }
|
184
|
-
app.namespace("/foo").helpers { define_method(:bar) { 42 } }
|
185
|
-
app.new.should respond_to(:foo)
|
186
|
-
app.new.should_not respond_to(:bar)
|
187
|
-
end
|
188
|
-
|
189
|
-
it "allows overwriting helpers for routes within a namespace" do
|
190
|
-
helpers { define_method(:foo) { "foo" } }
|
191
|
-
define_route(verb, "/foo") { foo }
|
192
|
-
app.namespace("/bar") do
|
193
|
-
define_method(:foo) { "bar" }
|
194
|
-
send(verb, "/foo") { foo }
|
195
|
-
end
|
196
|
-
browse_route(verb, "/foo").should be_ok
|
197
|
-
browse_route(verb, "/bar/foo").should be_ok
|
198
|
-
unless verb == :head
|
199
|
-
browse_route(verb, "/foo").body.should == "foo"
|
200
|
-
browse_route(verb, "/bar/foo").body.should == "bar"
|
201
|
-
end
|
202
|
-
end
|
203
|
-
|
204
|
-
it "allows accessing helpers defined outside the namespace" do
|
205
|
-
helpers { define_method(:foo) { "foo" } }
|
206
|
-
app.namespace("/foo").send(verb, "") { foo }
|
207
|
-
browse_route(verb, "/foo").should be_ok
|
208
|
-
browse_route(verb, "/foo").body.should == "foo" unless verb == :head
|
209
|
-
end
|
210
|
-
|
211
|
-
it "allows calling super in helpers overwritten inside a namespace" do
|
212
|
-
helpers { define_method(:foo) { "foo" } }
|
213
|
-
app.namespace("/foo") do
|
214
|
-
define_method(:foo) { super().upcase }
|
215
|
-
send(verb) { foo }
|
216
|
-
end
|
217
|
-
browse_route(verb, "/foo").should be_ok
|
218
|
-
browse_route(verb, "/foo").body.should == "FOO" unless verb == :head
|
219
|
-
end
|
220
|
-
end
|
221
|
-
|
222
|
-
end
|
223
|
-
end
|
224
|
-
|
225
|
-
describe :errors do
|
226
|
-
it "should allow custom error handlers with not found" do
|
227
|
-
app.namespace('/de') do
|
228
|
-
not_found { 'nicht gefunden' }
|
229
|
-
end
|
230
|
-
get('/foo').status.should == 404
|
231
|
-
last_response.body.should_not == 'nicht gefunden'
|
232
|
-
get('/en/foo').status.should == 404
|
233
|
-
last_response.body.should_not == 'nicht gefunden'
|
234
|
-
get('/de/foo').status.should == 404
|
235
|
-
last_response.body.should == 'nicht gefunden'
|
236
|
-
end
|
237
|
-
|
238
|
-
it "should allow custom error handlers with error" do
|
239
|
-
app.namespace('/de') do
|
240
|
-
error(404) { 'nicht gefunden' }
|
241
|
-
end
|
242
|
-
get('/foo').status.should == 404
|
243
|
-
last_response.body.should_not == 'nicht gefunden'
|
244
|
-
get('/en/foo').status.should == 404
|
245
|
-
last_response.body.should_not == 'nicht gefunden'
|
246
|
-
get('/de/foo').status.should == 404
|
247
|
-
last_response.body.should == 'nicht gefunden'
|
248
|
-
end
|
249
|
-
end
|
250
|
-
|
251
|
-
describe 'conditions' do
|
252
|
-
it 'allows using conditions' do
|
253
|
-
app.namespace(:host_name => 'example.com') do
|
254
|
-
get('/') { 'yes' }
|
255
|
-
end
|
256
|
-
app.get('/') { 'no' }
|
257
|
-
get('/', {}, { 'HTTP_HOST' => 'example.com' })
|
258
|
-
last_response.body.should == 'yes'
|
259
|
-
get('/', {}, { 'HTTP_HOST' => 'example.org' })
|
260
|
-
last_response.body.should == 'no'
|
261
|
-
end
|
262
|
-
|
263
|
-
it 'allows combining conditions with a prefix' do
|
264
|
-
app.namespace('/foo', :host_name => 'example.com') do
|
265
|
-
get { 'yes' }
|
266
|
-
end
|
267
|
-
app.get('/foo') { 'no' }
|
268
|
-
get('/foo', {}, { 'HTTP_HOST' => 'example.com' })
|
269
|
-
last_response.body.should == 'yes'
|
270
|
-
get('/foo', {}, { 'HTTP_HOST' => 'example.org' })
|
271
|
-
last_response.body.should == 'no'
|
272
|
-
end
|
273
|
-
end
|
274
|
-
|
275
|
-
describe 'memory' do
|
276
|
-
before do
|
277
|
-
app.namespace('/foo') { get('/bar') { 'blah' }}
|
278
|
-
end
|
279
|
-
|
280
|
-
def measure
|
281
|
-
100.times { get('/foo/bar') }
|
282
|
-
GC.start
|
283
|
-
# ObjectSpace.each_object.to_a.size
|
284
|
-
sum = 0
|
285
|
-
ObjectSpace.each_object { sum += 1 }
|
286
|
-
sum
|
287
|
-
end
|
288
|
-
|
289
|
-
it 'should not leak objects' do
|
290
|
-
if Monkey::Engine.mri?
|
291
|
-
measure
|
292
|
-
2.times do
|
293
|
-
first = measure
|
294
|
-
second = measure
|
295
|
-
second.should <= first + 3
|
296
|
-
end
|
297
|
-
end
|
298
|
-
end
|
299
|
-
end
|
300
|
-
end
|
data/spec/spec_helper.rb
DELETED