cuba-contrib 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +19 -0
- data/README.markdown +142 -0
- data/cuba-contrib.gemspec +24 -0
- data/lib/cuba/contrib.rb +22 -0
- data/lib/cuba/contrib/form_helpers.rb +107 -0
- data/lib/cuba/contrib/mote.rb +49 -0
- data/lib/cuba/contrib/prelude.rb +14 -0
- data/lib/cuba/contrib/rendering.rb +41 -0
- data/lib/cuba/contrib/settings.rb +20 -0
- data/lib/cuba/contrib/text_helpers.rb +35 -0
- data/lib/cuba/contrib/version.rb +5 -0
- data/rakefile +7 -0
- data/test/contrib.rb +68 -0
- data/test/helper.rb +11 -0
- data/test/mote.rb +37 -0
- data/test/rendering.rb +42 -0
- data/test/settings.rb +24 -0
- data/test/text_helper.rb +27 -0
- metadata +96 -0
data/LICENSE
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
Copyright (c) 2010, 2011 Michel Martens, Damian Janowski and Cyril David
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
+
of this software and associated documentation files (the "Software"), to deal
|
5
|
+
in the Software without restriction, including without limitation the rights
|
6
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
+
copies of the Software, and to permit persons to whom the Software is
|
8
|
+
furnished to do so, subject to the following conditions:
|
9
|
+
|
10
|
+
The above copyright notice and this permission notice shall be included in
|
11
|
+
all copies or substantial portions of the Software.
|
12
|
+
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
|
+
THE SOFTWARE.
|
data/README.markdown
ADDED
@@ -0,0 +1,142 @@
|
|
1
|
+
# cuba-contrib
|
2
|
+
|
3
|
+
[Cuba][cuba] is probably one of the tiniest rack-based micro
|
4
|
+
frameworks around. Weighing in at only __138 LOC__, it has proven
|
5
|
+
itself to be a very resilient tool in various web application domains.
|
6
|
+
Check [the list of sites][sites] built using Cuba in order to
|
7
|
+
grasp the endless possibilities.
|
8
|
+
|
9
|
+
## STEP 1: Cuba::Prelude
|
10
|
+
|
11
|
+
Cuba [does one thing, and it does it well][unix]. Cuba-contrib, on
|
12
|
+
the other hand, layers requirement-specific functionality on
|
13
|
+
top of it. This allows us to build simpler and lighter solutions.
|
14
|
+
|
15
|
+
To get started with `Cuba::Contrib`, install it using RubyGems:
|
16
|
+
|
17
|
+
``` bash
|
18
|
+
$ gem install cuba # if you haven't already done so
|
19
|
+
$ gem install cuba-contrib
|
20
|
+
```
|
21
|
+
|
22
|
+
For the remainder of the examples below, we'll assume you
|
23
|
+
always put your main cuba application in `app.rb` and your
|
24
|
+
views in `views`.
|
25
|
+
|
26
|
+
``` bash
|
27
|
+
$ touch app.rb
|
28
|
+
$ mkdir views
|
29
|
+
```
|
30
|
+
|
31
|
+
Now you can require it in your application
|
32
|
+
|
33
|
+
``` ruby
|
34
|
+
require "cuba"
|
35
|
+
require "cuba/contrib"
|
36
|
+
|
37
|
+
Cuba.plugin Cuba::Prelude
|
38
|
+
```
|
39
|
+
|
40
|
+
`Cuba::Prelude` adds the basic stuff you'll need:
|
41
|
+
|
42
|
+
``` ruby
|
43
|
+
Cuba.define do
|
44
|
+
on "about" do
|
45
|
+
# same as encodeURIComponent in javascript land
|
46
|
+
res.write urlencode("http://www.google.com")
|
47
|
+
|
48
|
+
# basically an alias for Rack::Utils.escape_html
|
49
|
+
res.write h("Cuba & Cuba Contrib")
|
50
|
+
end
|
51
|
+
end
|
52
|
+
```
|
53
|
+
|
54
|
+
## STEP 2: Choose your templating
|
55
|
+
|
56
|
+
### Here comes a new challenger: Mote
|
57
|
+
|
58
|
+
We prefer to use our home-grown templating engine called
|
59
|
+
[Mote][mote]. We do that by simply loading the plugin `Cuba::Mote`:
|
60
|
+
|
61
|
+
``` ruby
|
62
|
+
require "cuba"
|
63
|
+
require "cuba/contrib"
|
64
|
+
|
65
|
+
Cuba.plugin Cuba::Mote
|
66
|
+
|
67
|
+
Cuba.define do
|
68
|
+
on "home" do
|
69
|
+
res.write view("home")
|
70
|
+
end
|
71
|
+
|
72
|
+
on "about" do
|
73
|
+
res.write partial("about")
|
74
|
+
end
|
75
|
+
end
|
76
|
+
```
|
77
|
+
|
78
|
+
This assumes that you have a `views` folder, containing a `home.mote`
|
79
|
+
and an `about.mote`.
|
80
|
+
|
81
|
+
### Classic templating needs
|
82
|
+
|
83
|
+
``` ruby
|
84
|
+
require "cuba"
|
85
|
+
require "cuba/contrib"
|
86
|
+
|
87
|
+
Cuba.plugin Cuba::Rendering
|
88
|
+
Cuba.set :template_engine, "haml"
|
89
|
+
|
90
|
+
Cuba.define do
|
91
|
+
on "home" do
|
92
|
+
res.write view("home") # renders views/home.haml
|
93
|
+
end
|
94
|
+
|
95
|
+
on "about" do
|
96
|
+
res.write partial("about") # renders views/about.haml
|
97
|
+
end
|
98
|
+
end
|
99
|
+
```
|
100
|
+
|
101
|
+
## STEP 3: Make your own plugins
|
102
|
+
|
103
|
+
Authoring your own plugins is pretty straightforward.
|
104
|
+
|
105
|
+
``` ruby
|
106
|
+
module MyOwnHelper
|
107
|
+
def markdown(str)
|
108
|
+
BlueCloth.new(str).to_html
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
Cuba.plugin MyOwnHelper
|
113
|
+
```
|
114
|
+
|
115
|
+
that's the simplest kind of plugin you'll write. In fact,
|
116
|
+
that's exactly how the `markdown` helper is written in
|
117
|
+
`Cuba::TextHelpers`.
|
118
|
+
|
119
|
+
A more complicated plugin for example, will make use of
|
120
|
+
`Cuba::Settings` to provide default values:
|
121
|
+
|
122
|
+
``` ruby
|
123
|
+
module Rendering
|
124
|
+
def self.setup(app)
|
125
|
+
app.plugin Cuba::Settings # we need this for default values
|
126
|
+
app.set :template_engine, "erb"
|
127
|
+
end
|
128
|
+
|
129
|
+
def partial(template, locals = {})
|
130
|
+
render("#{template}.#{settings.template_engine}", locals)
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
134
|
+
Cuba.plugin Rendering
|
135
|
+
```
|
136
|
+
|
137
|
+
This sample plugin actually resembles how `Cuba::Rendering` works.
|
138
|
+
|
139
|
+
[cuba]: http://cuba.is
|
140
|
+
[sites]: http://cuba.is/sites
|
141
|
+
[mote]: http://github.com/soveran/mote
|
142
|
+
[unix]: http://en.wikipedia.org/wiki/Unix_philosophy
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require "./lib/cuba/contrib/version"
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = "cuba-contrib"
|
5
|
+
s.version = Cuba::Contrib::VERSION
|
6
|
+
s.summary = "Cuba plugins and utilities."
|
7
|
+
s.description = "Includes various helper tools for Cuba."
|
8
|
+
s.authors = ["Cyril David"]
|
9
|
+
s.email = ["me@cyrildavid.com"]
|
10
|
+
s.homepage = "http://github.com/cyx/cuba-contrib"
|
11
|
+
|
12
|
+
s.files = Dir[
|
13
|
+
"LICENSE",
|
14
|
+
"README.markdown",
|
15
|
+
"rakefile",
|
16
|
+
"lib/**/*.rb",
|
17
|
+
"*.gemspec",
|
18
|
+
"test/*.*"
|
19
|
+
]
|
20
|
+
|
21
|
+
s.add_dependency "cuba"
|
22
|
+
s.add_development_dependency "cutest"
|
23
|
+
s.add_development_dependency "capybara"
|
24
|
+
end
|
data/lib/cuba/contrib.rb
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
class Cuba
|
2
|
+
CONTRIB_ROOT = File.expand_path("../../", File.dirname(__FILE__))
|
3
|
+
|
4
|
+
def self.plugin(mixin)
|
5
|
+
include mixin
|
6
|
+
extend mixin::ClassMethods if defined?(mixin::ClassMethods)
|
7
|
+
|
8
|
+
mixin.setup(self) if mixin.respond_to?(:setup)
|
9
|
+
end
|
10
|
+
|
11
|
+
# TODO: remove this as soon as cuba core implements this.
|
12
|
+
def session
|
13
|
+
env["rack.session"]
|
14
|
+
end
|
15
|
+
|
16
|
+
autoload :Prelude, "cuba/contrib/prelude"
|
17
|
+
autoload :Rendering, "cuba/contrib/rendering"
|
18
|
+
autoload :Settings, "cuba/contrib/settings"
|
19
|
+
autoload :Mote, "cuba/contrib/mote"
|
20
|
+
autoload :TextHelpers, "cuba/contrib/text_helpers"
|
21
|
+
autoload :FormHelpers, "cuba/contrib/form_helpers"
|
22
|
+
end
|
@@ -0,0 +1,107 @@
|
|
1
|
+
class Cuba
|
2
|
+
module FormHelpers
|
3
|
+
include Cuba::Prelude
|
4
|
+
|
5
|
+
def cuba_contrib_partial(template, locals = {})
|
6
|
+
partial(cuba_contrib_path("%s.mote" % template), locals)
|
7
|
+
end
|
8
|
+
|
9
|
+
def cuba_contrib_path(*args)
|
10
|
+
File.join(Cuba::CONTRIB_ROOT, "views", *args)
|
11
|
+
end
|
12
|
+
|
13
|
+
def cuba_contrib_form(record)
|
14
|
+
view(cuba_contrib_path("form"), model: record)
|
15
|
+
end
|
16
|
+
|
17
|
+
def select_options(pairs, selected = nil, prompt = nil)
|
18
|
+
"".tap do |ret|
|
19
|
+
ret << option_tag(prompt, "") if prompt
|
20
|
+
|
21
|
+
pairs.each do |label, value|
|
22
|
+
ret << option_tag(label, value, selected)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def option_tag(label, value, selected = nil)
|
28
|
+
cuba_contrib_partial("option", selected: selected, value: value, label: label)
|
29
|
+
end
|
30
|
+
|
31
|
+
def datefield(model, field, hint = nil)
|
32
|
+
input(model, field, hint) do
|
33
|
+
cuba_contrib_partial("textfield",
|
34
|
+
name: field_name(model, field),
|
35
|
+
value: model.send(field),
|
36
|
+
class: "date"
|
37
|
+
)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def textfield(model, field, hint = nil)
|
42
|
+
input(model, field, hint) do
|
43
|
+
cuba_contrib_partial("textfield",
|
44
|
+
name: field_name(model, field),
|
45
|
+
value: model.send(field)
|
46
|
+
)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def password_field(model, field, hint = nil)
|
51
|
+
input(model, field, hint) do
|
52
|
+
cuba_contrib_partial("password",
|
53
|
+
name: field_name(model, field)
|
54
|
+
)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
def filefield(model, field, hint = nil)
|
59
|
+
input(model, field, hint) do
|
60
|
+
cuba_contrib_partial("file",
|
61
|
+
name: field_name(model, field)
|
62
|
+
)
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
def textarea(model, field, hint = nil)
|
67
|
+
input(model, field, hint) do
|
68
|
+
cuba_contrib_partial("textarea",
|
69
|
+
name: field_name(model, field),
|
70
|
+
value: model.send(field)
|
71
|
+
)
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
def dropdown(model, field, options, hint = nil)
|
76
|
+
input(model, field, hint) do
|
77
|
+
cuba_contrib_partial("select",
|
78
|
+
name: field_name(model, field),
|
79
|
+
options: select_options(options, model.send(field))
|
80
|
+
)
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
def input(model, field, hint)
|
85
|
+
cuba_contrib_partial("field",
|
86
|
+
label: humanize(field),
|
87
|
+
input: yield,
|
88
|
+
hint: hint,
|
89
|
+
error: localize_errors(model, field).join(", ")
|
90
|
+
)
|
91
|
+
end
|
92
|
+
|
93
|
+
def localize_errors(model, field)
|
94
|
+
model.errors[field].map do |err|
|
95
|
+
unless settings.localized_errors[err]
|
96
|
+
raise "No localized error defined for: #{err}"
|
97
|
+
end
|
98
|
+
|
99
|
+
settings.localized_errors[err] % { field: humanize(field) }
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
def field_name(model, field)
|
104
|
+
"#{underscore(model.class.name)}[#{field}]"
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
require "mote"
|
2
|
+
|
3
|
+
class Cuba
|
4
|
+
module Mote
|
5
|
+
include ::Mote::Helpers
|
6
|
+
|
7
|
+
def self.setup(app)
|
8
|
+
app.plugin Cuba::Settings
|
9
|
+
|
10
|
+
app.set :views, File.expand_path("views", Dir.pwd)
|
11
|
+
app.set :layout, "layout"
|
12
|
+
end
|
13
|
+
|
14
|
+
def partial(template, locals = {})
|
15
|
+
mote(mote_path(template), locals)
|
16
|
+
end
|
17
|
+
|
18
|
+
def view(template, locals = {})
|
19
|
+
raise NoLayout.new(self) unless settings.layout
|
20
|
+
|
21
|
+
partial(
|
22
|
+
settings.layout,
|
23
|
+
locals.merge(mote_vars(partial(template, locals)))
|
24
|
+
)
|
25
|
+
end
|
26
|
+
|
27
|
+
def mote_path(template)
|
28
|
+
return template if template.end_with?(".mote")
|
29
|
+
|
30
|
+
File.expand_path("#{template}.mote", settings.views)
|
31
|
+
end
|
32
|
+
|
33
|
+
def mote_vars(content)
|
34
|
+
{ content: content, session: session }
|
35
|
+
end
|
36
|
+
|
37
|
+
class NoLayout < StandardError
|
38
|
+
attr :instance
|
39
|
+
|
40
|
+
def initialize(instance)
|
41
|
+
@instance = instance
|
42
|
+
end
|
43
|
+
|
44
|
+
def message
|
45
|
+
"Missing Layout: Try doing #{instance.class}.set :layout, 'layout'"
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require "tilt"
|
2
|
+
|
3
|
+
class Cuba
|
4
|
+
module Rendering
|
5
|
+
def self.setup(app)
|
6
|
+
app.plugin Cuba::Settings
|
7
|
+
app.set :template_engine, "erb"
|
8
|
+
app.set :views, File.expand_path("views", Dir.pwd)
|
9
|
+
end
|
10
|
+
|
11
|
+
def view(template, locals = {})
|
12
|
+
partial("layout", { content: partial(template, locals) }.merge(locals))
|
13
|
+
end
|
14
|
+
|
15
|
+
def partial(template, locals = {})
|
16
|
+
render("#{settings.views}/#{template}.#{settings.template_engine}", locals)
|
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, locals) {
|
37
|
+
Tilt.new(template, 1, options)
|
38
|
+
}.render(self, locals, &block)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,20 @@
|
|
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_accessor, key
|
10
|
+
|
11
|
+
send :"#{key}=", value
|
12
|
+
end
|
13
|
+
|
14
|
+
private
|
15
|
+
def metaclass
|
16
|
+
class << self; self; end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
class Cuba
|
2
|
+
module TextHelpers
|
3
|
+
def markdown(str)
|
4
|
+
require "bluecloth"
|
5
|
+
|
6
|
+
BlueCloth.new(str).to_html
|
7
|
+
end
|
8
|
+
|
9
|
+
def truncate(str, length, ellipses = "...")
|
10
|
+
return str if str.length <= length
|
11
|
+
|
12
|
+
sprintf("%.#{length}s#{ellipses}", str)
|
13
|
+
end
|
14
|
+
|
15
|
+
def nl2br(str)
|
16
|
+
str.gsub(/\n|\r\n/, "<br>")
|
17
|
+
end
|
18
|
+
|
19
|
+
def currency(amount, unit = "$")
|
20
|
+
"#{unit} %.2f" % amount
|
21
|
+
end
|
22
|
+
|
23
|
+
def titlecase(str)
|
24
|
+
str.to_s.tr("_", " ").gsub(/(^|\s)([a-z])/) { |char| char.upcase }
|
25
|
+
end
|
26
|
+
|
27
|
+
def humanize(str)
|
28
|
+
titlecase(str.to_s.tr("_", " ").gsub(/_id$/, ""))
|
29
|
+
end
|
30
|
+
|
31
|
+
def underscore(str)
|
32
|
+
str.gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').downcase
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
data/rakefile
ADDED
data/test/contrib.rb
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
require_relative "helper"
|
2
|
+
|
3
|
+
scope do
|
4
|
+
module Helper
|
5
|
+
def clean(str)
|
6
|
+
str.strip
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
Cuba.plugin Helper
|
11
|
+
|
12
|
+
Cuba.define do
|
13
|
+
on default do
|
14
|
+
res.write clean " foo "
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
test do
|
19
|
+
_, _, body = Cuba.call({ "PATH_INFO" => "", "SCRIPT_NAME" => "" })
|
20
|
+
|
21
|
+
assert_response body, ["foo"]
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
scope do
|
26
|
+
module Number
|
27
|
+
def num
|
28
|
+
1
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
module Plugin
|
33
|
+
def self.setup(app)
|
34
|
+
app.plugin Number
|
35
|
+
end
|
36
|
+
|
37
|
+
def bar
|
38
|
+
"baz"
|
39
|
+
end
|
40
|
+
|
41
|
+
module ClassMethods
|
42
|
+
def foo
|
43
|
+
"bar"
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
Cuba.reset!
|
49
|
+
|
50
|
+
Cuba.plugin Plugin
|
51
|
+
|
52
|
+
Cuba.define do
|
53
|
+
on default do
|
54
|
+
res.write bar
|
55
|
+
res.write num
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
test do
|
60
|
+
assert_equal "bar", Cuba.foo
|
61
|
+
end
|
62
|
+
|
63
|
+
test do
|
64
|
+
_, _, body = Cuba.call({ "PATH_INFO" => "", "SCRIPT_NAME" => "" })
|
65
|
+
|
66
|
+
assert_response body, ["baz", "1"]
|
67
|
+
end
|
68
|
+
end
|
data/test/helper.rb
ADDED
data/test/mote.rb
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
require_relative "helper"
|
2
|
+
|
3
|
+
Cuba.plugin Cuba::Mote
|
4
|
+
|
5
|
+
Cuba.set :views, "./test/views"
|
6
|
+
|
7
|
+
Cuba.define do
|
8
|
+
on "frag" do
|
9
|
+
res.write partial("frag", foo: "Bar")
|
10
|
+
end
|
11
|
+
|
12
|
+
on "" do
|
13
|
+
res.write view("home", title: "Hola")
|
14
|
+
end
|
15
|
+
|
16
|
+
on "abs_path" do
|
17
|
+
res.write view("./test/views/custom/abs_path.mote", title: "Absolute")
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
test do
|
22
|
+
_, _, body = Cuba.call({ "PATH_INFO" => "/", "SCRIPT_NAME" => "" })
|
23
|
+
|
24
|
+
assert_response body, ["<title>Hola</title>\n<h1>Home</h1>"]
|
25
|
+
end
|
26
|
+
|
27
|
+
test do
|
28
|
+
_, _, body = Cuba.call({ "PATH_INFO" => "/frag", "SCRIPT_NAME" => "" })
|
29
|
+
|
30
|
+
assert_response body, ["<h1>Bar</h1>"]
|
31
|
+
end
|
32
|
+
|
33
|
+
test do
|
34
|
+
_, _, body = Cuba.call({ "PATH_INFO" => "/abs_path", "SCRIPT_NAME" => "" })
|
35
|
+
|
36
|
+
assert_response body, ["<title>Absolute</title>\n<h1>Abs Path</h1>"]
|
37
|
+
end
|
data/test/rendering.rb
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
require_relative "helper"
|
2
|
+
|
3
|
+
Cuba.plugin Cuba::Rendering
|
4
|
+
Cuba.views = "./test/views"
|
5
|
+
|
6
|
+
Cuba.define do
|
7
|
+
on "home" do
|
8
|
+
res.write view("home", name: "Agent Smith", title: "Home")
|
9
|
+
end
|
10
|
+
|
11
|
+
on "about" do
|
12
|
+
res.write partial("about", title: "About Cuba")
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
test "partial" do
|
17
|
+
_, _, body = Cuba.call({ "PATH_INFO" => "/about", "SCRIPT_NAME" => "/" })
|
18
|
+
|
19
|
+
assert_response body, ["<h1>About Cuba</h1>"]
|
20
|
+
end
|
21
|
+
|
22
|
+
test "view" do
|
23
|
+
_, _, body = Cuba.call({ "PATH_INFO" => "/home", "SCRIPT_NAME" => "/" })
|
24
|
+
|
25
|
+
assert_response body, ["<title>Cuba: Home</title>\n<h1>Home</h1>\n<p>Hello Agent Smith</p>"]
|
26
|
+
end
|
27
|
+
|
28
|
+
test "partial with str as engine" do
|
29
|
+
Cuba.template_engine = "str"
|
30
|
+
|
31
|
+
_, _, body = Cuba.call({ "PATH_INFO" => "/about", "SCRIPT_NAME" => "/" })
|
32
|
+
|
33
|
+
assert_response body, ["<h1>About Cuba</h1>"]
|
34
|
+
end
|
35
|
+
|
36
|
+
test "view with str as engine" do
|
37
|
+
Cuba.template_engine = "str"
|
38
|
+
|
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>"]
|
42
|
+
end
|
data/test/settings.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
require_relative "helper"
|
2
|
+
|
3
|
+
Cuba.plugin Cuba::Settings
|
4
|
+
Cuba.set :foo, "bar"
|
5
|
+
|
6
|
+
Cuba.define do
|
7
|
+
on default do
|
8
|
+
res.write settings.foo
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
test do
|
13
|
+
assert_equal "bar", Cuba.foo
|
14
|
+
end
|
15
|
+
|
16
|
+
test do
|
17
|
+
_, _, body = Cuba.call({ "PATH_INFO" => "", "SCRIPT_NAME" => "" })
|
18
|
+
assert_response body, ["bar"]
|
19
|
+
end
|
20
|
+
|
21
|
+
test do
|
22
|
+
Cuba.foo = "baz"
|
23
|
+
assert_equal "baz", Cuba.foo
|
24
|
+
end
|
data/test/text_helper.rb
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require_relative "helper"
|
4
|
+
|
5
|
+
setup do
|
6
|
+
obj = Object.new
|
7
|
+
obj.extend Cuba::TextHelpers
|
8
|
+
end
|
9
|
+
|
10
|
+
test "truncate" do |helper|
|
11
|
+
assert_equal "the q...", helper.truncate("the quick brown", 5)
|
12
|
+
|
13
|
+
assert_equal "same string", helper.truncate("same string", 11)
|
14
|
+
end
|
15
|
+
|
16
|
+
test "markdown" do |helper|
|
17
|
+
assert_equal "<h1>Hola Mundo</h1>", helper.markdown("# Hola Mundo")
|
18
|
+
end
|
19
|
+
|
20
|
+
test "nl2br" do |helper|
|
21
|
+
assert_equal "Hi<br>there<br>Joe", helper.nl2br("Hi\nthere\r\nJoe")
|
22
|
+
end
|
23
|
+
|
24
|
+
test "currency" do |helper|
|
25
|
+
assert_equal "$ 2.22", helper.currency(2.221)
|
26
|
+
assert_equal "€ 2.22", helper.currency(2.221, "€")
|
27
|
+
end
|
metadata
ADDED
@@ -0,0 +1,96 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: cuba-contrib
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Cyril David
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-11-04 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: cuba
|
16
|
+
requirement: &2152741820 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *2152741820
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: cutest
|
27
|
+
requirement: &2152741300 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
type: :development
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *2152741300
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: capybara
|
38
|
+
requirement: &2152740680 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
type: :development
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *2152740680
|
47
|
+
description: Includes various helper tools for Cuba.
|
48
|
+
email:
|
49
|
+
- me@cyrildavid.com
|
50
|
+
executables: []
|
51
|
+
extensions: []
|
52
|
+
extra_rdoc_files: []
|
53
|
+
files:
|
54
|
+
- LICENSE
|
55
|
+
- README.markdown
|
56
|
+
- rakefile
|
57
|
+
- lib/cuba/contrib/form_helpers.rb
|
58
|
+
- lib/cuba/contrib/mote.rb
|
59
|
+
- lib/cuba/contrib/prelude.rb
|
60
|
+
- lib/cuba/contrib/rendering.rb
|
61
|
+
- lib/cuba/contrib/settings.rb
|
62
|
+
- lib/cuba/contrib/text_helpers.rb
|
63
|
+
- lib/cuba/contrib/version.rb
|
64
|
+
- lib/cuba/contrib.rb
|
65
|
+
- cuba-contrib.gemspec
|
66
|
+
- test/contrib.rb
|
67
|
+
- test/helper.rb
|
68
|
+
- test/mote.rb
|
69
|
+
- test/rendering.rb
|
70
|
+
- test/settings.rb
|
71
|
+
- test/text_helper.rb
|
72
|
+
homepage: http://github.com/cyx/cuba-contrib
|
73
|
+
licenses: []
|
74
|
+
post_install_message:
|
75
|
+
rdoc_options: []
|
76
|
+
require_paths:
|
77
|
+
- lib
|
78
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
79
|
+
none: false
|
80
|
+
requirements:
|
81
|
+
- - ! '>='
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
version: '0'
|
84
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
85
|
+
none: false
|
86
|
+
requirements:
|
87
|
+
- - ! '>='
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
requirements: []
|
91
|
+
rubyforge_project:
|
92
|
+
rubygems_version: 1.8.11
|
93
|
+
signing_key:
|
94
|
+
specification_version: 3
|
95
|
+
summary: Cuba plugins and utilities.
|
96
|
+
test_files: []
|