nyny 2.1.1 → 2.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +2 -0
- data/README.md +36 -3
- data/benchmark.rb +30 -0
- data/lib/nyny.rb +3 -1
- data/lib/nyny/core-ext/templates.rb +19 -0
- data/lib/nyny/version.rb +1 -1
- data/nyny.gemspec +1 -0
- data/spec/spec_helper.rb +4 -0
- data/spec/templates_spec.rb +52 -0
- data/spec/views/index.erb +1 -0
- data/spec/views/instance.erb +1 -0
- data/spec/views/layout.erb +7 -0
- data/spec/views/local.erb +1 -0
- metadata +27 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6dd6eb95721d78c86e3bbb400a9840369d02e438
|
4
|
+
data.tar.gz: d85151c9759b13ef449ca5b37fecf4575a486a84
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5cd94aac928361668da4b20ee7d23609749d47d4545b7fc13a52f51fae4421c8a7a79655ec96488a90899f13b582bd6de6d9308842b7cd9b95a8f664fb3cce37
|
7
|
+
data.tar.gz: 3d6a67e6dfa7052770ac7b9199863d6d4ad2890d13a3d28be60aaf36ffba7da6fb6be157647a881a8ec1e9f2b49696f5687dac548a5135f41738e12ee77327f6
|
data/.gitignore
CHANGED
data/README.md
CHANGED
@@ -42,6 +42,7 @@ Open the browser at [http://localhost:9292](http://localhost:9292)
|
|
42
42
|
- [Environment](#environment)
|
43
43
|
- [Running](#running)
|
44
44
|
- [Defining routes](#defining-routes)
|
45
|
+
- [Templates](#templates)
|
45
46
|
- [Request scope](#request-scope)
|
46
47
|
- [Filters](#filters)
|
47
48
|
- [Middleware](#middleware)
|
@@ -144,7 +145,6 @@ development easier: Rack::CommonLogger and Rack::ShowExceptions.
|
|
144
145
|
This will show all requests in the log, and will provide useful details
|
145
146
|
in the case a error occurs during a request.
|
146
147
|
|
147
|
-
|
148
148
|
## Defining routes
|
149
149
|
|
150
150
|
NYNY supports the following verbs for defining a route: delete, get, head,
|
@@ -182,6 +182,36 @@ end
|
|
182
182
|
Each block that is passed to a route definition is evaluated in the context of
|
183
183
|
a request scope. See below what methods are available there.
|
184
184
|
|
185
|
+
## Templates
|
186
|
+
NYNY can render templates, all you need is to call the `render` function:
|
187
|
+
```ruby
|
188
|
+
class App < NYNY::App
|
189
|
+
get '/' do
|
190
|
+
render 'index.erb'
|
191
|
+
end
|
192
|
+
end
|
193
|
+
```
|
194
|
+
There are 2 ways to pass data to the template:
|
195
|
+
|
196
|
+
Via a instance variable:
|
197
|
+
```ruby
|
198
|
+
class App < NYNY::App
|
199
|
+
get '/' do
|
200
|
+
@foo = 'bar' #access it as @foo in template
|
201
|
+
render 'index.erb'
|
202
|
+
end
|
203
|
+
end
|
204
|
+
```
|
205
|
+
|
206
|
+
Or via a local variable:
|
207
|
+
```ruby
|
208
|
+
class App < NYNY::App
|
209
|
+
get '/' do
|
210
|
+
render 'index.erb', :foo => 'bar' #access it as foo in template
|
211
|
+
end
|
212
|
+
end
|
213
|
+
```
|
214
|
+
|
185
215
|
## Request scope
|
186
216
|
As was said above, when you pass a block to a route definition,
|
187
217
|
that block is evaluated in the context of a [RequestScope][2].
|
@@ -189,7 +219,9 @@ This means that several methods/objects available inside that block:
|
|
189
219
|
|
190
220
|
- `request` - A `Rack::Request` object which encapsulates the request
|
191
221
|
to that route. (see [Rack::Request documentation][3] for more info)
|
192
|
-
- `response` - A `Rack::Response` object which encapsulates the response
|
222
|
+
- `response` - A `Rack::Response` object which encapsulates the response.
|
223
|
+
Additionally, NYNY's response exposes 2 more methods in addition to Rack's ones.
|
224
|
+
(see [primitives.rb][primitivesrb])
|
193
225
|
- `params` - a hash which contains both POST body params and GET querystring params.
|
194
226
|
- `headers` - allows you to read/add headers to the response
|
195
227
|
(ex: `headers 'Content-Type' => 'text/html'`)
|
@@ -229,7 +261,7 @@ end
|
|
229
261
|
|
230
262
|
## Middleware
|
231
263
|
|
232
|
-
A NYNY app is a Rack middleware, which means that it can be used inside
|
264
|
+
A NYNY app is a Rack middleware, which means that it can be used inside
|
233
265
|
Sinatra, Rails, or any other Rack-based app:
|
234
266
|
|
235
267
|
```ruby
|
@@ -346,3 +378,4 @@ TBD.
|
|
346
378
|
[performance]: https://github.com/alisnic/nyny/blob/master/Performance.md
|
347
379
|
[rack-middleware]: https://github.com/rack/rack/wiki/List-of-Middleware
|
348
380
|
[halt-definition]: https://github.com/alisnic/nyny/blob/master/lib/nyny/request_scope.rb#L36
|
381
|
+
[primitivesrb]: https://github.com/alisnic/nyny/blob/master/lib/nyny/primitives.rb
|
data/benchmark.rb
CHANGED
@@ -93,3 +93,33 @@ end
|
|
93
93
|
run_test 'Url patterns', apps do |app|
|
94
94
|
app.get '/foo'
|
95
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,5 +1,6 @@
|
|
1
1
|
require 'uri'
|
2
2
|
require 'rack'
|
3
|
+
require 'tilt'
|
3
4
|
|
4
5
|
require 'nyny/version'
|
5
6
|
require 'nyny/primitives'
|
@@ -9,12 +10,13 @@ require 'nyny/middleware_chain'
|
|
9
10
|
require 'nyny/app'
|
10
11
|
require 'nyny/router'
|
11
12
|
|
12
|
-
|
13
13
|
# Register core extensions
|
14
14
|
require 'nyny/core-ext/runner'
|
15
|
+
require 'nyny/core-ext/templates'
|
15
16
|
|
16
17
|
module NYNY
|
17
18
|
App.register NYNY::Runner
|
19
|
+
App.register NYNY::Templates
|
18
20
|
|
19
21
|
class EnvString < String
|
20
22
|
[:production, :development, :test].each do |env|
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module NYNY
|
2
|
+
module Templates
|
3
|
+
module Helpers
|
4
|
+
def render template, locals = {}, options = {}, &block
|
5
|
+
template_cache.fetch(template) do
|
6
|
+
Tilt.new(template, options)
|
7
|
+
end.render(self, locals, &block)
|
8
|
+
end
|
9
|
+
|
10
|
+
def template_cache
|
11
|
+
Thread.current[:template_cache] ||= Tilt::Cache.new
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.registered app
|
16
|
+
app.helpers Helpers
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
data/lib/nyny/version.rb
CHANGED
data/nyny.gemspec
CHANGED
@@ -19,6 +19,7 @@ Gem::Specification.new do |spec|
|
|
19
19
|
spec.require_paths = ["lib"]
|
20
20
|
|
21
21
|
spec.add_dependency "rack"
|
22
|
+
spec.add_dependency "tilt"
|
22
23
|
spec.add_development_dependency "bundler", "~> 1.3"
|
23
24
|
spec.add_development_dependency "rake"
|
24
25
|
spec.add_development_dependency "rspec"
|
data/spec/spec_helper.rb
CHANGED
@@ -20,6 +20,10 @@ class Rack::MockRequest
|
|
20
20
|
def options(uri, opts={}) request("OPTIONS", uri, opts) end
|
21
21
|
end
|
22
22
|
|
23
|
+
def template name
|
24
|
+
File.join(File.dirname(__FILE__), 'views', name)
|
25
|
+
end
|
26
|
+
|
23
27
|
def extended_modules_for kls
|
24
28
|
(class << kls; self end).included_modules
|
25
29
|
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Templates do
|
4
|
+
let (:app) do
|
5
|
+
mock_app do
|
6
|
+
get '/without_layout' do
|
7
|
+
render template('index.erb')
|
8
|
+
end
|
9
|
+
|
10
|
+
get '/with_layout' do
|
11
|
+
render template('layout.erb') do
|
12
|
+
render template('index.erb')
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
get '/instance_var' do
|
17
|
+
@foo = 'bar'
|
18
|
+
render template('instance.erb')
|
19
|
+
end
|
20
|
+
|
21
|
+
get '/local_var' do
|
22
|
+
render template('local.erb'), :foo => 'bar'
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
it 'renders correctly without layout' do
|
28
|
+
response = app.get('/without_layout')
|
29
|
+
response.body.should == '<p>Hello!</p>'
|
30
|
+
end
|
31
|
+
|
32
|
+
it 'passes a instance variable to template' do
|
33
|
+
response = app.get('/instance_var')
|
34
|
+
response.body.should == 'bar'
|
35
|
+
end
|
36
|
+
|
37
|
+
it 'passes a local variable to template' do
|
38
|
+
response = app.get('/local_var')
|
39
|
+
response.body.should == 'bar'
|
40
|
+
end
|
41
|
+
|
42
|
+
it 'renders correctly with layout' do
|
43
|
+
response = app.get('/with_layout')
|
44
|
+
|
45
|
+
rendered = Tilt.new(template('layout.erb')).render do
|
46
|
+
Tilt.new(template('index.erb')).render
|
47
|
+
end
|
48
|
+
|
49
|
+
response.body.should == rendered
|
50
|
+
end
|
51
|
+
|
52
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
<p>Hello!</p>
|
@@ -0,0 +1 @@
|
|
1
|
+
<%= @foo %>
|
@@ -0,0 +1 @@
|
|
1
|
+
<%= foo %>
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: nyny
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.
|
4
|
+
version: 2.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andrei Lisnic
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-
|
11
|
+
date: 2013-12-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rack
|
@@ -24,6 +24,20 @@ dependencies:
|
|
24
24
|
- - '>='
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: tilt
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
27
41
|
- !ruby/object:Gem::Dependency
|
28
42
|
name: bundler
|
29
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -87,6 +101,7 @@ files:
|
|
87
101
|
- lib/nyny.rb
|
88
102
|
- lib/nyny/app.rb
|
89
103
|
- lib/nyny/core-ext/runner.rb
|
104
|
+
- lib/nyny/core-ext/templates.rb
|
90
105
|
- lib/nyny/middleware_chain.rb
|
91
106
|
- lib/nyny/primitives.rb
|
92
107
|
- lib/nyny/request_scope.rb
|
@@ -101,6 +116,11 @@ files:
|
|
101
116
|
- spec/router_spec.rb
|
102
117
|
- spec/runner_spec.rb
|
103
118
|
- spec/spec_helper.rb
|
119
|
+
- spec/templates_spec.rb
|
120
|
+
- spec/views/index.erb
|
121
|
+
- spec/views/instance.erb
|
122
|
+
- spec/views/layout.erb
|
123
|
+
- spec/views/local.erb
|
104
124
|
homepage: http://alisnic.github.io/nyny/
|
105
125
|
licenses:
|
106
126
|
- MIT
|
@@ -133,3 +153,8 @@ test_files:
|
|
133
153
|
- spec/router_spec.rb
|
134
154
|
- spec/runner_spec.rb
|
135
155
|
- spec/spec_helper.rb
|
156
|
+
- spec/templates_spec.rb
|
157
|
+
- spec/views/index.erb
|
158
|
+
- spec/views/instance.erb
|
159
|
+
- spec/views/layout.erb
|
160
|
+
- spec/views/local.erb
|