Navigation Menu

Skip to content

Commit

Permalink
dynamic key retrieval
Browse files Browse the repository at this point in the history
  • Loading branch information
Radagaisus committed Sep 6, 2012
1 parent 97c6503 commit 6e924c1
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 6 deletions.
17 changes: 15 additions & 2 deletions README.md
Expand Up @@ -272,12 +272,25 @@ class User extends Orpheus
user = User.create()
user('jackson')
.monthly_ranking.incrby(1, 'Midnight Oil - The Dead Heart')
.err ->
res.json status: 400
.exec ->
res.json status: 200
```

Using arguments in dyanmic keys is easy:

```coffee
@zset 'monthly_ranking'
key: (year, month) ->
"ranking:#{year || d.getFullYear()}:#{month || d.getMonth()+1}"

# later on, in a far away place...
user('bean')
.monthly_ranking.incrby(1, 'Stoned Jesus - I'm The Mountain', key: [2012, 12])
```
Everything inside the `key` object will be passed to the dynamic key function.
## One to One Maps
Maps are used to map between a unique attribute of the model and the model ID.
Expand Down
12 changes: 8 additions & 4 deletions lib/orpheus.coffee
Expand Up @@ -270,13 +270,17 @@ class OrpheusAPI
@operations = ['add', 'set', 'del']
for f in @operations
@_add_op f


_create_command: (key, value, f) ->
type = value.type

@[key][f] = (args...) =>

# Pop out dynamic key arguments, if any
if _.isObject(args[args.length-1]) and args[args.length-1].key
dynamic_key_args = args.pop().key

# Type Conversion
# ------------------------
# Convert types for all the commands
Expand Down Expand Up @@ -306,7 +310,7 @@ class OrpheusAPI
return

# Add multi command
@_commands.push _.flatten [f, @_get_key(key), args]
@_commands.push _.flatten [f, @_get_key(key, dynamic_key_args), args]

# Response Schema
# --------------------
Expand Down Expand Up @@ -375,7 +379,7 @@ class OrpheusAPI
"#{host}#{pid}#{time}#{counter}#{random}"


_get_key: (key) ->
_get_key: (key, dynamic_key_args) ->
# Default: orpheus:pl:15
k = "#{@prefix}:#{@q}:#{@id}"

Expand All @@ -393,7 +397,7 @@ class OrpheusAPI
# generate a new key name, if it has a
# dynamic key function.
if key and @model[key].options.key
key = @model[key].options.key()
key = @model[key].options.key.apply(this, dynamic_key_args)

if type is 'str' or type is 'num'
# orpheus:us:15:name somename
Expand Down
24 changes: 24 additions & 0 deletions test/orpheus.spec.coffee
Expand Up @@ -91,6 +91,30 @@ describe 'Redis Commands', ->

done()

it 'Dynamic Keys Arguments', (done) ->
class User extends Orpheus
constructor: ->
@str 'name',
key: (var1, var2) ->
var1 ||= 'hello'
var2 ||= 'sup'
"#{var1}:#{var2}"
user = User.create()

user('hello')
.name.set('shalom')
.exec ->
r.hget "#{PREFIX}:us:hello", "hello:sup", (err, res) ->
expect(res).toBe 'shalom'

user('hiho')
.name.set('shalom', key: ['what', 'is'])
.exec ->

r.hget "#{PREFIX}:us:hiho", "what:is", (err, res) ->
expect(res).toBe 'shalom'
done()

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

0 comments on commit 6e924c1

Please sign in to comment.