Permalink
Browse files

Updated documentation

  • Loading branch information...
1 parent 82e0705 commit 93a018a700dd72eb24b9ef2a46437e838e4149a2 @camshaft camshaft committed Oct 2, 2012
Showing with 246 additions and 45 deletions.
  1. +8 −0 LICENSE
  2. +237 −44 README.md
  3. +1 −1 package.json
View
8 LICENSE
@@ -0,0 +1,8 @@
+The MIT License (MIT)
+Copyright (c) 2012 Cameron Bytheway <cameron@nujii.com>
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
View
281 README.md
@@ -1,7 +1,234 @@
CoffeeScript meets Collection+JSON (cscj) [![Build Status](https://secure.travis-ci.org/CamShaft/cscj.png)](http://travis-ci.org/CamShaft/cscj)
=========================================
-cscj is a little templating library that makes writing the Collection+JSON media type painless:
+cscj is a little templating library that makes writing the Collection+JSON media type painless.
+
+Public API
+----------
+
+```coffee
+cscj = require "cscj"
+
+view = """
+@collection ->
+ @href "http://example.com"
+"""
+
+fn = cscj.compile view, options
+fn(locals)
+```
+
+### Options
+
+* `locals` Local variable object
+* `filename` Used in exceptions
+* `debug` Outputs tokens and function body generated
+
+Syntax
+------
+
+A collection is started by defining the root:
+
+```coffee
+@collection ->
+
+ # Collection parts go here
+
+```
+
+### Collection
+
+```coffee
+@collection ->
+
+ # Let's set our `href`
+ @href "http://example.com"
+
+ # Oops there was an error!
+ @error title: "Not Found!", code: "404", message: "This is not the collection your are looking for"
+
+ # We can also specify the error type
+ @error "password", title: "Invalid password": code: "400", message: "Too short"
+
+ # We can add some links
+ @link href: "http://example.com", rel: "index"
+ @link href: "http://example.com/users", rel: "users"
+ @link href: "http://example.com/posts", rel: "posts"
+
+ # Add an item
+ @item ->
+
+ # Item parts go here
+
+ # Add a query
+ @query ->
+
+ # Query parts go here
+
+ # Add a template
+ @template ->
+
+ # Template parts go here
+
+```
+
+### Item
+
+```coffee
+@item ->
+
+ # Href
+ @href "http://example.com/users/1"
+
+ # Links
+ @link href: "http://example.com/users/1/photos", rel: "photos"
+ @link href: "http://example.com/users/1/friends", rel: "friends"
+
+ # Data
+ @datum name: "firstName", value: "Cameron", prompt: "First Name"
+ @datum name: "lastName", value: "Bytheway", prompt: "Last Name"
+```
+
+### Query
+
+```coffee
+@query ->
+
+ # Href
+ @href "http://example.com/users"
+
+ # Rel
+ @rel "users"
+
+ # Data
+ @datum name: "firstName", prompt: "First Name"
+ @datum name: "lastName", prompt: "Last Name"
+
+ # Optional
+
+ # Encoding is defined by the extension for URI Templates
+ # https://github.com/mamund/collection-json/blob/master/extensions/uri-templates.md
+ @encoding "uri-template"
+```
+
+### Template
+
+```coffee
+@template ->
+
+ # Data
+ @datum name: "firstName", prompt: "First Name"
+ @datum name: "lastName", prompt: "Last Name"
+
+ # Optional
+
+ # The following are defined by the extension for multiple templates
+ # https://github.com/mamund/collection-json/blob/master/extensions/templates.md
+
+ @href "http://example.com/users/avatar"
+
+ @rel "avatar"
+
+ @type "application/x-www-form-urlencoded"
+
+ @name "avatar"
+```
+
+Referencing Local Variables
+---------------------------
+
+When we pass in locals, they are referenced as normal variables:
+
+```coffee
+# example.coffee
+cscj = require "cscj"
+
+cscj.renderFile "./view.coffee", {root: "http://example.com"}, (error, collection)->
+ console.log collection
+```
+
+```coffee
+# view.coffee
+@collection ->
+
+ @href root
+```
+
+Running this code will output:
+
+```json
+{
+ "collection":{
+ "href":"http://example.com",
+ "version":"1.0"
+ }
+}
+```
+
+You can also define helpers:
+
+```coffee
+# example2.coffee
+cscj = require "cscj"
+
+locals =
+ links: (collection)->
+ collection.link rel: "index", href: "http://example.com"
+ collection.link rel: "users", href: "http://example.com/users"
+ root: "http://example.com"
+
+cscj.renderFile "./view.coffee", locals, (error, collection)->
+ console.log collection
+```
+
+```coffee
+# view.coffee
+@collection ->
+
+ @href root
+ links @
+```
+
+Running this code will output:
+
+```json
+{
+ "collection":{
+ "href":"http://example.com",
+ "version":"1.0",
+ "links": [
+ { "href": "http://example.com", "rel": "index" },
+ { "href": "http://example.com/users", "rel": "users" }
+ ]
+ }
+}
+```
+
+Using CoffeeScript
+------------------
+
+Since the template is just CoffeeScript you can do some powerful stuff:
+
+```coffee
+@collection ->
+ ...
+ for item in items
+ @item ->
+
+ @href item.href
+
+ # Iterate the links in the item
+ for link in item.links
+ @link rel: link.rel, href: link.href
+
+ # Iterate the keys in the item and add them to the `data` array
+ for key, value in item
+ @datum name: key, value: value if not (key in ["href", "links"])
+ ...
+```
+
+Example
+-------
```coffee
# views/index.coffee
@@ -39,36 +266,19 @@ which would render
"collection":{
"href":"http://localhost:5000",
"version":"1.0",
- "error":{
- "code":"404",
- "message":"This is a test"
- },
+ "error":{ "code":"404", "message":"This is a test" },
"links":[
- {
- "href":"http://localhost:5000",
- "rel":"index"
- }
+ { "href":"http://localhost:5000", "rel":"index" }
],
"items":[
{
"href":"http://localhost:5000/people/1",
"links":[
- {
- "href":"http://localhost:5000/1/photos",
- "rel":"photos"
- }
+ { "href":"http://localhost:5000/1/photos", "rel":"photos" }
],
"data":[
- {
- "name":"firstName",
- "value":"Cameron",
- "prompt":"First Name"
- },
- {
- "name":"lastName",
- "value":"Bytheway",
- "prompt":"Last Name"
- }
+ { "name":"firstName", "value":"Cameron", "prompt":"First Name" },
+ { "name":"lastName", "value":"Bytheway", "prompt":"Last Name" }
]
}
],
@@ -77,38 +287,21 @@ which would render
"href":"http://localhost:5000/people",
"rel":"people",
"data":[
- {
- "name":"firstName",
- "prompt":"First Name"
- },
- {
- "name":"lastName",
- "prompt":"Last Name"
- }
+ { "name":"firstName", "prompt":"First Name" },
+ { "name":"lastName", "prompt":"Last Name" }
]
}
],
"template":{
"data":[
- {
- "name":"firstName",
- "prompt":"First Name"
- },
- {
- "name":"lastName",
- "prompt":"Last Name"
- }
+ { "name":"firstName", "prompt":"First Name" },
+ { "name":"lastName", "prompt":"Last Name" }
]
}
}
}
```
-Usage
------
-
-Just add it to your dependencies in `package.json` and you're ready to go!
-
Tests
-----
```bash
View
2 package.json
@@ -11,7 +11,7 @@
},
"repository": "",
"author": "Cameron Bytheway",
- "license": "BSD",
+ "license": "MIT",
"dependencies": {
"coffee-dsl": "~0.0.0",
"coffee-script": "~1.3.3"

0 comments on commit 93a018a

Please sign in to comment.