cuba-sugar 0.2.4 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile.lock ADDED
@@ -0,0 +1,29 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ cuba-sugar (0.3.0)
5
+ cuba (~> 3.1.0)
6
+ json (~> 1.7.3)
7
+ rack_csrf (~> 2.4.0)
8
+
9
+ GEM
10
+ remote: http://rubygems.org/
11
+ specs:
12
+ cuba (3.1.0)
13
+ rack
14
+ cutest (1.1.3)
15
+ json (1.7.3)
16
+ rack (1.4.1)
17
+ rack_csrf (2.4.0)
18
+ rack (>= 0.9)
19
+ rake (0.9.2.2)
20
+ tilt (1.3.3)
21
+
22
+ PLATFORMS
23
+ ruby
24
+
25
+ DEPENDENCIES
26
+ cuba-sugar!
27
+ cutest (~> 1.1.3)
28
+ rake
29
+ tilt (~> 1.3.3)
data/README.md CHANGED
@@ -18,15 +18,43 @@ Usage
18
18
 
19
19
  Like any other cuba app, but provides:
20
20
 
21
+ You have two choices:
22
+
23
+ * `require` only the tool you want eg: `require 'cuba/sugar/content_for'`
24
+ * `require` all the tools eg: `require 'cuba/sugar'`
25
+
26
+ ### content_for
27
+
28
+ ```ruby
29
+ require "cuba"
30
+ require "cuba/sugar/content_for"
31
+
32
+ Cuba.plugin Cuba::Sugar::ContentFor
33
+ ```
34
+
35
+ In your views to define where the content block will be rendered
36
+
37
+ ```erb
38
+ <% yield_for :menu %>
39
+
40
+ # And when you want to define a content
41
+ <% content_for :menu %>
42
+ <ul id="menu">
43
+ <li class="active">Home</li>
44
+ <li>Users</li>
45
+ </ul>
46
+ <% end %>
47
+ ```
48
+
21
49
  ### as
22
50
 
23
51
  ```ruby
24
52
  require "cuba"
25
- require "cuba/sugar"
53
+ require "cuba/sugar/as"
26
54
 
27
55
  Cuba.use Rack::Session::Cookie
28
56
 
29
- Cuba.plugin Cuba::Sugar
57
+ Cuba.plugin Cuba::Sugar::As
30
58
  Cuba.define do
31
59
  on post do
32
60
  on "users" do
@@ -43,11 +71,11 @@ end
43
71
 
44
72
  ```ruby
45
73
  require "cuba"
46
- require "cuba/sugar"
74
+ require "cuba/sugar/as"
47
75
 
48
76
  Cuba.use Rack::Session::Cookie
49
77
 
50
- Cuba.plugin Cuba::Sugar
78
+ Cuba.plugin Cuba::Sugar::As
51
79
  Cuba.define do
52
80
  on get do
53
81
  on "weather" do
@@ -75,11 +103,11 @@ In the code:
75
103
 
76
104
  ```ruby
77
105
  require "cuba"
78
- require "cuba/sugar"
106
+ require "cuba/sugar/csrf"
79
107
 
80
108
  Cuba.use Rack::Csrf
81
109
 
82
- Cuba.plugin Cuba::Sugar
110
+ Cuba.plugin Cuba::Sugar::Csrf
83
111
  Cuba.define do
84
112
  # Automatic csrf validation
85
113
  on post
@@ -111,9 +139,9 @@ end
111
139
 
112
140
  ```ruby
113
141
  require "cuba"
114
- require "cuba/sugar"
142
+ require "cuba/sugar/routes"
115
143
 
116
- Cuba.plugin Cuba::Sugar
144
+ Cuba.plugin Cuba::Sugar::Routes
117
145
  Cuba.define do
118
146
  on subdomain("wsdl") do
119
147
  run WSDL
data/cuba-sugar.gemspec CHANGED
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = "cuba-sugar"
3
- s.version = "0.2.4"
3
+ s.version = "0.3.0"
4
4
  s.summary = "Give Cuba some Sugar!"
5
5
  s.description = "Useful stuff to use with cuba"
6
6
  s.authors = ["elcuervo"]
@@ -9,9 +9,10 @@ Gem::Specification.new do |s|
9
9
  s.files = `git ls-files`.split("\n")
