From 5e56e4039d20b099fddc25114ba56a01696fa767 Mon Sep 17 00:00:00 2001 From: Radagaisus Date: Fri, 29 Aug 2014 05:51:33 +0300 Subject: [PATCH 1/2] cleaned comment --- lib/orpheus.coffee | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/orpheus.coffee b/lib/orpheus.coffee index 56757ed..0533a40 100644 --- a/lib/orpheus.coffee +++ b/lib/orpheus.coffee @@ -339,7 +339,7 @@ class Orpheus # Orpheus API -#-------------------------------------# +# ------------------------------------------------------------------------------------ class OrpheusAPI constructor: (id, @model) -> From 5835603c72d6e1286e6205d77aaa2aefcfbaf378 Mon Sep 17 00:00:00 2001 From: Radagaisus Date: Fri, 29 Aug 2014 06:28:10 +0300 Subject: [PATCH 2/2] Updated AsyncJS dependency to 0.9.0; Introduced the collection API with 'each' and 'eachSeries'; added tests and fixed a test that needed an update --- lib/orpheus.coffee | 56 ++++++++++++++++++++++++++++++++++------ package.json | 2 +- test/orpheus.spec.coffee | 34 +++++++++++++++++++++--- 3 files changed, 80 insertions(+), 12 deletions(-) diff --git a/lib/orpheus.coffee b/lib/orpheus.coffee index 0533a40..9e402fd 100644 --- a/lib/orpheus.coffee +++ b/lib/orpheus.coffee @@ -322,22 +322,62 @@ class Orpheus # Throw an error, the `id` parameter was not passed correctly message = "Orpheus Model must be instantiated with a proper id" throw new Error(message) - - # Converts class Player to 'player' - name = @toString().match(/function (.*)\(/)[1].toLowerCase() - # Qualifier + # Converts class User to 'user' + name = @toString().match(/function (.*)\(/)[1].toLowerCase() + # Get the plural name + plural_name = inflector.pluralize(name) + # Get the model qualifier from the name q = name.substr(0,2) - + # Create the model model = new OrpheusModel(name, q) - - # Add to Schema + # Add the model to the schema Orpheus.schema[name] = model.id - + # Add the collection to the schema + Orpheus.schema[plural_name] = new OrpheusCollection(model.id) + # Return the model's `id` function, ready to be used return model.id +# Orpheus Collection API +# ------------------------------------------------------------------------------------- +class OrpheusCollection + + # Create the orpheus collection. Properly sets the name of the collection and the + # model the collection should use, and generates all the asynchronous functions + # that are available to be used on the collection's models. + constructor: (model) -> + # Set the name of the collection to the model's pluralized name + @name = model.pname + # The model we invoke with the collection methods + @model = model + # Dynamically generate all the collection's methods + @create_methods() + + + # Dynamically generate all the available methods. Each method's usage syntax is + # very similar: + # + # Collection.each ['id1', 'id2', 'id3'], + # action: (model, callback) -> model.do_stuff().exec(callback) + # callback: (err, collection = []) -> + # # stuff has been done on all the models, or an error occured + # + create_methods: => + # List of all the available collection methods + available_methods = ['each', 'eachSeries'] + # Run over all the methods + for method in available_methods + do (method) => + @[method] = (models = [], options = {}) -> + # Convert the model ids to ready to be used model APIs + models = (@model(id) for id in models) + # Call the specified `method` in AsyncJS with the models (ready to be used), + # the `action` function to run on each and the final `callback` function. + async[method](models, options.action, options.callback) + + # Orpheus API # ------------------------------------------------------------------------------------ class OrpheusAPI diff --git a/package.json b/package.json index 049676d..2092cec 100644 --- a/package.json +++ b/package.json @@ -11,7 +11,7 @@ "redis": "0.10.0" }, "dependencies": { - "async": "0.1.22", + "async": "0.9.0", "underscore": "1.4.3" }, "bugs": { diff --git a/test/orpheus.spec.coffee b/test/orpheus.spec.coffee index dfe39cf..735c7f7 100644 --- a/test/orpheus.spec.coffee +++ b/test/orpheus.spec.coffee @@ -514,7 +514,7 @@ describe 'Get', -> # Final callback, making sure we got everything right , (err, results) -> - expect(err).toBeNull() + expect(err).toBeUndefined() done() @@ -1414,9 +1414,36 @@ describe 'Connect', -> done() +# Collections API +# ----------------------------------------------------------------------------------- +describe 'Collections', -> + + it '#each', (done) -> + class User extends Orpheus + constructor: -> + @str 'name' + user = User.create() + users = Orpheus.schema.users -# Bugs & Regressions -# --------------------------------------- + users.each ['a', 'b', 'c'], + action: (user, callback) -> user.name.set(user.id).exec(callback) + callback: (err, results) -> + + r.multi() + .hget("#{PREFIX}:us:a", 'name') + .hget("#{PREFIX}:us:b", 'name') + .hget("#{PREFIX}:us:c", 'name') + .exec (err, results) -> + console.log '???', results + expect(results[0]).toEqual 'a' + expect(results[1]).toEqual 'b' + expect(results[2]).toEqual 'c' + done() + + + +# Bugs and Regressions +# ----------------------------------------------------------------------------------- describe 'Regressions', -> it 'Returns an object with a number field with 0 as the value', (done) -> @@ -1439,3 +1466,4 @@ describe 'Regressions', -> .exec (err, res) -> expect(res.points).toBe(0) done() +