cuba-contrib 0.1.0.rc2 → 3.0.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
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 = "0.1.0.rc2"
3
+ s.version = "3.0.0.rc1"
4
4
  s.summary = "Cuba plugins and utilities."
5
5
  s.description = "Includes various helper tools for Cuba."
6
6
  s.authors = ["Cyril David"]
data/lib/cuba/contrib.rb CHANGED
@@ -1,22 +1,9 @@
1
1
  class Cuba
2
2
  CONTRIB_ROOT = File.expand_path("../../", File.dirname(__FILE__))
3
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"
4
+ autoload :Prelude, "cuba/contrib/prelude"
5
+ autoload :Rendering, "cuba/contrib/rendering"
6
+ autoload :Mote, "cuba/contrib/mote"
20
7
  autoload :TextHelpers, "cuba/contrib/text_helpers"
21
8
  autoload :FormHelpers, "cuba/contrib/form_helpers"
22
- end
9
+ end
@@ -5,17 +5,15 @@ class Cuba
5
5
  include ::Mote::Helpers
6
6
 
7
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"
8
+ app.settings[:views] = File.expand_path("views", Dir.pwd)
9
+ app.settings[:layout] = "layout"
12
10
  end
13
11
 
14
12
  def partial(template, locals = {})
15
13
  mote(mote_path(template), locals)
16
14
  end
17
15
 
18
- def view(template, locals = {}, layout = settings.layout)
16
+ def view(template, locals = {}, layout = settings[:layout])
19
17
  raise NoLayout.new(self) unless layout
20
18
 
21
19
  partial(layout, locals.merge(mote_vars(partial(template, locals))))
@@ -24,7 +22,7 @@ class Cuba
24
22
  def mote_path(template)
25
23
  return template if template.end_with?(".mote")
26
24
 
27
- File.expand_path("#{template}.mote", settings.views)
25
+ File.join(settings[:views], "#{template}.mote")
28
26
  end
29
27
 
30
28
  def mote_vars(content)
@@ -39,8 +37,8 @@ class Cuba
39
37
  end
40
38
 
41
39
  def message
42
- "Missing Layout: Try doing #{instance.class}.set :layout, 'layout'"
40
+ "Missing Layout: Try doing #{instance.class}.settings[:layout] = 'layout'"
43
41
  end
44
42
  end
45
43
  end
46
- end
44
+ end
@@ -3,17 +3,17 @@ require "tilt"
3
3
  class Cuba
4
4
  module Rendering
5
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)
6
+ app.settings[:template_engine] = "erb"
7
+ app.settings[:views] = File.expand_path("views", Dir.pwd)
8
+ app.settings[:layout] = "layout"
9
9
  end
10
10
 
11
- def view(template, locals = {}, layout = "layout")
11
+ def view(template, locals = {}, layout = settings[:layout])
12
12
  partial(layout, { content: partial(template, locals) }.merge(locals))
13
13
  end
14
14
 
15
15
  def partial(template, locals = {})
