Skip to content
Browse files

Simplified index

  • Loading branch information...
1 parent cde7b8c commit 112c05c72efb071c192c85c9c59401386966c092 Mark Evans committed Mar 27, 2012
Showing with 16 additions and 31 deletions.
  1. +5 −16 src/index.coffee
  2. +1 −1 src/model.coffee
  3. +10 −14 test/src/index.coffee
View
21 src/index.coffee
@@ -19,31 +19,20 @@ class egg.Index extends egg.Base
# Bind to model changes
@modelClass.on 'add', (params)=>
- @add(params.instance, params.instance.attrs())
+ @setFor(params.instance.attrs()).add(params.instance)
@modelClass.on 'change', (params)=>
- @remove(params.instance, params.from)
- @add(params.instance, params.to)
+ @setFor(params.to).add(params.instance)
+ @setFor(params.from).remove(params.instance)
@modelClass.on 'remove', (params)=>
- @remove(params.instance, params.instance.attrs())
+ @setFor(params.instance.attrs()).remove(params.instance)
modelKey: (attrs)->
values = []
for key in @attrNames
values.push attrs[key]
values.join('-')
- find: (attrs)->
- @where(attrs).first()
-
- where: (attrs)->
+ setFor: (attrs)->
@models[@modelKey(attrs)] ?= new egg.Set
-
- add: (model, attrs)->
- set = @models[@modelKey(attrs)] ?= new egg.Set
- set.add(model)
-
- remove: (model, attrs)->
- set = @models[@modelKey(attrs)]
- set.remove(model) if set
View
2 src/model.coffee
@@ -27,7 +27,7 @@ class egg.Model extends egg.Base
@where: (attrs)->
index = egg.Index.for(@, Object.keys(attrs))
if index
- index.where(attrs)
+ index.setFor(attrs)
else
@filter (model)->
for key, value of attrs
View
24 test/src/index.coffee
@@ -37,24 +37,20 @@ describe 'egg.Index', ->
pea4 = Pea.create attrs: {zinc: 'farm', apple: 'house'}
it "should be added when one is created", ->
- expect( index.where(zinc: 1, apple: 'gog' ).toArray() ).toEqual([pea1, pea2])
- expect( index.where(zinc: 'dimble', apple: 'gog' ).toArray() ).toEqual([pea3])
- expect( index.where(zinc: 'farm', apple: 'house').toArray() ).toEqual([pea4])
- expect( index.where(zinc: 'farm', apple: 'gog' ).toArray() ).toEqual([])
+ expect( index.setFor(zinc: 1, apple: 'gog' ).toArray() ).toEqual([pea1, pea2])
+ expect( index.setFor(zinc: 'dimble', apple: 'gog' ).toArray() ).toEqual([pea3])
+ expect( index.setFor(zinc: 'farm', apple: 'house').toArray() ).toEqual([pea4])
+ expect( index.setFor(zinc: 'farm', apple: 'gog' ).toArray() ).toEqual([])
it "should update when one is changed", ->
pea1.set('zinc', 'farm')
pea2.set('zinc', 'nuther')
- expect( index.where(zinc: 1, apple: 'gog' ).toArray() ).toEqual([])
- expect( index.where(zinc: 'dimble', apple: 'gog' ).toArray() ).toEqual([pea3])
- expect( index.where(zinc: 'farm', apple: 'house').toArray() ).toEqual([pea4])
- expect( index.where(zinc: 'farm', apple: 'gog' ).toArray() ).toEqual([pea1])
- expect( index.where(zinc: 'nuther', apple: 'gog' ).toArray() ).toEqual([pea2])
+ expect( index.setFor(zinc: 1, apple: 'gog' ).toArray() ).toEqual([])
+ expect( index.setFor(zinc: 'dimble', apple: 'gog' ).toArray() ).toEqual([pea3])
+ expect( index.setFor(zinc: 'farm', apple: 'house').toArray() ).toEqual([pea4])
+ expect( index.setFor(zinc: 'farm', apple: 'gog' ).toArray() ).toEqual([pea1])
+ expect( index.setFor(zinc: 'nuther', apple: 'gog' ).toArray() ).toEqual([pea2])
it "should update when one is destroyed", ->
pea2.destroy()
- expect( index.where(zinc: 1, apple: 'gog' ).toArray() ).toEqual([pea1])
-
- it "should find just one", ->
- expect( index.find(zinc: 1, apple: 'gog') ).toEqual(pea1)
- expect( index.find(zinc: 'farm', apple: 'gog') ).toBeUndefined()
+ expect( index.setFor(zinc: 1, apple: 'gog' ).toArray() ).toEqual([pea1])

0 comments on commit 112c05c

Please sign in to comment.
Something went wrong with that request. Please try again.