10
10
  s.test_files = `git ls-files test`.split("\n")
11
11
 
12
- s.add_dependency("cuba", "~> 3.0")
13
- s.add_dependency("json", "~> 1.6.6")
12
+ s.add_dependency("cuba", "~> 3.1.0")
13
+ s.add_dependency("json", "~> 1.7.3")
14
14
  s.add_dependency("rack_csrf", "~> 2.4.0")
15
15
 
16
16
  s.add_development_dependency("cutest", "~> 1.1.3")
17
+ s.add_development_dependency("tilt", "~> 1.3.3")
17
18
  end
data/lib/cuba/sugar.rb CHANGED
@@ -1,123 +1,28 @@
1
- require "rack/csrf"
1
+ require 'cuba/sugar/as'
2
+ require 'cuba/sugar/csrf'
3
+ require 'cuba/sugar/routes'
4
+ require 'cuba/sugar/content_for'
2
5
 
3
6
  module Cuba::Sugar
7
+ include As
8
+ include Csrf
9
+ include Routes
10
+ include ContentFor
4
11
 
5
- # Sugar to access an option verb
12
+ # Public: Sugar to include helpers
6
13
  #
7
- # @example
8
- # on options do
9
- # as_json do
10
- # API.interaction_methods
11
- # end
12
- # end
13
- def options; req.options? end
14
-
15
- # Sugar to easily access params
16
- #
17
- # @example
18
- # on get, root do
19
- # as do
20
- # "This id: #{params['id']}"
21
- # end
22
- # end
23
- def params; req.params end
24
-
25
- # Sugar to do some common response tasks
26
- #
27
- # @example
28
- # on post, "users" do
29
- # as 201 do
30
- # "User successfully created!"
31
- # end
32
- # end
33
- def as(http_code = 200, extra_headers = {})
34
- res.status = http_code
35
- res.headers.merge! extra_headers
36
- res.write yield if block_given?
37
- end
38
-
39
- # Sugar to do some common response tasks as_json
40
- #
41
- # @example
42
- # on post, "users" do
43
- # as_json 201 do
44
- # "User successfully created!"
45
- # end
46
- # end
47
- def as_json(http_code = 200, extra_headers = {})
48
- require 'json'
49
- extra_headers["Content-Type"] ||= "application/json"
50
- as(http_code, extra_headers) do
51
- (yield).to_json if block_given?
52
- end
53
- end
54
-
55
- # Sugar to do match a given subdomain.
56
- # eg. blog.example.com
57
- #
58
- # @example
59
- # on subdomain("blog") do
60
- # as do
61
- # run Blog
62
- # end
63
- # end
64
- def subdomain(sub)
65
- sub == req.host.split(".").first
66
- end
67
-
68
- # Sugar to access session info
69
- #
70
- # @example
71
- # on root do
72
- # as do
73
- # "User name: #{session['name']}"
74
- # end
75
- # end
76
- def session
77
- env["rack.session"]
78
- end
79
-
80
- # Sugar to redirect a request
14
+ # *extensions - Modules to be evaluated
15
+ # &block - Methods to be included
81
16
  #
82
- # @example
83
- # on "old" do
84
- # redirect "/new"
85
- # end
86
- def redirect(*args)
87
- res.redirect(*args)
88
- end
89
-
90
- # Sugar to include helpers and access them from
91
- # views
17
+ # Examples:
92
18
  #
93
- # @example
94
- # helpers do
95
- # def now
96
- # Time.now
19
+ # helpers do
20
+ # def now
21
+ # Time.now
22
+ # end
97
23
  # end
98
- # end
99
24
  def helpers(*extensions, &block)
100
- instance_eval(&block) if block_given?
25
+ instance_eval(&block) if block
101
26
  extend(*extensions) if extensions.any?
102
27
  end
103
-
104
- # Sugar to include a csrf tag
105
- #
106
- # @example
107
- # <form action="/new">
108
- # <%= csrf_tag %>
109
- # <input type="text" />
110
- # </form>
111
- def csrf_tag
112
- Rack::Csrf.tag(env)
113
- end
114
-
115
- # Sugar to access the csrf token
116
- #
117
- # @example
118
- # <%= csrf_token %>
119
- def csrf_token
120
- Rack::Csrf.token(env)
121
- end
122
-
123
28
  end
