Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Removed ModelIndex -> Index and made it work for sets rather than ind…

…ividual models
  • Loading branch information...
commit c05101a7a28cc146733c55ff7bea88a7d7b05569 1 parent f041358
@markevans authored
View
3  Cakefile
@@ -33,7 +33,7 @@ task 'build', 'Build single application file from source files', ->
'src/model'
'src/active_record'
'src/scope'
- 'src/model_index'
+ 'src/index'
'src/view'
'src/js_model_view'
], 'lib/egg'
@@ -49,5 +49,6 @@ task 'build_tests', 'Build tests from source files', ->
build [
'test/src/base'
'test/src/set'
+ 'test/src/index'
'test/src/scope'
], 'test/tests'
View
27 src/index.coffee
@@ -1,7 +1,7 @@
rootKeyFor = (attrNames)->
attrNames.sort().join('-')
-class egg.ModelIndex extends egg.Base
+class egg.Index extends egg.Base
@indexes = {}
@@ -19,14 +19,14 @@ class egg.ModelIndex extends egg.Base
# Bind to model changes
@modelClass.on 'init', (params)=>
- @addToIndex(params.instance, params.instance.attrs())
+ @add(params.instance, params.instance.attrs())
@modelClass.on 'change', (params)=>
- @removeFromIndex(params.instance, params.from)
- @addToIndex(params.instance, params.to)
+ @remove(params.instance, params.from)
+ @add(params.instance, params.to)
@modelClass.on 'destroy', (params)=>
- @removeFromIndex(params.instance, params.instance.attrs())
+ @remove(params.instance, params.instance.attrs())
modelKey: (attrs)->
values = []
@@ -35,10 +35,15 @@ class egg.ModelIndex extends egg.Base
values.join('-')
find: (attrs)->
- @models[@modelKey(attrs)]
-
- addToIndex: (model, attrs)->
- @models[@modelKey(attrs)] = model
+ @where(attrs).takeOne()
+
+ where: (attrs)->
+ @models[@modelKey(attrs)] ?= new egg.Set
+
+ add: (model, attrs)->
+ set = @models[@modelKey(attrs)] ?= new egg.Set
+ set.add(model)
- removeFromIndex: (model, attrs)->
- delete @models[@modelKey(attrs)]
+ remove: (model, attrs)->
+ set = @models[@modelKey(attrs)]
+ set.remove(model) if set
View
4 src/model.coffee
@@ -28,7 +28,7 @@ egg.model = (klass)->
)
find: (attrs)->
- index = egg.ModelIndex.for(@, Object.keys(attrs))
+ index = egg.Index.for(@, Object.keys(attrs))
if index
index.find(attrs)
else
@@ -43,7 +43,7 @@ egg.model = (klass)->
@all().forEach (model)-> model.destroy()
index: (attrNames...)->
- egg.ModelIndex.create(modelClass: @, attrNames: attrNames)
+ egg.Index.create(modelClass: @, attrNames: attrNames)
count: ->
@all().count()
View
61 test/src/index.coffee
@@ -0,0 +1,61 @@
+describe 'egg.Index', ->
+
+ class window.Pea extends egg.Base
+ @use egg.model
+
+ index = null
+
+ beforeEach ->
+ Pea.destroyAll()
+
+ describe 'for', ->
+
+ beforeEach ->
+ index = egg.Index.create modelClass: Pea, attrNames: ['zinc', 'apple']
+
+ it "should return null if the index doesn't exist", ->
+ expect(egg.Index.for(Pea, ['doobie', 'do'])).toBeUndefined()
+ expect(egg.Index.for({}, ['zinc, apple'])).toBeUndefined()
+
+ it "should return the index if it exists", ->
+ expect(egg.Index.for(Pea, ['zinc', 'apple'])).toEqual(index)
+
+ it "shouldn't care about the order of the attrs", ->
+ expect(egg.Index.for(Pea, ['apple', 'zinc'])).toEqual(index)
+
+ describe 'auto-adding', ->
+
+ pea1 = null
+ pea2 = null
+ pea3 = null
+ pea4 = null
+
+ beforeEach ->
+ index = egg.Index.create modelClass: Pea, attrNames: ['zinc', 'apple']
+ pea1 = Pea.create attrs: {zinc: 1, apple: 'gog'}
+ pea2 = Pea.create attrs: {zinc: 1, apple: 'gog'}
+ pea3 = Pea.create attrs: {zinc: 'dimble', apple: 'gog'}
+ 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([])
+
+ 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])
+
+ 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()
Please sign in to comment.
Something went wrong with that request. Please try again.