cuba-contrib 3.1.0.rc1 → 3.1.0.rc2
Sign up to get free protection for your applications and to get access to all the features.
- data/cuba-contrib.gemspec +3 -4
- data/lib/cuba/contrib.rb +4 -7
- data/lib/cuba/{contrib/mote.rb → mote.rb} +2 -0
- data/lib/cuba/{contrib/prelude.rb → prelude.rb} +0 -0
- data/lib/cuba/{contrib/text_helpers.rb → text_helpers.rb} +13 -8
- data/lib/cuba/{contrib/with.rb → with.rb} +1 -1
- data/test/text_helpers.rb +46 -0
- data/test/with.rb +45 -0
- metadata +10 -18
- data/lib/cuba/contrib/form_helpers.rb +0 -107
- data/test/text_helper.rb +0 -27
- data/test/vars.rb +0 -32
- data/views/field.mote +0 -13
- data/views/file.mote +0 -1
- data/views/option.mote +0 -1
- data/views/password.mote +0 -1
- data/views/select.mote +0 -3
- data/views/textarea.mote +0 -1
- data/views/textfield.mote +0 -1
data/cuba-contrib.gemspec
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
Gem::Specification.new do |s|
|
2
2
|
s.name = "cuba-contrib"
|
3
|
-
s.version = "3.1.0.
|
3
|
+
s.version = "3.1.0.rc2"
|
4
4
|
s.summary = "Cuba plugins and utilities."
|
5
5
|
s.description = "Includes various helper tools for Cuba."
|
6
6
|
s.authors = ["Cyril David"]
|
@@ -13,11 +13,10 @@ Gem::Specification.new do |s|
|
|
13
13
|
"rakefile",
|
14
14
|
"lib/**/*.rb",
|
15
15
|
"*.gemspec",
|
16
|
-
"test/*.*"
|
17
|
-
"views/*.mote"
|
16
|
+
"test/*.*"
|
18
17
|
]
|
19
18
|
|
20
|
-
s.add_dependency "cuba", "3.0.
|
19
|
+
s.add_dependency "cuba", "3.1.0.rc2"
|
21
20
|
s.add_development_dependency "cutest"
|
22
21
|
s.add_development_dependency "capybara"
|
23
22
|
end
|
data/lib/cuba/contrib.rb
CHANGED
@@ -1,9 +1,6 @@
|
|
1
1
|
class Cuba
|
2
|
-
|
3
|
-
|
4
|
-
require "cuba/
|
5
|
-
require "cuba/
|
6
|
-
require "cuba/contrib/text_helpers"
|
7
|
-
require "cuba/contrib/form_helpers"
|
8
|
-
require "cuba/contrib/with"
|
2
|
+
require "cuba/prelude"
|
3
|
+
require "cuba/mote"
|
4
|
+
require "cuba/text_helpers"
|
5
|
+
require "cuba/with"
|
9
6
|
end
|
File without changes
|
@@ -1,9 +1,7 @@
|
|
1
1
|
class Cuba
|
2
2
|
module TextHelpers
|
3
3
|
def markdown(str)
|
4
|
-
|
5
|
-
|
6
|
-
BlueCloth.new(str).to_html
|
4
|
+
Markdown.new(str).to_html
|
7
5
|
end
|
8
6
|
|
9
7
|
def truncate(str, length, ellipses = "...")
|
@@ -20,16 +18,23 @@ class Cuba
|
|
20
18
|
"#{unit} %.2f" % amount
|
21
19
|
end
|
22
20
|
|
23
|
-
def
|
24
|
-
|
21
|
+
def delimit(number, delimiter = ",")
|
22
|
+
number.to_s.gsub(%r{(\d)(?=(\d\d\d)+(?!\d))}, "\\1#{delimiter}")
|
25
23
|
end
|
26
24
|
|
27
|
-
def
|
28
|
-
|
25
|
+
def titlecase(str)
|
26
|
+
res = str.to_s.dup
|
27
|
+
res.tr!("_", " ")
|
28
|
+
res.gsub!(/(^|\s)([a-z])/) { |char| char.upcase }
|
29
|
+
res
|
29
30
|
end
|
30
31
|
|
31
32
|
def underscore(str)
|
32
|
-
str.
|
33
|
+
res = str.to_s.dup
|
34
|
+
res.gsub!(/([A-Z]+)([A-Z][a-z])/,'\1_\2')
|
35
|
+
res.gsub!(/([a-z\d])([A-Z])/,'\1_\2')
|
36
|
+
res.downcase!
|
37
|
+
res
|
33
38
|
end
|
34
39
|
end
|
35
40
|
end
|
@@ -0,0 +1,46 @@
|
|
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
|
+
require "bluecloth"
|
18
|
+
assert_equal "<h1>Hola Mundo</h1>", helper.markdown("# Hola Mundo")
|
19
|
+
end
|
20
|
+
|
21
|
+
test "nl2br" do |helper|
|
22
|
+
assert_equal "Hi<br>there<br>Joe", helper.nl2br("Hi\nthere\r\nJoe")
|
23
|
+
end
|
24
|
+
|
25
|
+
test "currency" do |helper|
|
26
|
+
assert_equal "$ 2.22", helper.currency(2.221)
|
27
|
+
assert_equal "€ 2.22", helper.currency(2.221, "€")
|
28
|
+
end
|
29
|
+
|
30
|
+
test "delimit" do |helper|
|
31
|
+
assert_equal "2,000", helper.delimit(2000)
|
32
|
+
assert_equal "2.000", helper.delimit(2000, ".")
|
33
|
+
assert_equal "2,000,000", helper.delimit(2000000)
|
34
|
+
assert_equal "2,000,000.05", helper.delimit(2000000.05)
|
35
|
+
end
|
36
|
+
|
37
|
+
test "titlecase" do |helper|
|
38
|
+
assert_equal "Hello World", helper.titlecase("hello world")
|
39
|
+
end
|
40
|
+
|
41
|
+
test "underscore" do |helper|
|
42
|
+
assert_equal "foo_bar_baz", helper.underscore("FooBarBaz")
|
43
|
+
assert_equal "ohm_v1_foo", helper.underscore("OhmV1Foo")
|
44
|
+
assert_equal "ssl_error", helper.underscore("SSLError")
|
45
|
+
assert_equal "net_http", helper.underscore("NetHTTP")
|
46
|
+
end
|
data/test/with.rb
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
require_relative "helper"
|
2
|
+
|
3
|
+
Cuba.plugin Cuba::With
|
4
|
+
|
5
|
+
test do
|
6
|
+
class UserPhotos < Cuba
|
7
|
+
define do
|
8
|
+
on root do
|
9
|
+
res.write "uid: %d" % vars[:user_id]
|
10
|
+
res.write "site: %s" % vars[:site]
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
class Photos < Cuba
|
16
|
+
define do
|
17
|
+
on ":id/photos" do |id|
|
18
|
+
with user_id: id do
|
19
|
+
_, _, body = UserPhotos.call(req.env)
|
20
|
+
|
21
|
+
body.each do |line|
|
22
|
+
res.write line
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
res.write vars.inspect
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
Cuba.define do
|
32
|
+
on "users" do
|
33
|
+
with user_id: "default", site: "main" do
|
34
|
+
run Photos
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
_, _, body = Cuba.call({ "PATH_INFO" => "/users/1001/photos",
|
40
|
+
"SCRIPT_NAME" => "" })
|
41
|
+
|
42
|
+
|
43
|
+
assert_response body, ["uid: 1001", "site: main",
|
44
|
+
'{:user_id=>"default", :site=>"main"}']
|
45
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cuba-contrib
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 3.1.0.
|
4
|
+
version: 3.1.0.rc2
|
5
5
|
prerelease: 6
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-07-
|
12
|
+
date: 2012-07-18 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: cuba
|
@@ -18,7 +18,7 @@ dependencies:
|
|
18
18
|
requirements:
|
19
19
|
- - '='
|
20
20
|
- !ruby/object:Gem::Version
|
21
|
-
version: 3.0.
|
21
|
+
version: 3.1.0.rc2
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
24
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -26,7 +26,7 @@ dependencies:
|
|
26
26
|
requirements:
|
27
27
|
- - '='
|
28
28
|
- !ruby/object:Gem::Version
|
29
|
-
version: 3.0.
|
29
|
+
version: 3.1.0.rc2
|
30
30
|
- !ruby/object:Gem::Dependency
|
31
31
|
name: cutest
|
32
32
|
requirement: !ruby/object:Gem::Requirement
|
@@ -69,24 +69,16 @@ files:
|
|
69
69
|
- LICENSE
|
70
70
|
- README.markdown
|
71
71
|
- rakefile
|
72
|
-
- lib/cuba/contrib/form_helpers.rb
|
73
|
-
- lib/cuba/contrib/mote.rb
|
74
|
-
- lib/cuba/contrib/prelude.rb
|
75
|
-
- lib/cuba/contrib/text_helpers.rb
|
76
|
-
- lib/cuba/contrib/with.rb
|
77
72
|
- lib/cuba/contrib.rb
|
73
|
+
- lib/cuba/mote.rb
|
74
|
+
- lib/cuba/prelude.rb
|
75
|
+
- lib/cuba/text_helpers.rb
|
76
|
+
- lib/cuba/with.rb
|
78
77
|
- cuba-contrib.gemspec
|
79
78
|
- test/helper.rb
|
80
79
|
- test/mote.rb
|
81
|
-
- test/
|
82
|
-
- test/
|
83
|
-
- views/field.mote
|
84
|
-
- views/file.mote
|
85
|
-
- views/option.mote
|
86
|
-
- views/password.mote
|
87
|
-
- views/select.mote
|
88
|
-
- views/textarea.mote
|
89
|
-
- views/textfield.mote
|
80
|
+
- test/text_helpers.rb
|
81
|
+
- test/with.rb
|
90
82
|
homepage: http://github.com/cyx/cuba-contrib
|
91
83
|
licenses: []
|
92
84
|
post_install_message:
|
@@ -1,107 +0,0 @@
|
|
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
|
data/test/text_helper.rb
DELETED
@@ -1,27 +0,0 @@
|
|
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
|
data/test/vars.rb
DELETED
@@ -1,32 +0,0 @@
|
|
1
|
-
require_relative "helper"
|
2
|
-
|
3
|
-
Cuba.plugin Cuba::With
|
4
|
-
|
5
|
-
class UserPhotos < Cuba
|
6
|
-
define do
|
7
|
-
on root do
|
8
|
-
res.write "uid: %d" % vars[:user_id]
|
9
|
-
end
|
10
|
-
end
|
11
|
-
end
|
12
|
-
|
13
|
-
test do
|
14
|
-
Cuba.define do
|
15
|
-
on "users/:id/photos" do |id|
|
16
|
-
with user_id: id do
|
17
|
-
_, _, body = UserPhotos.call(req.env)
|
18
|
-
|
19
|
-
body.each do |line|
|
20
|
-
res.write line
|
21
|
-
end
|
22
|
-
end
|
23
|
-
|
24
|
-
res.write vars.inspect
|
25
|
-
end
|
26
|
-
end
|
27
|
-
|
28
|
-
_, _, body = Cuba.call({ "PATH_INFO" => "/users/1001/photos",
|
29
|
-
"SCRIPT_NAME" => "" })
|
30
|
-
|
31
|
-
assert_response body, ["uid: 1001", "{}"]
|
32
|
-
end
|
data/views/field.mote
DELETED
data/views/file.mote
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
<input type="file" name="{{ name }}">
|
data/views/option.mote
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
<option {{ 'selected="selected"' if selected == value }} value="{{ h(value) }}">{{ h(label) }}</option>
|
data/views/password.mote
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
<input type="password" name="{{ name }}" value="">
|
data/views/select.mote
DELETED
data/views/textarea.mote
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
<textarea rows=10 cols=50 name="{{ name }}">{{ value }}</textarea>
|
data/views/textfield.mote
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
<input type="text" {{ 'class="%s"' % params[:class] if params[:class] }} name="{{ name }}" value="{{ value }}">
|