@@ -0,0 +1,38 @@
1
+ module Cuba::Sugar
2
+ module As
3
+ # Public: Sugar to do some common response tasks
4
+ #
5
+ # http_code - Response status code (default: 200)
6
+ # extra_headers - Extra headers hash (default: {})
7
+ #
8
+ # Examples:
9
+ #
10
+ # on post, "users" do
11
+ # as 201 do
12
+ # "User successfully created!"
13
+ # end
14
+ # end
15
+ def as(http_code = 200, extra_headers = {}, &block)
16
+ res.status = http_code
17
+ res.headers.merge! extra_headers
18
+ res.write yield if block
19
+ end
20
+
21
+ # Public: Sugar to do some common response tasks as_json
22
+ #
23
+ # http_code - Response status code (default: 200)
24
+ # extra_headers - Extra headers hash (default: {})
25
+ # Examples:
26
+ #
27
+ # on post, "users" do
28
+ # as_json 201 do
29
+ # "User successfully created!"
30
+ # end
31
+ # end
32
+ def as_json(http_code = 200, extra_headers = {}, &block)
33
+ require 'json'
34
+ extra_headers["Content-Type"] ||= "application/json"
35
+ as(http_code, extra_headers) { yield.to_json if block }
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,35 @@
1
+ module Cuba::Sugar
2
+ module ContentFor
3
+ # Public: yields a content in a view
4
+ #
5
+ # symbol - The symbol to be searched
6
+ #
7
+ # Examples:
8
+ #
9
+ # <% yield_for :menu %>
10
+ def yield_for(symbol)
11
+ content_blocks[symbol].map(&:call)
12
+ end
13
+
14
+ # Public: Sets a content for a given symbol
15
+ #
16
+ # symbol - The symbol key
17
+ # &block - Block to be called
18
+ #
19
+ # Examples:
20
+ #
21
+ # <% content_for :menu do %>
22
+ # Home | Admin
23
+ # <% end %>
24
+ def content_for(symbol, &block)
25
+ content_blocks[symbol] << block
26
+ end
27
+
28
+ private
29
+
30
+ # Private: Hash of arrays to store content blocks
31
+ def content_blocks
32
+ @content_blocks ||= Hash.new { |h, k| h[k] = [] }
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,26 @@
1
+ require "rack/csrf"
2
+
3
+ module Cuba::Sugar
4
+ module Csrf
5
+ # Public: Sugar to include a csrf tag
6
+ #
7
+ # Examples:
8
+ #
9
+ # <form action="/new">
10
+ # <%= csrf_tag %>
11
+ # <input type="text" />
12
+ # </form>
13
+ def csrf_tag
14
+ Rack::Csrf.tag(env)
15
+ end
16
+
17
+ # Public: Sugar to access the csrf token
18
+ #
19
+ # Examples:
20
+ #
21
+ # <%= csrf_token %>
22
+ def csrf_token
23
+ Rack::Csrf.token(env)
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,31 @@
1
+ module Cuba::Sugar
2
+ module Routes
3
+ # Public: Sugar to access an option verb
4
+ #
5
+ # Examples:
6
+ #
7
+ # on options do
8
+ # as_json do
9
+ # API.interaction_methods
10
+ # end
11
+ # end
12
+ def options
13
+ req.options?
14
+ end
15
+
16
+ # Public: Sugar to do match a given subdomain. eg. blog.example.com
17
+ #
18
+ # subdomain - subdomain to be checked
19
+ #
20
+ # Examples:
21
+ #
22
+ # on subdomain("blog") do
23
+ # as do
24
+ # run Blog
25
+ # end
26
+ # end
27
+ def subdomain(sub)
28
+ sub == req.host.split(".").first
29
+ end
30
+ end
31
+ end
data/test/as.rb CHANGED
@@ -1,7 +1,8 @@
1
1
  require File.expand_path("helper", File.dirname(__FILE__))
2
+ require 'cuba/sugar/as'
2
3
 
3
4
  test "set status and headers through helper" do
4
- Cuba.plugin Cuba::Sugar
5
+ Cuba.plugin Cuba::Sugar::As
5
6
  Cuba.define do
6
7
  on "users" do
7
8
  as 201, {"Content-Location" => "http://somewhere.com/users/705"} do