16
- render("#{settings.views}/#{template}.#{settings.template_engine}",
16
+ render("#{settings[:views]}/#{template}.#{settings[:template_engine]}",
17
17
  locals, default_encoding: Encoding.default_external)
18
18
  end
19
19
 
@@ -34,9 +34,16 @@ class Cuba
34
34
  # render("layout.haml") { render("home.haml") }
35
35
  #
36
36
  def render(template, locals = {}, options = {}, &block)
37
- _cache.fetch(template, locals) {
37
+ _cache.fetch(template) {
38
38
  Tilt.new(template, 1, options)
39
39
  }.render(self, locals, &block)
40
40
  end
41
+
42
+ # @private Used internally by #render to cache the
43
+ # Tilt templates.
44
+ def _cache
45
+ Thread.current[:_cache] ||= Tilt::Cache.new
46
+ end
47
+ private :_cache
41
48
  end
42
- end
49
+ end
data/test/caching.rb ADDED
@@ -0,0 +1,17 @@
1
+ require File.expand_path("helper", File.dirname(__FILE__))
2
+
3
+ test "executes on true" do
4
+ Cuba.plugin Cuba::Rendering
5
+
6
+ Cuba.define do
7
+ on "foo/:i" do |i|
8
+ res.write render("test/test.erb", title: i)
9
+ end
10
+ end
11
+
12
+ 1000.times do |i|
13
+ _, _, resp = Cuba.call({ "PATH_INFO" => "/foo/#{i}", "SCRIPT_NAME" => "" })
14
+ end
15
+
16
+ assert_equal 1, Thread.current[:_cache].instance_variable_get(:@cache).size
17
+ end
data/test/layout.rb ADDED
@@ -0,0 +1,16 @@
1
+ require File.expand_path("helper", File.dirname(__FILE__))
2
+
3
+ test "simple layout support" do
4
+ Cuba.plugin Cuba::Rendering
5
+ Cuba.define do
6
+ on true do
7
+ res.write render("test/fixtures/layout.erb") {
8
+ render("test/fixtures/content.erb")
9
+ }
10
+ end
11
+ end
12
+
13
+ _, _, resp = Cuba.call({})
14
+
15
+ assert_equal ["alfa beta\n\n"], resp.body
16
+ end
data/test/mote.rb CHANGED
@@ -1,8 +1,8 @@
1
1
  require_relative "helper"
2
2
 
3
3
  Cuba.plugin Cuba::Mote
4
-
5
- Cuba.set :views, "./test/views"
4
+ Cuba.use Rack::Session::Cookie
5
+ Cuba.settings[:views] = "./test/views"
6
6
 
7
7
  Cuba.define do
8
8
  on "frag" do
data/test/rendering.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  require_relative "helper"
2
2
 
3
3
  Cuba.plugin Cuba::Rendering
4
- Cuba.views = "./test/views"
4
+ Cuba.settings[:views] = "./test/views"
5
5
 
6
6
  Cuba.define do
7
7
  on "home" do
@@ -26,7 +26,7 @@ test "view" do
26
26
  end
27
27
 
28
28
  test "partial with str as engine" do
29
- Cuba.template_engine = "str"
29
+ Cuba.settings[:template_engine] = "str"
30
30
 
31
31
  _, _, body = Cuba.call({ "PATH_INFO" => "/about", "SCRIPT_NAME" => "/" })
32
32
 
@@ -34,9 +34,9 @@ test "partial with str as engine" do
34
34
  end
35
35
 
36
36
  test "view with str as engine" do
37
- Cuba.template_engine = "str"
37
+ Cuba.settings[:template_engine] = "str"
38
38
 
39
39
  _, _, body = Cuba.call({ "PATH_INFO" => "/home", "SCRIPT_NAME" => "/" })
40
40
 
41
41
  assert_response body, ["<title>Cuba: Home</title>\n<h1>Home</h1>\n<p>Hello Agent Smith</p>"]
42
- end
42
+ end
data/test/test.erb ADDED
@@ -0,0 +1 @@
1
+ <%= title %>
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: 0.1.0.rc2
4
+ version: 3.0.0.rc1
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-01-23 00:00:00.000000000 Z
12
+ date: 2012-02-14 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: cuba
16
- requirement: &2152386760 !ruby/object:Gem::Requirement
16
+ requirement: &2152386960 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *2152386760
24
+ version_requirements: *2152386960
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: cutest
27
- requirement: &2152385600 !ruby/object:Gem::Requirement
27
+ requirement: &2152385700 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *2152385600
35
+ version_requirements: *2152385700
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: capybara
38
- requirement: &2152401420 !ruby/object:Gem::Requirement
38
+ requirement: &2152401460 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,7 +43,7 @@ dependencies:
43
43
  version: '0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *2152401420
46
+ version_requirements: *2152401460
47
47
  description: Includes various helper tools for Cuba.
48
48
  email:
49
49
  - me@cyrildavid.com
@@ -58,15 +58,15 @@ files:
58
58
  - lib/cuba/contrib/mote.rb
59
59
  - lib/cuba/contrib/prelude.rb
60
60
  - lib/cuba/contrib/rendering.rb
61
- - lib/cuba/contrib/settings.rb
62
61
  - lib/cuba/contrib/text_helpers.rb
63
62
  - lib/cuba/contrib.rb
64
63
  - cuba-contrib.gemspec
65
- - test/contrib.rb
64
+ - test/caching.rb
66
65
  - test/helper.rb
66
+ - test/layout.rb
67
67
  - test/mote.rb
68
68
  - test/rendering.rb
69
- - test/settings.rb
69
+ - test/test.erb
70
70
  - test/text_helper.rb
71
71
  - views/field.mote
72
72
  - views/file.mote
@@ -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
data/test/contrib.rb DELETED
@@ -1,68 +0,0 @@
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/settings.rb DELETED
@@ -1,38 +0,0 @@
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
- class Admin < Cuba
13
- end
14
-
15
- test do
16
- assert_equal "bar", Cuba.foo
17
- end
18
-
19
- test do
20
- _, _, body = Cuba.call({ "PATH_INFO" => "", "SCRIPT_NAME" => "" })
21
- assert_response body, ["bar"]
22
- end
23
-
24
- test do
25
- assert_equal "bar", Admin.foo
26
-
27
- Admin.foo = "baz"
28
-
29
- assert_equal "baz", Admin.foo
30
- assert_equal "bar", Cuba.foo
31
- end
32
-
33
- test do
34
- Cuba.foo = "baz"
35
- assert_equal "baz", Cuba.foo
36
- end
37
-
38
-