Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 49 additions & 9 deletions lib/orpheus.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -322,24 +322,64 @@ 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
constructor: (id, @model) ->

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"redis": "0.10.0"
},
"dependencies": {
"async": "0.1.22",
"async": "0.9.0",
"underscore": "1.4.3"
},
"bugs": {
Expand Down
34 changes: 31 additions & 3 deletions test/orpheus.spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,7 @@ describe 'Get', ->

# Final callback, making sure we got everything right
, (err, results) ->
expect(err).toBeNull()
expect(err).toBeUndefined()
done()


Expand Down Expand Up @@ -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) ->
Expand All @@ -1439,3 +1466,4 @@ describe 'Regressions', ->
.exec (err, res) ->
expect(res.points).toBe(0)
done()