data/test/as_json.rb CHANGED
@@ -1,10 +1,10 @@
1
1
  require File.expand_path("helper", File.dirname(__FILE__))
2
- require 'json'
2
+ require 'cuba/sugar/as'
3
3
 
4
4
  test "set a block to return json" do
5
5
  rum_and_coke = { "rum" => "hot", "coke" => "sweet" }
6
6
 
7
- Cuba.plugin Cuba::Sugar
7
+ Cuba.plugin Cuba::Sugar::As
8
8
  Cuba.define do
9
9
  on "drinks" do
10
10
  as_json 201, {"Content-Location" => "http://somewhere.com/drinks/42"} do
@@ -0,0 +1,25 @@
1
+ require File.expand_path("helper", File.dirname(__FILE__))
2
+ require 'cuba/render'
3
+ require 'cuba/sugar/content_for'
4
+
5
+ scope do
6
+ setup do
7
+ Cuba.plugin Cuba::Render
8
+ Cuba.plugin Cuba::Sugar::ContentFor
9
+
10
+ Cuba.settings[:render][:views] = "./test/views"
11
+ Cuba.settings[:render][:template_engine] = "erb"
12
+
13
+ Cuba.define do
14
+ on root do
15
+ res.write view("home")
16
+ end
17
+ end
18
+ end
19
+
20
+ test "content for the menu" do
21
+ _, _, body = Cuba.call({ "PATH_INFO" => "/", "SCRIPT_NAME" => "/" })
22
+
23
+ assert_equal body, [" alpha\nbeta\ngamma\n"]
24
+ end
25
+ end
data/test/csrf.rb CHANGED
@@ -1,8 +1,11 @@
1
1
  require File.expand_path("helper", File.dirname(__FILE__))
2
2
  require 'cuba/render'
3
+ require 'cuba/sugar/as'
4
+ require 'cuba/sugar/csrf'
3
5
 
4
6
  test "set status and headers through helper" do
5
- Cuba.plugin Cuba::Sugar
7
+ Cuba.plugin Cuba::Sugar::As
8
+ Cuba.plugin Cuba::Sugar::Csrf
6
9
  Cuba.plugin Cuba::Render
7
10
  Cuba.define do
8
11
  on "users" do
data/test/helper.rb CHANGED
@@ -1,6 +1,5 @@
1
1
  $:.unshift(File.expand_path("../lib", File.dirname(__FILE__)))
2
2
  require "cuba"
3
- require "cuba/sugar"
4
3
  require "cutest"
5
4
 
6
5
  prepare { Cuba.reset! }
data/test/helpers.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require File.expand_path("helper", File.dirname(__FILE__))
2
+ require 'cuba/sugar'
2
3
 
3
4
  test "set status and headers through helpers" do
4
5
  Cuba.plugin Cuba::Sugar
data/test/subdomain.rb CHANGED
@@ -1,7 +1,10 @@
1
1
  require File.expand_path("helper", File.dirname(__FILE__))
2
+ require 'cuba/sugar/as'
3
+ require 'cuba/sugar/routes'
2
4
 
3
5
  test "eval only in a given subdomain" do
4
- Cuba.plugin Cuba::Sugar
6
+ Cuba.plugin Cuba::Sugar::As
7
+ Cuba.plugin Cuba::Sugar::Routes
5
8
  Cuba.define do
6
9
  on subdomain("api"), root do
7
10
  as do
@@ -0,0 +1,4 @@
1
+ gamma
2
+ <% content_for :menu do %>
3
+ alpha
4
+ <% end %>
@@ -0,0 +1,4 @@
1
+ <% yield_for :menu %>
2
+ <% yield_for :not %>
3
+ beta
4
+ <%= content %>
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cuba-sugar
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.4
4
+ version: 0.3.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,33 +9,43 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-03-29 00:00:00.000000000Z
12
+ date: 2012-07-25 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: cuba
16
- requirement: &70145888408160 !ruby/object:Gem::Requirement
16
+ requirement: !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
20
20
  - !ruby/object:Gem::Version
21
- version: '3.0'
21
+ version: 3.1.0
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70145888408160
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: 3.1.0
25
30
  - !ruby/object:Gem::Dependency
26
31
  name: json
27
- requirement: &70145888407580 !ruby/object:Gem::Requirement
32
+ requirement: !ruby/object:Gem::Requirement
28
33
  none: false
