nyny 3.0.0 → 3.0.1
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 -11
- data/CHANGELOG +45 -40
- data/Gemfile +7 -8
- data/LICENSE.txt +22 -22
- data/Performance.md +41 -46
- data/README.md +423 -423
- data/Rakefile +6 -6
- data/benchmark.rb +116 -125
- data/lib/nyny.rb +33 -33
- data/lib/nyny/app.rb +79 -79
- data/lib/nyny/core-ext/runner.rb +19 -19
- data/lib/nyny/core-ext/templates.rb +20 -20
- data/lib/nyny/primitives.rb +25 -25
- data/lib/nyny/request_scope.rb +43 -43
- data/lib/nyny/route.rb +40 -40
- data/lib/nyny/router.rb +46 -44
- data/lib/nyny/version.rb +3 -3
- data/nyny.gemspec +29 -27
- data/spec/app_spec.rb +248 -248
- data/spec/inheritance_spec.rb +76 -75
- data/spec/nyny_spec.rb +11 -11
- data/spec/primitives_spec.rb +33 -33
- data/spec/request_scope_spec.rb +103 -103
- data/spec/router_spec.rb +29 -21
- data/spec/runner_spec.rb +23 -23
- data/spec/spec_helper.rb +61 -61
- data/spec/templates_spec.rb +52 -52
- data/spec/views/layout.erb +6 -6
- metadata +3 -3
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,116 @@
|
|
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
|
-
#
|
32
|
-
apps = build_apps do
|
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
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
end
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
end
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
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
|
+
# Hello World
|
32
|
+
apps = build_apps do
|
33
|
+
get '/' do
|
34
|
+
'Hello World'
|
35
|
+
end
|
36
|
+
end
|
37
|
+
run_test 'hello world', apps do |app|
|
38
|
+
app.get '/'
|
39
|
+
end
|
40
|
+
|
41
|
+
#
|
42
|
+
# Filters
|
43
|
+
apps = build_apps do
|
44
|
+
before do
|
45
|
+
request
|
46
|
+
end
|
47
|
+
|
48
|
+
after do
|
49
|
+
response
|
50
|
+
end
|
51
|
+
|
52
|
+
get '/' do
|
53
|
+
'Hello World!'
|
54
|
+
end
|
55
|
+
end
|
56
|
+
run_test 'filters', apps do |app|
|
57
|
+
app.get '/'
|
58
|
+
end
|
59
|
+
|
60
|
+
#
|
61
|
+
# Helpers
|
62
|
+
apps = build_apps do
|
63
|
+
helpers do
|
64
|
+
def da_request
|
65
|
+
request
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
get '/' do
|
70
|
+
da_request
|
71
|
+
end
|
72
|
+
end
|
73
|
+
run_test 'helpers', apps do |app|
|
74
|
+
app.get '/'
|
75
|
+
end
|
76
|
+
|
77
|
+
#
|
78
|
+
# Url patterns
|
79
|
+
apps = build_apps do
|
80
|
+
get '/:name' do
|
81
|
+
params[:name]
|
82
|
+
end
|
83
|
+
end
|
84
|
+
run_test 'Url patterns', apps do |app|
|
85
|
+
app.get '/foo'
|
86
|
+
end
|
87
|
+
|
88
|
+
# Plain routes
|
89
|
+
apps = build_apps do
|
90
|
+
[:get, :post, :put].each do |method|
|
91
|
+
10.times do |i|
|
92
|
+
send(method, "/foo/#{i}") do
|
93
|
+
i
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
98
|
+
run_test 'A lot o Plain routes', apps do |app|
|
99
|
+
app.get '/foo/5'
|
100
|
+
end
|
101
|
+
|
102
|
+
#
|
103
|
+
# Pattern routes
|
104
|
+
apps = build_apps do
|
105
|
+
[:get, :post, :put].each do |method|
|
106
|
+
10.times do |i|
|
107
|
+
send(method, "/foo/#{i}/:action") do
|
108
|
+
params[:action]
|
109
|
+
end
|
110
|
+
end
|
111
|
+
end
|
112
|
+
end
|
113
|
+
run_test 'A lot of Pattern routes', apps do |app|
|
114
|
+
app.get '/foo/5/edit'
|
115
|
+
end
|
116
|
+
|
data/lib/nyny.rb
CHANGED
@@ -1,33 +1,33 @@
|
|
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
|
-
|
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,79 +1,79 @@
|
|
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
|
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
|