Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

New version

* content_for feature
* splitted helpers
* TomDoc'd
  • Loading branch information...
commit 9b2a0d6f88dcc035cbba5ab9deabc8d92e99d181 1 parent 9a4e746
@elcuervo authored
View
42 README.md
@@ -18,15 +18,41 @@ Usage
Like any other cuba app, but provides:
+You have two choices:
+
+* `require` only the tool you want eg: `require 'cuba/sugar/content_for'`
+* `require` all the tools eg: `require 'cuba/sugar'`
+
+### content_for
+
+```ruby
+require "cuba"
+require "cuba/sugar/content_for"
+
+Cuba.plugin Cuba::Sugar::ContentFor
+
+# In your views to define where the content block will be rendered
+
+<% yield_for :menu %>
+
+# And when you want to define a content
+<% content_for :menu %>
+ <ul id="menu">
+ <li class="active">Home</li>
+ <li>Users</li>
+ </ul>
+<% end %>
+```
+
### as
```ruby
require "cuba"
-require "cuba/sugar"
+require "cuba/sugar/as"
Cuba.use Rack::Session::Cookie
-Cuba.plugin Cuba::Sugar
+Cuba.plugin Cuba::Sugar::As
Cuba.define do
on post do
on "users" do
@@ -43,11 +69,11 @@ end
```ruby
require "cuba"
-require "cuba/sugar"
+require "cuba/sugar/as"
Cuba.use Rack::Session::Cookie
-Cuba.plugin Cuba::Sugar
+Cuba.plugin Cuba::Sugar::As
Cuba.define do
on get do
on "weather" do
@@ -75,11 +101,11 @@ In the code:
```ruby
require "cuba"
-require "cuba/sugar"
+require "cuba/sugar/csrf"
Cuba.use Rack::Csrf
-Cuba.plugin Cuba::Sugar
+Cuba.plugin Cuba::Sugar::Csrf
Cuba.define do
# Automatic csrf validation
on post
@@ -111,9 +137,9 @@ end
```ruby
require "cuba"
-require "cuba/sugar"
+require "cuba/sugar/routes"
-Cuba.plugin Cuba::Sugar
+Cuba.plugin Cuba::Sugar::Routes
Cuba.define do
on subdomain("wsdl") do
run WSDL
View
6 cuba-sugar.gemspec
@@ -1,6 +1,6 @@
Gem::Specification.new do |s|
s.name = "cuba-sugar"
- s.version = "0.2.4"
+ s.version = "0.3.0"
s.summary = "Give Cuba some Sugar!"
s.description = "Useful stuff to use with cuba"
s.authors = ["elcuervo"]
@@ -9,8 +9,8 @@ Gem::Specification.new do |s|
s.files = `git ls-files`.split("\n")
s.test_files = `git ls-files test`.split("\n")
- s.add_dependency("cuba", "~> 3.0")
- s.add_dependency("json", "~> 1.6.6")
+ s.add_dependency("cuba", "~> 3.1.0")
+ s.add_dependency("json", "~> 1.7.3")
s.add_dependency("rack_csrf", "~> 2.4.0")
s.add_development_dependency("cutest", "~> 1.1.3")
View
129 lib/cuba/sugar.rb
@@ -1,123 +1,28 @@
-require "rack/csrf"
+require 'cuba/sugar/as'
+require 'cuba/sugar/csrf'
+require 'cuba/sugar/routes'
+require 'cuba/sugar/content_for'
module Cuba::Sugar
+ include As
+ include Csrf
+ include Routes
+ include ContentFor
- # Sugar to access an option verb
+ # Public: Sugar to include helpers
#
- # @example
- # on options do
- # as_json do
- # API.interaction_methods
- # end
- # end
- def options; req.options? end
-
- # Sugar to easily access params
- #
- # @example
- # on get, root do
- # as do
- # "This id: #{params['id']}"
- # end
- # end
- def params; req.params end
-
- # Sugar to do some common response tasks
- #
- # @example
- # on post, "users" do
- # as 201 do
- # "User successfully created!"
- # end
- # end
- def as(http_code = 200, extra_headers = {})
- res.status = http_code
- res.headers.merge! extra_headers
- res.write yield if block_given?
- end
-
- # Sugar to do some common response tasks as_json
- #
- # @example
- # on post, "users" do
- # as_json 201 do
- # "User successfully created!"
- # end
- # end
- def as_json(http_code = 200, extra_headers = {})
- require 'json'
- extra_headers["Content-Type"] ||= "application/json"
- as(http_code, extra_headers) do
- (yield).to_json if block_given?
- end
- end
-
- # Sugar to do match a given subdomain.
- # eg. blog.example.com
- #
- # @example
- # on subdomain("blog") do
- # as do
- # run Blog
- # end
- # end
- def subdomain(sub)
- sub == req.host.split(".").first
- end
-
- # Sugar to access session info
- #
- # @example
- # on root do
- # as do
- # "User name: #{session['name']}"
- # end
- # end
- def session
- env["rack.session"]
- end
-
- # Sugar to redirect a request
+ # *extensions - Modules to be evaluated
+ # &block - Methods to be included
#
- # @example
- # on "old" do
- # redirect "/new"
- # end
- def redirect(*args)
- res.redirect(*args)
- end
-
- # Sugar to include helpers and access them from
- # views
+ # Examples:
#
- # @example
- # helpers do
- # def now
- # Time.now
+ # helpers do
+ # def now
+ # Time.now
+ # end
# end
- # end
def helpers(*extensions, &block)
- instance_eval(&block) if block_given?
+ instance_eval(&block) if block
extend(*extensions) if extensions.any?
end
-
- # Sugar to include a csrf tag
- #
- # @example
- # <form action="/new">
- # <%= csrf_tag %>
- # <input type="text" />
- # </form>
- def csrf_tag
- Rack::Csrf.tag(env)
- end
-
- # Sugar to access the csrf token
- #
- # @example
- # <%= csrf_token %>
- def csrf_token
- Rack::Csrf.token(env)
- end
-
end
View
38 lib/cuba/sugar/as.rb
@@ -0,0 +1,38 @@
+module Cuba::Sugar
+ module As
+ # Public: Sugar to do some common response tasks
+ #
+ # http_code - Response status code (default: 200)
+ # extra_headers - Extra headers hash (default: {})
+ #
+ # Examples:
+ #
+ # on post, "users" do
+ # as 201 do
+ # "User successfully created!"
+ # end
+ # end
+ def as(http_code = 200, extra_headers = {}, &block)
+ res.status = http_code
+ res.headers.merge! extra_headers
+ res.write yield if block
+ end
+
+ # Public: Sugar to do some common response tasks as_json
+ #
+ # http_code - Response status code (default: 200)
+ # extra_headers - Extra headers hash (default: {})
+ # Examples:
+ #
+ # on post, "users" do
+ # as_json 201 do
+ # "User successfully created!"
+ # end
+ # end
+ def as_json(http_code = 200, extra_headers = {}, &block)
+ require 'json'
+ extra_headers["Content-Type"] ||= "application/json"
+ as(http_code, extra_headers) { yield.to_json if block }
+ end
+ end
+end
View
35 lib/cuba/sugar/content_for.rb
@@ -0,0 +1,35 @@
+module Cuba::Sugar
+ module ContentFor
+ # Public: yields a content in a view
+ #
+ # symbol - The symbol to be searched
+ #
+ # Examples:
+ #
+ # <% yield_for :menu %>
+ def yield_for(symbol)
+ content_blocks[symbol].map(&:call)
+ end
+
+ # Public: Sets a content for a given symbol
+ #
+ # symbol - The symbol key
+ # &block - Block to be called
+ #
+ # Examples:
+ #
+ # <% content_for :menu do %>
+ # Home | Admin
+ # <% end %>
+ def content_for(symbol, &block)
+ content_blocks[symbol] << block
+ end
+
+ private
+
+ # Private: Hash of arrays to store content blocks
+ def content_blocks
+ @content_blocks ||= Hash.new { |h, k| h[k] = [] }
+ end
+ end
+end
View
26 lib/cuba/sugar/csrf.rb
@@ -0,0 +1,26 @@
+require "rack/csrf"
+
+module Cuba::Sugar
+ module Csrf
+ # Public: Sugar to include a csrf tag
+ #
+ # Examples:
+ #
+ # <form action="/new">
+ # <%= csrf_tag %>
+ # <input type="text" />
+ # </form>
+ def csrf_tag
+ Rack::Csrf.tag(env)
+ end
+
+ # Public: Sugar to access the csrf token
+ #
+ # Examples:
+ #
+ # <%= csrf_token %>
+ def csrf_token
+ Rack::Csrf.token(env)
+ end
+ end
+end
View
31 lib/cuba/sugar/routes.rb
@@ -0,0 +1,31 @@
+module Cuba::Sugar
+ module Routes
+ # Public: Sugar to access an option verb
+ #
+ # Examples:
+ #
+ # on options do
+ # as_json do
+ # API.interaction_methods
+ # end
+ # end
+ def options
+ req.options?
+ end
+
+ # Public: Sugar to do match a given subdomain. eg. blog.example.com
+ #
+ # subdomain - subdomain to be checked
+ #
+ # Examples:
+ #
+ # on subdomain("blog") do
+ # as do
+ # run Blog
+ # end
+ # end
+ def subdomain(sub)
+ sub == req.host.split(".").first
+ end
+ end
+end
View
3  test/as.rb
@@ -1,7 +1,8 @@
require File.expand_path("helper", File.dirname(__FILE__))
+require 'cuba/sugar/as'
test "set status and headers through helper" do
- Cuba.plugin Cuba::Sugar
+ Cuba.plugin Cuba::Sugar::As
Cuba.define do
on "users" do
as 201, {"Content-Location" => "http://somewhere.com/users/705"} do
View
4 test/as_json.rb
@@ -1,10 +1,10 @@
require File.expand_path("helper", File.dirname(__FILE__))
-require 'json'
+require 'cuba/sugar/as'
test "set a block to return json" do
rum_and_coke = { "rum" => "hot", "coke" => "sweet" }
- Cuba.plugin Cuba::Sugar
+ Cuba.plugin Cuba::Sugar::As
Cuba.define do
on "drinks" do
as_json 201, {"Content-Location" => "http://somewhere.com/drinks/42"} do
View
25 test/content_for.rb
@@ -0,0 +1,25 @@
+require File.expand_path("helper", File.dirname(__FILE__))
+require 'cuba/render'
+require 'cuba/sugar/content_for'
+
+scope do
+ setup do
+ Cuba.plugin Cuba::Render
+ Cuba.plugin Cuba::Sugar::ContentFor
+
+ Cuba.settings[:render][:views] = "./test/views"
+ Cuba.settings[:render][:template_engine] = "erb"
+
+ Cuba.define do
+ on root do
+ res.write view("home")
+ end
+ end
+ end
+
+ test "content for the menu" do
+ _, _, body = Cuba.call({ "PATH_INFO" => "/", "SCRIPT_NAME" => "/" })
+
+ assert_equal body, [" alpha\nbeta\ngamma\n\n"]
+ end
+end
View
5 test/csrf.rb
@@ -1,8 +1,11 @@
require File.expand_path("helper", File.dirname(__FILE__))
require 'cuba/render'
+require 'cuba/sugar/as'
+require 'cuba/sugar/csrf'
test "set status and headers through helper" do
- Cuba.plugin Cuba::Sugar
+ Cuba.plugin Cuba::Sugar::As
+ Cuba.plugin Cuba::Sugar::Csrf
Cuba.plugin Cuba::Render
Cuba.define do
on "users" do
View
1  test/helper.rb
@@ -1,6 +1,5 @@
$:.unshift(File.expand_path("../lib", File.dirname(__FILE__)))
require "cuba"
-require "cuba/sugar"
require "cutest"
prepare { Cuba.reset! }
View
1  test/helpers.rb
@@ -1,4 +1,5 @@
require File.expand_path("helper", File.dirname(__FILE__))
+require 'cuba/sugar'
test "set status and headers through helpers" do
Cuba.plugin Cuba::Sugar
View
17 test/redirect.rb
@@ -1,17 +0,0 @@
-require File.expand_path("helper", File.dirname(__FILE__))
-
-test "redirect helper" do
- Cuba.plugin Cuba::Sugar
- Cuba.define do
- on "old" do
- redirect "/new"
- end
- end
-
- env = { "SCRIPT_NAME" => "/", "PATH_INFO" => "/old" }
-
- code, headers, _ = Cuba.call(env)
-
- assert_equal 302, code
- assert_equal "/new", headers["Location"]
-end
View
5 test/subdomain.rb
@@ -1,7 +1,10 @@
require File.expand_path("helper", File.dirname(__FILE__))
+require 'cuba/sugar/as'
+require 'cuba/sugar/routes'
test "eval only in a given subdomain" do
- Cuba.plugin Cuba::Sugar
+ Cuba.plugin Cuba::Sugar::As
+ Cuba.plugin Cuba::Sugar::Routes
Cuba.define do
on subdomain("api"), root do
as do
View
4 test/views/home.erb
@@ -0,0 +1,4 @@
+gamma
+<% content_for :menu do %>
+ alpha
+<% end %>
View
4 test/views/layout.erb
@@ -0,0 +1,4 @@
+<% yield_for :menu %>
+<% yield_for :not %>
+beta
+<%= content %>
Please sign in to comment.
Something went wrong with that request. Please try again.