Skip to content
Browse files

Initial commit

  • Loading branch information...
0 parents commit 0fa3bbde33719fd3db81d056c2dc677a1b150175 @camshaft camshaft committed Sep 28, 2012
Showing with 247 additions and 0 deletions.
  1. +1 −0 .gitignore
  2. +2 −0 index.js
  3. +87 −0 lib/index.coffee
  4. +23 −0 package.json
  5. +60 −0 test/cscj.test.coffee
  6. +26 −0 test/views/basic.coffee
  7. +24 −0 test/views/helpers.coffee
  8. +24 −0 test/views/variables.coffee
1 .gitignore
@@ -0,0 +1 @@
+node_modules
2 index.js
@@ -0,0 +1,2 @@
+require("coffee-script");
+module.exports = require("./lib");
87 lib/index.coffee
@@ -0,0 +1,87 @@
+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
+
+ 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
+
+# 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
23 package.json
@@ -0,0 +1,23 @@
+{
+ "name": "cscj",
+ "version": "0.0.0",
+ "description": "Collection+JSON Views in CoffeeScript",
+ "main": "index.js",
+ "directories": {
+ "test": "test"
+ },
+ "scripts": {
+ "test": "./node_modules/.bin/mocha -R spec --compilers coffee:coffee-script test"
+ },
+ "repository": "",
+ "author": "Cameron Bytheway",
+ "license": "BSD",
+ "dependencies": {
+ "coffee-script": "~1.3.3",
+ "collection-json": "~0.0.0"
+ },
+ "devDependencies": {
+ "mocha": "~1.3.2",
+ "should": "~1.1.0"
+ }
+}
60 test/cscj.test.coffee
@@ -0,0 +1,60 @@
+
+should = require "should"
+
+cscj = require ".."
+
+expectations = (error, result)->
+ should.not.exist error
+ should.exist result
+ should.exist result.collection
+ should.exist result.collection.version
+ should.exist result.collection.error
+ should.exist result.collection.links
+ should.exist result.collection.items
+ should.exist result.collection.queries
+ should.exist result.collection.template
+ result.collection.links.length.should.equal 1
+ result.collection.items.length.should.equal 1
+
+describe "CoffeeScript Collection+JSON", ->
+ it "should render a basic view", (done)->
+
+ cscj "#{__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)->
+ expectations error, result
+ done()
+
+ it "should render a view with helpers", (done)->
+
+ urlFor = (otps)->
+ "http://localhost:5000"
+
+ options =
+ urlFor: urlFor
+ item:
+ firstName: "Cameron"
+ lastName: "Bytheway"
+
+ cscj "#{__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)->
+ should.exist error
+ done()
+
+
+ it "should give an error when a helper throws an exception", (done)->
+ message = "Just for fun"
+ urlFor = (otps)->
+ throw new Error message
+ cscj "#{__dirname}/views/helpers.coffee", {urlFor:urlFor}, (error, result)->
+ should.exist error
+ error.message.should.equal message
+ done()
26 test/views/basic.coffee
@@ -0,0 +1,26 @@
+
+@collection ->
+
+ root = "http://localhost:5000"
+
+ @error code: "404", message: "This is a test"
+
+ @href root
+
+ @link href: root, rel: "index"
+
+ @item ->
+ @href "#{root}/people/1"
+ @link href: "#{root}/1/photos", rel: "photos"
+ @datum name: "firstName", value: "Cameron", prompt: "First Name"
+ @datum name: "lastName", value: "Bytheway", prompt: "Last Name"
+
+ @query ->
+ @href "#{root}/people"
+ @rel "people"
+ @datum name: "firstName", prompt: "First Name"
+ @datum name: "lastName", prompt: "Last Name"
+
+ @template ->
+ @datum name: "firstName", prompt: "First Name"
+ @datum name: "lastName", prompt: "Last Name"
24 test/views/helpers.coffee
@@ -0,0 +1,24 @@
+
+@collection ->
+
+ @error code: "404", message: "This is a test"
+
+ @href urlFor controller: "index"
+
+ @link href: urlFor(controller: "index"), rel: "index"
+
+ @item ->
+ @href urlFor item
+ @link href: urlFor(controller: "photos"), rel: "photos"
+ for k, v of item
+ @datum name: k, value: v
+
+ @query ->
+ @href urlFor controller: "people"
+ @rel "people"
+ @datum name: "firstName", prompt: "First Name"
+ @datum name: "lastName", prompt: "Last Name"
+
+ @template ->
+ @datum name: "firstName", prompt: "First Name"
+ @datum name: "lastName", prompt: "Last Name"
24 test/views/variables.coffee
@@ -0,0 +1,24 @@
+
+@collection ->
+
+ @error code: "404", message: "This is a test"
+
+ @href site
+
+ @link href: site, rel: "index"
+
+ @item ->
+ @href "#{site}/people/1"
+ @link href: "#{site}/1/photos", rel: "photos"
+ @datum name: "firstName", value: "Cameron", prompt: "First Name"
+ @datum name: "lastName", value: "Bytheway", prompt: "Last Name"
+
+ @query ->
+ @href "#{site}/people"
+ @rel "people"
+ @datum name: "firstName", prompt: "First Name"
+ @datum name: "lastName", prompt: "Last Name"
+
+ @template ->
+ @datum name: "firstName", prompt: "First Name"
+ @datum name: "lastName", prompt: "Last Name"

0 comments on commit 0fa3bbd

Please sign in to comment.
Something went wrong with that request. Please try again.