Permalink
Browse files

Now using coffee-dsl

  • Loading branch information...
camshaft committed Sep 28, 2012
1 parent a3c4009 commit 6165ba9072145c1b823ac2a6aa2c29c0674a3711
Showing with 117 additions and 155 deletions.
  1. +64 −65 README.md
  2. +47 −85 lib/index.coffee
  3. +1 −0 package.json
  4. +5 −5 test/cscj.test.coffee
View
129 README.md
@@ -30,78 +30,77 @@ cscj is a little templating library that makes writing the Collection+JSON media
@template ->
@datum name: "firstName", prompt: "First Name"
@datum name: "lastName", prompt: "Last Name"
-
```
which would render
```json
{
- "collection": {
- "href": "http://localhost:5000",
- "version": "1.0",
- "error": {
- "code": "404",
- "message": "This is a test"
- },
- "links": [
- {
- "href": "http://localhost:5000",
- "rel": "index"
- }
- ],
- "items": [
- {
- "href": "http://localhost:5000/people/1",
- "links": [
- {
- "href": "http://localhost:5000/1/photos",
- "rel": "photos"
- }
- ],
- "data": [
- {
- "name": "firstName",
- "value": "Cameron",
- "prompt": "First Name"
- },
- {
- "name": "lastName",
- "value": "Bytheway",
- "prompt": "Last Name"
- }
- ]
- }
- ],
- "queries": [
- {
- "href": "http://localhost:5000/people",
- "rel": "people",
- "data": [
- {
- "name": "firstName",
- "prompt": "First Name"
- },
- {
- "name": "lastName",
- "prompt": "Last Name"
- }
- ]
+ "collection":{
+ "href":"http://localhost:5000",
+ "version":"1.0",
+ "error":{
+ "code":"404",
+ "message":"This is a test"
+ },
+ "links":[
+ {
+ "href":"http://localhost:5000",
+ "rel":"index"
+ }
+ ],
+ "items":[
+ {
+ "href":"http://localhost:5000/people/1",
+ "links":[
+ {
+ "href":"http://localhost:5000/1/photos",
+ "rel":"photos"
+ }
+ ],
+ "data":[
+ {
+ "name":"firstName",
+ "value":"Cameron",
+ "prompt":"First Name"
+ },
+ {
+ "name":"lastName",
+ "value":"Bytheway",
+ "prompt":"Last Name"
+ }
+ ]
+ }
+ ],
+ "queries":[
+ {
+ "href":"http://localhost:5000/people",
+ "rel":"people",
+ "data":[
+ {
+ "name":"firstName",
+ "prompt":"First Name"
+ },
+ {
+ "name":"lastName",
+ "prompt":"Last Name"
+ }
+ ]
+ }
+ ],
+ "template":{
+ "data":[
+ {
+ "name":"firstName",
+ "prompt":"First Name"
+ },
+ {
+ "name":"lastName",
+ "prompt":"Last Name"
+ }
+ ]
+ }
}
- ],
- "template": {
- "data": [
- {
- "name": "firstName",
- "prompt": "First Name"
- },
- {
- "name": "lastName",
- "prompt": "Last Name"
- }
- ]
- }
- }
}
```
View
@@ -1,87 +1,49 @@
-fs = require "fs"
-coffee = require "coffee-script"
-CollectionJSON = require "collection-json"
-
-readCache = {}
-cacheStore = {}
-requires = {}
-
-render = (str, renderOptions, done)->
- shimmed =
- """
- (()->
- #{replaceAll(str, '\n','\n\t')}
- ).call __cscj
- """
-
- collectionScope = (collection)->
- error: (options)->
- collection.getError options
- href: (value)->
- collection.href = value
- link: (options)->
- collection.addLink options
- item: (itemFun)->
- item = collection.addItem href: collection.href
- itemFun.call itemScope(item)
- query: (queryFun)->
- query = collection.addQuery href: collection.href, rel: "new-query"
- queryFun.call queryScope(query)
- template: (templateFun)->
- template = collection.getTemplate href: collection.href
- templateFun.call templateScope(template)
-
- itemScope = (item)->
- href: (value)->
- item.href = value
- link: (options)->
- item.addLink options
- datum: (options)->
- item.addDatum options
-
- queryScope = (query)->
- href: (value)->
- query.href = value
- rel: (value)->
- query.rel = value
- datum: (options)->
- query.addDatum options
+cscj = require("coffee-dsl").dsl()
- templateScope = (template)->
- href: (value)->
- template.href = value
- datum: (options)->
- template.addDatum options
-
- __cscj =
- collection: (collectionFun)->
- collection = CollectionJSON.create()
- collectionFun.call collectionScope(collection)
- collection.toJSON()
-
- renderOptions.__cscj = __cscj
-
- try
- result = coffee.eval shimmed, sandbox:renderOptions
- done null, result
- catch e
- done e
-
-module.exports = (path, options, done)->
- fs.readFile path, "utf8", (err, str)->
- return done err if err
- render str, options, done
-
-module.exports.render = render
+CollectionJSON = require "collection-json"
-# http://fagnerbrack.com/en/2012/03/27/javascript-replaceall/
-# Faster than str.replace
-replaceAll = (str, token, newToken, ignoreCase) ->
- i = -1
- _token = undefined
- if typeof token is "string"
- _token = (if ignoreCase is true then token.toLowerCase() else `undefined`)
- str = str.substring(0, i)
- .concat(newToken)
- .concat(str.substring(i + token.length)) while (i = ((if _token isnt `undefined` then str.toLowerCase().indexOf(_token, (if i >= 0 then i + newToken.length else 0)) else str.indexOf(token, (if i >= 0 then i + newToken.length else 0))))) isnt -1
- str
+collectionScope = (collection)->
+ error: (options)->
+ collection.getError options
+ href: (value)->
+ collection.href = value
+ link: (options)->
+ collection.addLink options
+ item: (itemFun)->
+ item = collection.addItem href: collection.href
+ itemFun.call itemScope(item)
+ query: (queryFun)->
+ query = collection.addQuery href: collection.href, rel: "new-query"
+ queryFun.call queryScope(query)
+ template: (templateFun)->
+ template = collection.getTemplate href: collection.href
+ templateFun.call templateScope(template)
+
+itemScope = (item)->
+ href: (value)->
+ item.href = value
+ link: (options)->
+ item.addLink options
+ datum: (options)->
+ item.addDatum options
+
+queryScope = (query)->
+ href: (value)->
+ query.href = value
+ rel: (value)->
+ query.rel = value
+ datum: (options)->
+ query.addDatum options
+
+templateScope = (template)->
+ href: (value)->
+ template.href = value
+ datum: (options)->
+ template.addDatum options
+
+cscj.set "collection", (collectionFun)->
+ collection = CollectionJSON.create()
+ collectionFun.call collectionScope(collection)
+ collection.toJSON()
+
+module.exports = cscj
View
@@ -13,6 +13,7 @@
"author": "Cameron Bytheway",
"license": "BSD",
"dependencies": {
+ "coffee-dsl": "~0.0.0",
"coffee-script": "~1.3.3",
"collection-json": "~0.0.0"
},
View
@@ -19,13 +19,13 @@ expectations = (error, result)->
describe "CoffeeScript Collection+JSON", ->
it "should render a basic view", (done)->
- cscj "#{__dirname}/views/basic.coffee", {}, (error, result)->
+ cscj.renderFile "#{__dirname}/views/basic.coffee", {}, (error, result)->
expectations error, result
done()
it "should render a view with variables", (done)->
- cscj "#{__dirname}/views/variables.coffee", {site: "http://localhost:5000"}, (error, result)->
+ cscj.renderFile "#{__dirname}/views/variables.coffee", {site: "http://localhost:5000"}, (error, result)->
expectations error, result
done()
@@ -40,12 +40,12 @@ describe "CoffeeScript Collection+JSON", ->
firstName: "Cameron"
lastName: "Bytheway"
- cscj "#{__dirname}/views/helpers.coffee", options, (error, result)->
+ cscj.renderFile "#{__dirname}/views/helpers.coffee", options, (error, result)->
expectations error, result
done()
it "should give an error when a variable is not in scope", (done)->
- cscj "#{__dirname}/views/variables.coffee", {}, (error, result)->
+ cscj.renderFile "#{__dirname}/views/variables.coffee", {}, (error, result)->
should.exist error
done()
@@ -54,7 +54,7 @@ describe "CoffeeScript Collection+JSON", ->
message = "Just for fun"
urlFor = (otps)->
throw new Error message
- cscj "#{__dirname}/views/helpers.coffee", {urlFor:urlFor}, (error, result)->
+ cscj.renderFile "#{__dirname}/views/helpers.coffee", {urlFor:urlFor}, (error, result)->
should.exist error
error.message.should.equal message
done()

0 comments on commit 6165ba9

Please sign in to comment.