cuba 3.0.0.rc1 → 3.0.0.rc2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.markdown +117 -2
- data/cuba.gemspec +1 -2
- data/lib/cuba.rb +1 -2
- data/lib/cuba/render.rb +48 -0
- data/test/render.rb +94 -0
- metadata +10 -20
- data/lib/cuba/settings.rb +0 -25
data/README.markdown
CHANGED
@@ -107,7 +107,6 @@ Cuba.define do
|
|
107
107
|
|
108
108
|
# /username/foobar
|
109
109
|
on "username/:username" do |username|
|
110
|
-
|
111
110
|
user = User.find_by_username(username) # username == "foobar"
|
112
111
|
|
113
112
|
# /username/foobar/posts
|
@@ -149,7 +148,6 @@ Cuba.define do
|
|
149
148
|
end
|
150
149
|
```
|
151
150
|
|
152
|
-
|
153
151
|
HTTP Verbs
|
154
152
|
----------
|
155
153
|
|
@@ -248,6 +246,123 @@ end
|
|
248
246
|
To read more about testing, check the documentation for [Cutest][cutest] and
|
249
247
|
[Capybara][capybara].
|
250
248
|
|
249
|
+
Settings
|
250
|
+
--------
|
251
|
+
|
252
|
+
_You need Cuba 3.0.0 release candidate (gem install cuba --pre)._
|
253
|
+
|
254
|
+
Each Cuba app can store settings in the `Cuba.settings` hash. The settings are
|
255
|
+
inherited if you happen to subclass `Cuba`
|
256
|
+
|
257
|
+
``` ruby
|
258
|
+
Cuba.settings[:layout] = "guest"
|
259
|
+
|
260
|
+
class Users < Cuba; end
|
261
|
+
class Admin < Cuba; end
|
262
|
+
|
263
|
+
Admin.settings[:layout] = "admin"
|
264
|
+
|
265
|
+
assert_equal "guest", Users.settings[:layout]
|
266
|
+
assert_equal "admin", Admin.settings[:layout]
|
267
|
+
|
268
|
+
```
|
269
|
+
|
270
|
+
Feel free to store whatever you find convenient.
|
271
|
+
|
272
|
+
Rendering
|
273
|
+
---------
|
274
|
+
|
275
|
+
_You need Cuba 3.0.0 release candidate (gem install cuba --pre)._
|
276
|
+
|
277
|
+
Cuba ships with a plugin that provides helpers for rendering templates. It uses
|
278
|
+
[Tilt][tilt], a gem that interfaces with many template engines.
|
279
|
+
|
280
|
+
``` ruby
|
281
|
+
require "cuba/render"
|
282
|
+
|
283
|
+
Cuba.plugin Cuba::Render
|
284
|
+
|
285
|
+
Cuba.define do
|
286
|
+
on default do
|
287
|
+
|
288
|
+
# Within the partial, you will have access to the local variable `content`,
|
289
|
+
# that will hold the value "hello, world".
|
290
|
+
res.write render("home.haml", content: "hello, world")
|
291
|
+
end
|
292
|
+
end
|
293
|
+
```
|
294
|
+
|
295
|
+
Note that in order to use this plugin you need to have [Tilt][tilt] installed, along
|
296
|
+
with the templating engines you want to use.
|
297
|
+
|
298
|
+
Plugins
|
299
|
+
-------
|
300
|
+
|
301
|
+
_You need Cuba 3.0.0 release candidate (gem install cuba --pre)._
|
302
|
+
|
303
|
+
Cuba provides a way to extend its functionality with plugins.
|
304
|
+
|
305
|
+
### How to create plugins
|
306
|
+
|
307
|
+
Authoring your own plugins is pretty straightforward.
|
308
|
+
|
309
|
+
``` ruby
|
310
|
+
module MyOwnHelper
|
311
|
+
def markdown(str)
|
312
|
+
BlueCloth.new(str).to_html
|
313
|
+
end
|
314
|
+
end
|
315
|
+
|
316
|
+
Cuba.plugin MyOwnHelper
|
317
|
+
```
|
318
|
+
|
319
|
+
That's the simplest kind of plugin you'll write. In fact, that's exactly how
|
320
|
+
the `markdown` helper is written in `Cuba::TextHelpers`.
|
321
|
+
|
322
|
+
A more complicated plugin can make use of `Cuba.settings` to provide default
|
323
|
+
values. In the following example, note that if the module has a `setup` method it will
|
324
|
+
be called as soon as it is included:
|
325
|
+
|
326
|
+
``` ruby
|
327
|
+
module Render
|
328
|
+
def self.setup(app)
|
329
|
+
app.settings[:template_engine] = "erb"
|
330
|
+
end
|
331
|
+
|
332
|
+
def partial(template, locals = {})
|
333
|
+
render("#{template}.#{settings[:template_engine]}", locals)
|
334
|
+
end
|
335
|
+
end
|
336
|
+
|
337
|
+
Cuba.plugin Render
|
338
|
+
```
|
339
|
+
|
340
|
+
This sample plugin actually resembles how `Cuba::Render` works.
|
341
|
+
|
342
|
+
Finally, if a module called `ClassMethods` is present, `Cuba` will be extended
|
343
|
+
with it.
|
344
|
+
|
345
|
+
``` ruby
|
346
|
+
module GetSetter
|
347
|
+
module ClassMethods
|
348
|
+
def set(key, value)
|
349
|
+
settings[key] = value
|
350
|
+
end
|
351
|
+
|
352
|
+
def get(key)
|
353
|
+
settings[key]
|
354
|
+
end
|
355
|
+
end
|
356
|
+
end
|
357
|
+
|
358
|
+
Cuba.plugin GetSetter
|
359
|
+
|
360
|
+
Cuba.set(:foo, "bar")
|
361
|
+
|
362
|
+
assert_equal "bar", Cuba.get(:foo)
|
363
|
+
assert_equal "bar", Cuba.settings[:foo]
|
364
|
+
```
|
365
|
+
|
251
366
|
Installation
|
252
367
|
------------
|
253
368
|
|
data/cuba.gemspec
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
Gem::Specification.new do |s|
|
2
2
|
s.name = "cuba"
|
3
|
-
s.version = "3.0.0.
|
3
|
+
s.version = "3.0.0.rc2"
|
4
4
|
s.summary = "Microframework for web applications."
|
5
5
|
s.description = "Cuba is a microframework for web applications."
|
6
6
|
s.authors = ["Michel Martens"]
|
@@ -18,7 +18,6 @@ Gem::Specification.new do |s|
|
|
18
18
|
]
|
19
19
|
|
20
20
|
s.add_dependency "rack"
|
21
|
-
s.add_dependency "tilt"
|
22
21
|
s.add_development_dependency "cutest"
|
23
22
|
s.add_development_dependency "capybara"
|
24
23
|
end
|
data/lib/cuba.rb
CHANGED
data/lib/cuba/render.rb
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
require "tilt"
|
2
|
+
|
3
|
+
class Cuba
|
4
|
+
module Render
|
5
|
+
def self.setup(app)
|
6
|
+
app.settings[:template_engine] ||= "erb"
|
7
|
+
app.settings[:views] ||= File.expand_path("views", Dir.pwd)
|
8
|
+
end
|
9
|
+
|
10
|
+
def view(template, locals = {}, layout = "layout")
|
11
|
+
partial(layout, { content: partial(template, locals) }.merge(locals))
|
12
|
+
end
|
13
|
+
|
14
|
+
def partial(template, locals = {})
|
15
|
+
render("#{settings[:views]}/#{template}.#{settings[:template_engine]}",
|
16
|
+
locals, default_encoding: Encoding.default_external)
|
17
|
+
end
|
18
|
+
|
19
|
+
# Render any type of template file supported by Tilt.
|
20
|
+
#
|
21
|
+
# @example
|
22
|
+
#
|
23
|
+
# # Renders home, and is assumed to be HAML.
|
24
|
+
# render("home.haml")
|
25
|
+
#
|
26
|
+
# # Renders with some local variables
|
27
|
+
# render("home.haml", site_name: "My Site")
|
28
|
+
#
|
29
|
+
# # Renders with HAML options
|
30
|
+
# render("home.haml", {}, ugly: true, format: :html5)
|
31
|
+
#
|
32
|
+
# # Renders in layout
|
33
|
+
# render("layout.haml") { render("home.haml") }
|
34
|
+
#
|
35
|
+
def render(template, locals = {}, options = {}, &block)
|
36
|
+
_cache.fetch(template) {
|
37
|
+
Tilt.new(template, 1, options)
|
38
|
+
}.render(self, locals, &block)
|
39
|
+
end
|
40
|
+
|
41
|
+
# @private Used internally by #render to cache the
|
42
|
+
# Tilt templates.
|
43
|
+
def _cache
|
44
|
+
Thread.current[:_cache] ||= Tilt::Cache.new
|
45
|
+
end
|
46
|
+
private :_cache
|
47
|
+
end
|
48
|
+
end
|
data/test/render.rb
ADDED
@@ -0,0 +1,94 @@
|
|
1
|
+
require_relative "helper"
|
2
|
+
|
3
|
+
require "cuba/render"
|
4
|
+
|
5
|
+
test "doesn't override the settings if they already exist" do
|
6
|
+
Cuba.settings[:views] = "./test/views"
|
7
|
+
Cuba.settings[:template_engine] = "haml"
|
8
|
+
|
9
|
+
Cuba.plugin Cuba::Render
|
10
|
+
|
11
|
+
assert_equal "./test/views", Cuba.settings[:views]
|
12
|
+
assert_equal "haml", Cuba.settings[:template_engine]
|
13
|
+
end
|
14
|
+
|
15
|
+
scope do
|
16
|
+
setup do
|
17
|
+
Cuba.plugin Cuba::Render
|
18
|
+
Cuba.settings[:views] = "./test/views"
|
19
|
+
Cuba.settings[:template_engine] = "erb"
|
20
|
+
|
21
|
+
Cuba.define do
|
22
|
+
on "home" do
|
23
|
+
res.write view("home", name: "Agent Smith", title: "Home")
|
24
|
+
end
|
25
|
+
|
26
|
+
on "about" do
|
27
|
+
res.write partial("about", title: "About Cuba")
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
test "partial" do
|
33
|
+
_, _, body = Cuba.call({ "PATH_INFO" => "/about", "SCRIPT_NAME" => "/" })
|
34
|
+
|
35
|
+
assert_response body, ["<h1>About Cuba</h1>\n"]
|
36
|
+
end
|
37
|
+
|
38
|
+
test "view" do
|
39
|
+
_, _, body = Cuba.call({ "PATH_INFO" => "/home", "SCRIPT_NAME" => "/" })
|
40
|
+
|
41
|
+
assert_response body, ["<title>Cuba: Home</title>\n<h1>Home</h1>\n<p>Hello Agent Smith</p>\n"]
|
42
|
+
end
|
43
|
+
|
44
|
+
test "partial with str as engine" do
|
45
|
+
Cuba.settings[:template_engine] = "str"
|
46
|
+
|
47
|
+
_, _, body = Cuba.call({ "PATH_INFO" => "/about", "SCRIPT_NAME" => "/" })
|
48
|
+
|
49
|
+
assert_response body, ["<h1>About Cuba</h1>\n"]
|
50
|
+
end
|
51
|
+
|
52
|
+
test "view with str as engine" do
|
53
|
+
Cuba.settings[:template_engine] = "str"
|
54
|
+
|
55
|
+
_, _, body = Cuba.call({ "PATH_INFO" => "/home", "SCRIPT_NAME" => "/" })
|
56
|
+
|
57
|
+
assert_response body, ["<title>Cuba: Home</title>\n<h1>Home</h1>\n<p>Hello Agent Smith</p>\n\n"]
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
test "caching behavior" do
|
62
|
+
Thread.current[:_cache] = nil
|
63
|
+
|
64
|
+
Cuba.plugin Cuba::Render
|
65
|
+
Cuba.settings[:views] = "./test/views"
|
66
|
+
|
67
|
+
Cuba.define do
|
68
|
+
on "foo/:i" do |i|
|
69
|
+
res.write partial("test", title: i)
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
10.times do |i|
|
74
|
+
_, _, resp = Cuba.call({ "PATH_INFO" => "/foo/#{i}", "SCRIPT_NAME" => "" })
|
75
|
+
end
|
76
|
+
|
77
|
+
assert_equal 1, Thread.current[:_cache].instance_variable_get(:@cache).size
|
78
|
+
end
|
79
|
+
|
80
|
+
test "simple layout support" do
|
81
|
+
Cuba.plugin Cuba::Render
|
82
|
+
|
83
|
+
Cuba.define do
|
84
|
+
on true do
|
85
|
+
res.write render("test/views/layout-yield.erb") {
|
86
|
+
render("test/views/content-yield.erb")
|
87
|
+
}
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
_, _, resp = Cuba.call({})
|
92
|
+
|
93
|
+
assert_equal ["Header\nThis is the actual content.\nFooter\n"], resp.body
|
94
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cuba
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 3.0.0.
|
4
|
+
version: 3.0.0.rc2
|
5
5
|
prerelease: 6
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-02-
|
12
|
+
date: 2012-02-19 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rack
|
16
|
-
requirement: &
|
16
|
+
requirement: &2152217600 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,21 +21,10 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
25
|
-
- !ruby/object:Gem::Dependency
|
26
|
-
name: tilt
|
27
|
-
requirement: &2160203660 !ruby/object:Gem::Requirement
|
28
|
-
none: false
|
29
|
-
requirements:
|
30
|
-
- - ! '>='
|
31
|
-
- !ruby/object:Gem::Version
|
32
|
-
version: '0'
|
33
|
-
type: :runtime
|
34
|
-
prerelease: false
|
35
|
-
version_requirements: *2160203660
|
24
|
+
version_requirements: *2152217600
|
36
25
|
- !ruby/object:Gem::Dependency
|
37
26
|
name: cutest
|
38
|
-
requirement: &
|
27
|
+
requirement: &2152216880 !ruby/object:Gem::Requirement
|
39
28
|
none: false
|
40
29
|
requirements:
|
41
30
|
- - ! '>='
|
@@ -43,10 +32,10 @@ dependencies:
|
|
43
32
|
version: '0'
|
44
33
|
type: :development
|
45
34
|
prerelease: false
|
46
|
-
version_requirements: *
|
35
|
+
version_requirements: *2152216880
|
47
36
|
- !ruby/object:Gem::Dependency
|
48
37
|
name: capybara
|
49
|
-
requirement: &
|
38
|
+
requirement: &2152216340 !ruby/object:Gem::Requirement
|
50
39
|
none: false
|
51
40
|
requirements:
|
52
41
|
- - ! '>='
|
@@ -54,7 +43,7 @@ dependencies:
|
|
54
43
|
version: '0'
|
55
44
|
type: :development
|
56
45
|
prerelease: false
|
57
|
-
version_requirements: *
|
46
|
+
version_requirements: *2152216340
|
58
47
|
description: Cuba is a microframework for web applications.
|
59
48
|
email:
|
60
49
|
- michel@soveran.com
|
@@ -66,7 +55,7 @@ files:
|
|
66
55
|
- CHANGELOG
|
67
56
|
- README.markdown
|
68
57
|
- Rakefile
|
69
|
-
- lib/cuba/
|
58
|
+
- lib/cuba/render.rb
|
70
59
|
- lib/cuba/test.rb
|
71
60
|
- lib/cuba.rb
|
72
61
|
- cuba.gemspec
|
@@ -85,6 +74,7 @@ files:
|
|
85
74
|
- test/path.rb
|
86
75
|
- test/plugin.rb
|
87
76
|
- test/redefinition.rb
|
77
|
+
- test/render.rb
|
88
78
|
- test/root.rb
|
89
79
|
- test/run.rb
|
90
80
|
- test/segment.rb
|
data/lib/cuba/settings.rb
DELETED
@@ -1,25 +0,0 @@
|
|
1
|
-
class Cuba
|
2
|
-
module Settings
|
3
|
-
def settings
|
4
|
-
self.class
|
5
|
-
end
|
6
|
-
|
7
|
-
module ClassMethods
|
8
|
-
def set(key, value)
|
9
|
-
metaclass.send :attr_writer, key
|
10
|
-
metaclass.module_eval %{
|
11
|
-
def #{key}
|
12
|
-
@#{key} ||= #{value.inspect}
|
13
|
-
end
|
14
|
-
}
|
15
|
-
|
16
|
-
send :"#{key}=", value
|
17
|
-
end
|
18
|
-
|
19
|
-
private
|
20
|
-
def metaclass
|
21
|
-
class << self; self; end
|
22
|
-
end
|
23
|
-
end
|
24
|
-
end
|
25
|
-
end
|