29
34
  requirements:
30
35
  - - ~>
31
36
  - !ruby/object:Gem::Version
32
- version: 1.6.6
37
+ version: 1.7.3
33
38
  type: :runtime
34
39
  prerelease: false
35
- version_requirements: *70145888407580
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: 1.7.3
36
46
  - !ruby/object:Gem::Dependency
37
47
  name: rack_csrf
38
- requirement: &70145888407060 !ruby/object:Gem::Requirement
48
+ requirement: !ruby/object:Gem::Requirement
39
49
  none: false
40
50
  requirements:
41
51
  - - ~>
@@ -43,10 +53,15 @@ dependencies:
43
53
  version: 2.4.0
44
54
  type: :runtime
45
55
  prerelease: false
46
- version_requirements: *70145888407060
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 2.4.0
47
62
  - !ruby/object:Gem::Dependency
48
63
  name: cutest
49
- requirement: &70145888406560 !ruby/object:Gem::Requirement
64
+ requirement: !ruby/object:Gem::Requirement
50
65
  none: false
51
66
  requirements:
52
67
  - - ~>
@@ -54,7 +69,28 @@ dependencies:
54
69
  version: 1.1.3
55
70
  type: :development
56
71
  prerelease: false
57
- version_requirements: *70145888406560
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ version: 1.1.3
78
+ - !ruby/object:Gem::Dependency
79
+ name: tilt
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ~>
84
+ - !ruby/object:Gem::Version
85
+ version: 1.3.3
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ~>
92
+ - !ruby/object:Gem::Version
93
+ version: 1.3.3
58
94
  description: Useful stuff to use with cuba
59
95
  email:
60
96
  - yo@brunoaguirre.com
@@ -64,19 +100,26 @@ extra_rdoc_files: []
64
100
  files:
65
101
  - .gitignore
66
102
  - Gemfile
103
+ - Gemfile.lock
67
104
  - LICENSE
68
105
  - README.md
69
106
  - Rakefile
70
107
  - cuba-sugar.gemspec
71
108
  - lib/cuba/sugar.rb
109
+ - lib/cuba/sugar/as.rb
110
+ - lib/cuba/sugar/content_for.rb
111
+ - lib/cuba/sugar/csrf.rb
112
+ - lib/cuba/sugar/routes.rb
72
113
  - test/as.rb
73
114
  - test/as_json.rb
115
+ - test/content_for.rb
74
116
  - test/csrf.rb
75
117
  - test/fixtures/csrf.erb
76
118
  - test/helper.rb
77
119
  - test/helpers.rb
78
- - test/redirect.rb
79
120
  - test/subdomain.rb
121
+ - test/views/home.erb
122
+ - test/views/layout.erb
80
123
  homepage: http://github.com/elcuervo/cuba-sugar
81
124
  licenses: []
82
125
  post_install_message:
@@ -97,16 +140,18 @@ required_rubygems_version: !ruby/object:Gem::Requirement
97
140
  version: '0'
98
141
  requirements: []
99
142
  rubyforge_project:
100
- rubygems_version: 1.8.15
143
+ rubygems_version: 1.8.22
101
144
  signing_key:
102
145
  specification_version: 3
103
146
  summary: Give Cuba some Sugar!
104
147
  test_files:
105
148
  - test/as.rb
106
149
  - test/as_json.rb
150
+ - test/content_for.rb
107
151
  - test/csrf.rb
108
152
  - test/fixtures/csrf.erb
109
153
  - test/helper.rb
110
154
  - test/helpers.rb
111
- - test/redirect.rb
112
155
  - test/subdomain.rb
156
+ - test/views/home.erb
157
+ - test/views/layout.erb
data/test/redirect.rb DELETED
@@ -1,17 +0,0 @@
1
- require File.expand_path("helper", File.dirname(__FILE__))
2
-
3
- test "redirect helper" do
4
- Cuba.plugin Cuba::Sugar
5
- Cuba.define do
6
- on "old" do
7
- redirect "/new"
8
- end
9
- end
10
-
11
- env = { "SCRIPT_NAME" => "/", "PATH_INFO" => "/old" }
12
-
13
- code, headers, _ = Cuba.call(env)
14
-
15
- assert_equal 302, code
16
- assert_equal "/new", headers["Location"]
17
- end