Permalink
Browse files

New version

* content_for feature
* splitted helpers
* TomDoc'd
  • Loading branch information...
1 parent 9a4e746 commit 9b2a0d6f88dcc035cbba5ab9deabc8d92e99d181 @elcuervo committed Jul 6, 2012
View
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
Oops, something went wrong.

0 comments on commit 9b2a0d6

Please sign in to comment.