Skip to content

Commit

Permalink
0.5.0 support for the function
Browse files Browse the repository at this point in the history
  • Loading branch information
Radagaisus committed Jan 3, 2014
1 parent 9df6f25 commit 44569d5
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 10 deletions.
23 changes: 18 additions & 5 deletions dist/orpheus.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions history.md
Expand Up @@ -36,6 +36,28 @@ Instead of:
}


- Add a new function for all dynamic key models, `key`, that stores and then uses the parameters it was supplied with as the dynamic key arguments for the command. For example:

```coffee
class User extends orpheus
constructor: ->
@set 'books', key: (genre) -> "books:#{genre}"

user = orpheus.schema.user

user(user_id)
.books.key('scifi').add('Hyperion')
.exec()
```

Might be more convenient than:

```coffee
user(uder_id)
.books.add('Hyperion', key: 'scifi')
.exec()
```

- Support for NodeJS 0.10 and above


Expand Down
20 changes: 16 additions & 4 deletions lib/orpheus.coffee
Expand Up @@ -340,6 +340,11 @@ class OrpheusAPI
if value.type in ['set', 'zset', 'hash', 'list']
for f in commands.keys
@_create_command key, value, f
# Add a `key` function, that supplies dynamic key arguments for keys.
do (key) =>
@[key].key = (args...) =>
@[key]._key_arguments = args
return @[key]

for rel in @rels
prel = inflector.pluralize(rel)
Expand Down Expand Up @@ -492,11 +497,18 @@ class OrpheusAPI

type = @model[key].type

# Generate a new key name, if it has a dynamic key function.
# Generate a new key name, if it has a dynamic key function, and if it's not
# just a request to get the regular model hash.
if key and @model[key].options.key
# Dynamic keys arguments can be passed in an array, or, if it's just one
# key, as is.
dynamic_key_args = [dynamic_key_args] unless _.isArray(dynamic_key_args)
# If the dynamic key was supploed as a paramter, we use that. Otherwise,
# we use the `@[key]._key_arguments` that was supplied using the `key`
# function.
if dynamic_key_args
# Dynamic keys arguments can be passed in an array, or, if it's just one
# key, as is.
dynamic_key_args = [dynamic_key_args] unless _.isArray(dynamic_key_args)
else
dynamic_key_args = @[key]._key_arguments
# Call the model key function with the dynamic key arguments to get the key
key = @model[key].options.key.apply(this, dynamic_key_args)

Expand Down
17 changes: 16 additions & 1 deletion test/orpheus.spec.coffee
Expand Up @@ -151,7 +151,22 @@ describe 'Redis Commands', ->
r.hget "#{PREFIX}:te:hello", "test:yeah", (err, res) ->
expect(res).toBe 'Barış'
done()


it 'Dynamic key arguments, using the key function', (done) ->

class Test extends Orpheus
constructor: ->
@str 'something', key: (one, two) -> "#{one}:#{two}"

test = Test.create()

test('hi')
.something.key('ha', 'ha').set('boom')
.exec ->
r.hget "#{PREFIX}:te:hi", "ha:ha", (err, res) ->
expect(res).toBe 'boom'
done()


it 'Num and Str single commands', (done) ->
class Player extends Orpheus
Expand Down

0 comments on commit 44569d5

Please sign in to comment.