nyny 2.2.1 → 3.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +20 -20
- data/.rspec +2 -2
- data/.ruby-version +1 -1
- data/.travis.yml +11 -12
- data/CHANGELOG +40 -27
- data/Gemfile +8 -8
- data/LICENSE.txt +22 -22
- data/Performance.md +46 -44
- data/README.md +423 -381
- data/Rakefile +6 -6
- data/benchmark.rb +125 -125
- data/lib/nyny.rb +33 -36
- data/lib/nyny/app.rb +79 -67
- data/lib/nyny/core-ext/runner.rb +19 -19
- data/lib/nyny/core-ext/templates.rb +20 -18
- data/lib/nyny/primitives.rb +25 -25
- data/lib/nyny/request_scope.rb +43 -52
- data/lib/nyny/route.rb +40 -40
- data/lib/nyny/router.rb +44 -43
- data/lib/nyny/version.rb +3 -3
- data/nyny.gemspec +27 -26
- data/spec/app_spec.rb +248 -200
- data/spec/inheritance_spec.rb +76 -0
- data/spec/nyny_spec.rb +11 -11
- data/spec/primitives_spec.rb +33 -33
- data/spec/request_scope_spec.rb +103 -105
- data/spec/router_spec.rb +21 -21
- data/spec/runner_spec.rb +23 -22
- data/spec/spec_helper.rb +61 -58
- data/spec/templates_spec.rb +52 -52
- data/spec/views/layout.erb +6 -6
- metadata +27 -12
- data/lib/nyny/middleware_chain.rb +0 -14
data/Rakefile
CHANGED
@@ -1,6 +1,6 @@
|
|
1
|
-
require "bundler/gem_tasks"
|
2
|
-
require 'rspec/core/rake_task'
|
3
|
-
|
4
|
-
desc "run all specs"
|
5
|
-
RSpec::Core::RakeTask.new(:default)
|
6
|
-
|
1
|
+
require "bundler/gem_tasks"
|
2
|
+
require 'rspec/core/rake_task'
|
3
|
+
|
4
|
+
desc "run all specs"
|
5
|
+
RSpec::Core::RakeTask.new(:default)
|
6
|
+
|
data/benchmark.rb
CHANGED
@@ -1,125 +1,125 @@
|
|
1
|
-
#!ruby -I ./lib -I lib
|
2
|
-
require 'nyny'
|
3
|
-
require 'ruby-prof'
|
4
|
-
require 'benchmark'
|
5
|
-
require 'sinatra'
|
6
|
-
include Benchmark
|
7
|
-
|
8
|
-
set :run, false #do not run sinatra's builtin web server
|
9
|
-
|
10
|
-
def build_apps &block
|
11
|
-
sinatra = Class.new(Sinatra::Base, &block).new
|
12
|
-
nyny = Class.new(NYNY::App, &block).new
|
13
|
-
return [nyny, sinatra].map {|app| Rack::MockRequest.new(app) }
|
14
|
-
end
|
15
|
-
|
16
|
-
def run_test name, apps, &block
|
17
|
-
nyny, sinatra = apps
|
18
|
-
prc = Proc.new(&block)
|
19
|
-
|
20
|
-
puts "\nTest: #{name}"
|
21
|
-
Benchmark.benchmark(CAPTION, 7, FORMAT, "> NYNY/Sinatra:") do |x|
|
22
|
-
nyny_time = x.report("nyny: ") { 1000.times { prc.call(nyny) } }
|
23
|
-
sinatra_time = x.report("sinatra:") { 1000.times { prc.call(sinatra) } }
|
24
|
-
puts "NYNY is #{"%.2f" % [sinatra_time.real/nyny_time.real]}x faster than Sinatra in this test"
|
25
|
-
end
|
26
|
-
end
|
27
|
-
|
28
|
-
puts "Comparing NYNY #{NYNY::VERSION} with Sinatra #{Sinatra::VERSION}"
|
29
|
-
|
30
|
-
#
|
31
|
-
# Empty app
|
32
|
-
apps = build_apps do
|
33
|
-
#empty app
|
34
|
-
end
|
35
|
-
run_test 'empty', apps do |app|
|
36
|
-
app.get '/'
|
37
|
-
end
|
38
|
-
|
39
|
-
#
|
40
|
-
# Hello World
|
41
|
-
apps = build_apps do
|
42
|
-
get '/' do
|
43
|
-
'Hello World'
|
44
|
-
end
|
45
|
-
end
|
46
|
-
run_test 'hello world', apps do |app|
|
47
|
-
app.get '/'
|
48
|
-
end
|
49
|
-
|
50
|
-
#
|
51
|
-
# Filters
|
52
|
-
apps = build_apps do
|
53
|
-
before do
|
54
|
-
request
|
55
|
-
end
|
56
|
-
|
57
|
-
after do
|
58
|
-
response
|
59
|
-
end
|
60
|
-
|
61
|
-
get '/' do
|
62
|
-
'Hello World!'
|
63
|
-
end
|
64
|
-
end
|
65
|
-
run_test 'filters', apps do |app|
|
66
|
-
app.get '/'
|
67
|
-
end
|
68
|
-
|
69
|
-
#
|
70
|
-
# Helpers
|
71
|
-
apps = build_apps do
|
72
|
-
helpers do
|
73
|
-
def da_request
|
74
|
-
request
|
75
|
-
end
|
76
|
-
end
|
77
|
-
|
78
|
-
get '/' do
|
79
|
-
da_request
|
80
|
-
end
|
81
|
-
end
|
82
|
-
run_test 'helpers', apps do |app|
|
83
|
-
app.get '/'
|
84
|
-
end
|
85
|
-
|
86
|
-
#
|
87
|
-
# Url patterns
|
88
|
-
apps = build_apps do
|
89
|
-
get '/:name' do
|
90
|
-
params[:name]
|
91
|
-
end
|
92
|
-
end
|
93
|
-
run_test 'Url patterns', apps do |app|
|
94
|
-
app.get '/foo'
|
95
|
-
end
|
96
|
-
|
97
|
-
# Plain routes
|
98
|
-
apps = build_apps do
|
99
|
-
[:get, :post, :put].each do |method|
|
100
|
-
10.times do |i|
|
101
|
-
send(method, "/foo/#{i}") do
|
102
|
-
i
|
103
|
-
end
|
104
|
-
end
|
105
|
-
end
|
106
|
-
end
|
107
|
-
run_test 'A lot o Plain routes', apps do |app|
|
108
|
-
app.get '/foo/5'
|
109
|
-
end
|
110
|
-
|
111
|
-
#
|
112
|
-
# Pattern routes
|
113
|
-
apps = build_apps do
|
114
|
-
[:get, :post, :put].each do |method|
|
115
|
-
10.times do |i|
|
116
|
-
send(method, "/foo/#{i}/:action") do
|
117
|
-
params[:action]
|
118
|
-
end
|
119
|
-
end
|
120
|
-
end
|
121
|
-
end
|
122
|
-
run_test 'A lot of Pattern routes', apps do |app|
|
123
|
-
app.get '/foo/5/edit'
|
124
|
-
end
|
125
|
-
|
1
|
+
#!ruby -I ./lib -I lib
|
2
|
+
require 'nyny'
|
3
|
+
require 'ruby-prof'
|
4
|
+
require 'benchmark'
|
5
|
+
require 'sinatra'
|
6
|
+
include Benchmark
|
7
|
+
|
8
|
+
set :run, false #do not run sinatra's builtin web server
|
9
|
+
|
10
|
+
def build_apps &block
|
11
|
+
sinatra = Class.new(Sinatra::Base, &block).new
|
12
|
+
nyny = Class.new(NYNY::App, &block).new
|
13
|
+
return [nyny, sinatra].map {|app| Rack::MockRequest.new(app) }
|
14
|
+
end
|
15
|
+
|
16
|
+
def run_test name, apps, &block
|
17
|
+
nyny, sinatra = apps
|
18
|
+
prc = Proc.new(&block)
|
19
|
+
|
20
|
+
puts "\nTest: #{name}"
|
21
|
+
Benchmark.benchmark(CAPTION, 7, FORMAT, "> NYNY/Sinatra:") do |x|
|
22
|
+
nyny_time = x.report("nyny: ") { 1000.times { prc.call(nyny) } }
|
23
|
+
sinatra_time = x.report("sinatra:") { 1000.times { prc.call(sinatra) } }
|
24
|
+
puts "NYNY is #{"%.2f" % [sinatra_time.real/nyny_time.real]}x faster than Sinatra in this test"
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
puts "Comparing NYNY #{NYNY::VERSION} with Sinatra #{Sinatra::VERSION}"
|
29
|
+
|
30
|
+
#
|
31
|
+
# Empty app
|
32
|
+
apps = build_apps do
|
33
|
+
#empty app
|
34
|
+
end
|
35
|
+
run_test 'empty', apps do |app|
|
36
|
+
app.get '/'
|
37
|
+
end
|
38
|
+
|
39
|
+
#
|
40
|
+
# Hello World
|
41
|
+
apps = build_apps do
|
42
|
+
get '/' do
|
43
|
+
'Hello World'
|
44
|
+
end
|
45
|
+
end
|
46
|
+
run_test 'hello world', apps do |app|
|
47
|
+
app.get '/'
|
48
|
+
end
|
49
|
+
|
50
|
+
#
|
51
|
+
# Filters
|
52
|
+
apps = build_apps do
|
53
|
+
before do
|
54
|
+
request
|
55
|
+
end
|
56
|
+
|
57
|
+
after do
|
58
|
+
response
|
59
|
+
end
|
60
|
+
|
61
|
+
get '/' do
|
62
|
+
'Hello World!'
|
63
|
+
end
|
64
|
+
end
|
65
|
+
run_test 'filters', apps do |app|
|
66
|
+
app.get '/'
|
67
|
+
end
|
68
|
+
|
69
|
+
#
|
70
|
+
# Helpers
|
71
|
+
apps = build_apps do
|
72
|
+
helpers do
|
73
|
+
def da_request
|
74
|
+
request
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
get '/' do
|
79
|
+
da_request
|
80
|
+
end
|
81
|
+
end
|
82
|
+
run_test 'helpers', apps do |app|
|
83
|
+
app.get '/'
|
84
|
+
end
|
85
|
+
|
86
|
+
#
|
87
|
+
# Url patterns
|
88
|
+
apps = build_apps do
|
89
|
+
get '/:name' do
|
90
|
+
params[:name]
|
91
|
+
end
|
92
|
+
end
|
93
|
+
run_test 'Url patterns', apps do |app|
|
94
|
+
app.get '/foo'
|
95
|
+
end
|
96
|
+
|
97
|
+
# Plain routes
|
98
|
+
apps = build_apps do
|
99
|
+
[:get, :post, :put].each do |method|
|
100
|
+
10.times do |i|
|
101
|
+
send(method, "/foo/#{i}") do
|
102
|
+
i
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
107
|
+
run_test 'A lot o Plain routes', apps do |app|
|
108
|
+
app.get '/foo/5'
|
109
|
+
end
|
110
|
+
|
111
|
+
#
|
112
|
+
# Pattern routes
|
113
|
+
apps = build_apps do
|
114
|
+
[:get, :post, :put].each do |method|
|
115
|
+
10.times do |i|
|
116
|
+
send(method, "/foo/#{i}/:action") do
|
117
|
+
params[:action]
|
118
|
+
end
|
119
|
+
end
|
120
|
+
end
|
121
|
+
end
|
122
|
+
run_test 'A lot of Pattern routes', apps do |app|
|
123
|
+
app.get '/foo/5/edit'
|
124
|
+
end
|
125
|
+
|
data/lib/nyny.rb
CHANGED
@@ -1,36 +1,33 @@
|
|
1
|
-
require 'uri'
|
2
|
-
require 'rack'
|
3
|
-
|
4
|
-
|
5
|
-
require 'nyny/
|
6
|
-
require 'nyny/
|
7
|
-
require 'nyny/
|
8
|
-
require 'nyny/
|
9
|
-
require 'nyny/
|
10
|
-
require 'nyny/
|
11
|
-
require 'nyny/
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
@env ||= EnvString.new(ENV['RACK_ENV'] || 'development')
|
35
|
-
end
|
36
|
-
end
|
1
|
+
require 'uri'
|
2
|
+
require 'rack'
|
3
|
+
|
4
|
+
require 'nyny/version'
|
5
|
+
require 'nyny/primitives'
|
6
|
+
require 'nyny/request_scope'
|
7
|
+
require 'nyny/route'
|
8
|
+
require 'nyny/app'
|
9
|
+
require 'nyny/router'
|
10
|
+
require 'nyny/core-ext/runner'
|
11
|
+
require 'nyny/core-ext/templates'
|
12
|
+
|
13
|
+
module NYNY
|
14
|
+
class EnvString < String
|
15
|
+
[:production, :development, :test].each do |env|
|
16
|
+
define_method "#{env}?" do
|
17
|
+
self == env.to_s
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.root
|
23
|
+
Dir.pwd
|
24
|
+
end
|
25
|
+
|
26
|
+
def self.env
|
27
|
+
@env ||= EnvString.new(ENV['RACK_ENV'] || 'development')
|
28
|
+
end
|
29
|
+
|
30
|
+
App.register NYNY::Runner
|
31
|
+
App.register NYNY::Templates
|
32
|
+
end
|
33
|
+
|
data/lib/nyny/app.rb
CHANGED
@@ -1,67 +1,79 @@
|
|
1
|
-
module NYNY
|
2
|
-
class App
|
3
|
-
HTTP_VERBS = [:delete, :get, :head, :options, :patch, :post, :put, :trace]
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
@
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
@
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
end
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
end
|
1
|
+
module NYNY
|
2
|
+
class App
|
3
|
+
HTTP_VERBS = [:delete, :get, :head, :options, :patch, :post, :put, :trace]
|
4
|
+
|
5
|
+
def self.inheritable name, value
|
6
|
+
@_inheritables ||= []
|
7
|
+
@_inheritables << name
|
8
|
+
self.class.send :attr_accessor, name
|
9
|
+
self.send "#{name}=", value
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.inherited subclass
|
13
|
+
@_inheritables.each do |attr|
|
14
|
+
subclass.send "#{attr}=", self.send(attr).clone
|
15
|
+
subclass.instance_variable_set "@_inheritables", @_inheritables.clone
|
16
|
+
end
|
17
|
+
|
18
|
+
super
|
19
|
+
end
|
20
|
+
|
21
|
+
inheritable :builder, Rack::Builder.new
|
22
|
+
inheritable :routes, []
|
23
|
+
inheritable :before_hooks, []
|
24
|
+
inheritable :after_hooks, []
|
25
|
+
inheritable :scope_class, Class.new(RequestScope)
|
26
|
+
|
27
|
+
def initialize app=nil
|
28
|
+
self.class.builder.run Router.new({
|
29
|
+
:routes => self.class.routes,
|
30
|
+
:fallback => (app || lambda {|env| Response.new '', 404 }),
|
31
|
+
:before_hooks => self.class.before_hooks,
|
32
|
+
:after_hooks => self.class.after_hooks,
|
33
|
+
:scope_class => self.class.scope_class
|
34
|
+
})
|
35
|
+
end
|
36
|
+
|
37
|
+
def call env
|
38
|
+
self.class.builder.call env
|
39
|
+
end
|
40
|
+
|
41
|
+
#class methods
|
42
|
+
class << self
|
43
|
+
HTTP_VERBS.each do |method|
|
44
|
+
define_method method do |str, &blk|
|
45
|
+
routes << Route.new(method, str, &blk)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
def register *extensions
|
50
|
+
extensions.each do |ext|
|
51
|
+
extend ext
|
52
|
+
ext.registered(self) if ext.respond_to?(:registered)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
def namespace url, &block
|
57
|
+
app = Class.new(NYNY::App, &block)
|
58
|
+
builder.map (url) { use app }
|
59
|
+
end
|
60
|
+
|
61
|
+
def before &blk
|
62
|
+
before_hooks << Proc.new(&blk)
|
63
|
+
end
|
64
|
+
|
65
|
+
def after &blk
|
66
|
+
after_hooks << Proc.new(&blk)
|
67
|
+
end
|
68
|
+
|
69
|
+
def use middleware, *args, &block
|
70
|
+
builder.use middleware, *args, &block
|
71
|
+
end
|
72
|
+
|
73
|
+
def helpers *args, &block
|
74
|
+
args << Module.new(&block) if block_given?
|
75
|
+
args.each {|m| scope_class.send :include, m }
|
76
|
+
end
|
77
|
+
end #class methods
|
78
|
+
end
|
79
|
+
end
|