nyny 2.2.1 → 3.0.0

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,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
- require 'tilt'
4
-
5
- require 'nyny/version'
6
- require 'nyny/primitives'
7
- require 'nyny/request_scope'
8
- require 'nyny/route'
9
- require 'nyny/middleware_chain'
10
- require 'nyny/app'
11
- require 'nyny/router'
12
-
13
- # Register core extensions
14
- require 'nyny/core-ext/runner'
15
- require 'nyny/core-ext/templates'
16
-
17
- module NYNY
18
- App.register NYNY::Runner
19
- App.register NYNY::Templates
20
-
21
- class EnvString < String
22
- [:production, :development, :test].each do |env|
23
- define_method "#{env}?" do
24
- self == env.to_s
25
- end
26
- end
27
- end
28
-
29
- def self.root
30
- Dir.pwd
31
- end
32
-
33
- def self.env
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
- attr_reader :middleware_chain, :router
6
- def initialize app=nil
7
- @router = Router.new({
8
- :routes => self.class.routes,
9
- :fallback => (app || lambda {|env| Response.new '', 404 }),
10
- :before_hooks => self.class.before_hooks,
11
- :after_hooks => self.class.after_hooks
12
- })
13
- @middleware_chain = MiddlewareChain.new(self.class.middlewares,
14
- lambda {|env| _call(env)})
15
- end
16
-
17
- def _call env
18
- router.call env
19
- end
20
-
21
- def call env
22
- middleware_chain.call env
23
- end
24
-
25
- #class methods
26
- class << self
27
- HTTP_VERBS.each do |method|
28
- define_method method do |str, &blk|
29
- routes << Route.new(method, str, &blk)
30
- end
31
- end
32
-
33
- def middlewares; @middlewares ||= [] end
34
- def routes; @routes ||= [] end
35
- def before_hooks; @before_hooks ||= [] end
36
- def after_hooks; @after_hooks ||= [] end
37
-
38
- # move middleware chain and runner to core-ext
39
- def register *extensions
40
- extensions.each do |ext|
41
- extend ext
42
- ext.registered(self) if ext.respond_to?(:registered)
43
- end
44
- end
45
-
46
- def before &blk
47
- before_hooks << Proc.new(&blk)
48
- end
49
-
50
- def after &blk
51
- after_hooks << Proc.new(&blk)
52
- end
53
-
54
- def use middleware, *args, &block
55
- middlewares << [middleware, args, block]
56
- end
57
-
58
- def helpers *args, &block
59
- if block_given?
60
- args << Module.new(&block)
61
- end
62
-
63
- args.each {|m| RequestScope.add_helper_module m }
64
- end
65
- end #class methods
66
- end
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