Created
March 12, 2012 15:18
-
-
Save doitian/2022574 to your computer and use it in GitHub Desktop.
Revisions
-
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,44 @@ #>>lang=cf @Factory = Factory = {} ids = {} sequences = {} sequence = (name, callback) -> sequences[name] = callback define = (name, defaults = {}) -> Factory[name] = (attrs = {}) -> result = $.extend {}, defaults, attrs for k, v of result if typeof v is 'function' result[k] = v.call(result, result) result next = (name) -> -> ids[name] = (ids[name] ? 0) + 1 console.log ids[name] sequences[name]?(ids[name]) sequence 'email', (n) -> "user#{n}@exmaple.com" one = (name) -> -> Factory[name]() many = (name, n = 1) -> -> (Factory[name]() for i in [1..n]) define 'book', name: 'Funning Book' define 'profile', name: 'Test' email: next('email') # if an attribute depends on other, must defined after nickname: -> @name define 'user', profile: one('profile') books: many('book', 3) This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,19 @@ # SON.stringify([Factory.user(), Factory.user()], null, 2) [ { "profile": { "name": "Test", "email": "[email protected]", "nickname": "Test" }, "books": [ { "name": "Funning Book" }, { "name": "Funning Book" }, { "name": "Funning Book" } ] }, { "profile": { "name": "Test", "email": "[email protected]", "nickname": "Test" }, "books": [ { "name": "Funning Book" }, { "name": "Funning Book" }, { "name": "Funning Book" } ] } ]