Skip to content

Commit

Permalink
commenting
Browse files Browse the repository at this point in the history
  • Loading branch information
lancejpollard committed Apr 7, 2012
1 parent 58ed5e3 commit 682f2fa
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 4 deletions.
15 changes: 14 additions & 1 deletion lib/tower/controller/params.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,23 @@
Tower.Controller.Params = {
ClassMethods: {
param: function(key, options) {
if (options == null) options = {};
return this.params()[key] = Tower.HTTP.Param.create(key, options);
},
params: function() {
var arg, key, value, _i, _len;
if (arguments.length) {
for (_i = 0, _len = arguments.length; _i < _len; _i++) {
arg = arguments[_i];
if (typeof arg === "object") {
for (key in arg) {
value = arg[key];
this.param(key, value);
}
} else {
this.param(arg);
}
}
}
return this.metadata().params;
}
},
Expand Down
6 changes: 6 additions & 0 deletions lib/tower/http/param.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ Tower.HTTP.Param = (function() {
Param.separator = "_";

Param.create = function(key, options) {
if (options == null) options = {};
if (typeof options === "string") {
options = {
type: options
};
}
options.type || (options.type = "String");
return new Tower.HTTP.Param[options.type](key, options);
};
Expand Down
5 changes: 5 additions & 0 deletions src/tower/controller/callbacks.coffee
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
# @mixin
Tower.Controller.Callbacks =
ClassMethods:
# Callback to run before any action.
beforeAction: ->
@before "action", arguments...

# Callback to run after any action.
afterAction: ->
@after "action", arguments...

# All of the callbacks defined for this controller.
#
# @return [Object]
callbacks: ->
@metadata().callbacks

Expand Down
22 changes: 20 additions & 2 deletions src/tower/controller/params.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,28 @@ Tower.Controller.Params =
# @option options [String] type
#
# @return [Tower.HTTP.Param]
param: (key, options = {}) ->
param: (key, options) ->
@params()[key] = Tower.HTTP.Param.create(key, options)


# Return all params, or define multiple params at once.
#
# @example Pass in an object
# class App.UsersController extends App.ApplicationController
# @params email: "String"
#
# @example Pass in strings
# class App.UsersController extends App.ApplicationController
# @params "email", "firstName", "lastName"
#
# @return [Object]
params: ->
if arguments.length
for arg in arguments
if typeof arg == "object"
@param(key, value) for key, value of arg
else
@param(arg)

@metadata().params

InstanceMethods:
Expand Down
43 changes: 43 additions & 0 deletions src/tower/controller/responding.coffee
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
# @mixin
Tower.Controller.Responding =
ClassMethods:
# Defines mime types that are rendered by default when invoking {#respondWith}.
#
# @example
# @respondTo "html", "json", "csv", "pdf"
#
# @example Only certain actions
# @respondTo "json", only: "edit"
#
# @example Except certain actions
# @respondTo "json", except: ["create", "destroy"]
#
# @return [Function] Return this controller.
respondTo: ->
mimes = @mimes()
args = _.args(arguments)
Expand All @@ -26,13 +38,44 @@ Tower.Controller.Responding =
InstanceMethods:
# Build a responder.
#
# @example
# index: ->
# App.User.all (error, users) =>
# @respondTo (format) =>
# format.html()
# format.json => @render json: users
#
# @param [Function] block
#
# @return [void] Requires a block.
respondTo: (block) ->
Tower.Controller.Responder.respond(@, {}, block)

# A more robust responder.
#
# Wraps a resource around a responder for default representation.
# First it invokes {#respondTo}, and if a response cannot be found
# (ie. no block for the request was given and template was not available),
# it instantiates a {Tower.Controller.Responder} with the controller and resource.
#
# @example
# index: ->
# App.User.all (error, users) =>
# @respondWith(users)
#
# @example With simple handler
# index: ->
# App.User.all (error, users) =>
# @respondWith users, (format) =>
# format.html()
# format.json => @render json: users
#
# @example With success and failure handlers
# index: ->
# App.User.all (error, users) =>
# @respondWith users, (success, failure) =>
# success.json => @render json: users
# failure.json => @render text: "Error!", status: 404
respondWith: ->
args = _.args(arguments)
callback = null
Expand Down
3 changes: 2 additions & 1 deletion src/tower/http/param.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ class Tower.HTTP.Param
@pageKey: "page"
@separator: "_" # or "-"

@create: (key, options) ->
@create: (key, options = {}) ->
options = type: options if typeof options == "string"
options.type ||= "String"
new Tower.HTTP.Param[options.type](key, options)

Expand Down

0 comments on commit 682f2fa

Please sign in to comment.