From 6e924c198da892b0c6013d91fe510bb440018c73 Mon Sep 17 00:00:00 2001 From: Radagaisus Date: Thu, 6 Sep 2012 22:27:34 +0300 Subject: [PATCH] dynamic key retrieval --- README.md | 17 +++++++++++++++-- lib/orpheus.coffee | 12 ++++++++---- test/orpheus.spec.coffee | 24 ++++++++++++++++++++++++ 3 files changed, 47 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 0169db5..2fafb42 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/lib/orpheus.coffee b/lib/orpheus.coffee index dac691d..2cf6f44 100644 --- a/lib/orpheus.coffee +++ b/lib/orpheus.coffee @@ -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 @@ -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 # -------------------- @@ -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}" @@ -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 diff --git a/test/orpheus.spec.coffee b/test/orpheus.spec.coffee index 4097bd7..d1367fc 100644 --- a/test/orpheus.spec.coffee +++ b/test/orpheus.spec.coffee @@ -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: ->