nyny 3.0.0 → 3.0.1

Sign up to get free protection for your applications and to get access to all the features.
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
- # 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
+ # 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