Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: markevans/egg-js
base: f94b108086
...
head fork: markevans/egg-js
compare: c05101a7a2
Checking mergeability… Don't worry, you can still create the pull request.
  • 3 commits
  • 8 files changed
  • 0 commit comments
  • 1 contributor
View
1  .gitignore
@@ -1,3 +1,4 @@
lib/egg.js
lib/egg.min.js
test/tests.js
+test/tests.coffee
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
61 src/index.coffee
@@ -1,12 +1,49 @@
-#= require ./core_ext
-#= require ./init
-#= require ./publisher
-#= require ./events
-#= require ./base
-#= require ./rest_api
-#= require ./model
-#= require ./active_record
-#= require ./scope
-#= require ./model_index
-#= require ./view
-#= require ./js_model_view
+rootKeyFor = (attrNames)->
+ attrNames.sort().join('-')
+
+class egg.Index extends egg.Base
+
+ @indexes = {}
+
+ @for: (modelClass, attrNames)->
+ @indexes[modelClass.name]?[rootKeyFor(attrNames)]
+
+ @init (opts)->
+ @modelClass = opts.modelClass
+ @attrNames = opts.attrNames.sort()
+ @models = {}
+
+ # Add to store of indexes
+ @constructor.indexes[@modelClass.name] ?= {}
+ @constructor.indexes[@modelClass.name][rootKeyFor(@attrNames)] = @
+
+ # Bind to model changes
+ @modelClass.on 'init', (params)=>
+ @add(params.instance, params.instance.attrs())
+
+ @modelClass.on 'change', (params)=>
+ @remove(params.instance, params.from)
+ @add(params.instance, params.to)
+
+ @modelClass.on 'destroy', (params)=>
+ @remove(params.instance, params.instance.attrs())
+
+ modelKey: (attrs)->
+ values = []
+ for key in @attrNames
+ values.push attrs[key]
+ values.join('-')
+
+ find: (attrs)->
+ @where(attrs).takeOne()
+
+ where: (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
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
44 src/model_index.coffee
@@ -1,44 +0,0 @@
-rootKeyFor = (attrNames)->
- attrNames.sort().join('-')
-
-class egg.ModelIndex extends egg.Base
-
- @indexes = {}
-
- @for: (modelClass, attrNames)->
- @indexes[modelClass.name]?[rootKeyFor(attrNames)]
-
- @init (opts)->
- @modelClass = opts.modelClass
- @attrNames = opts.attrNames.sort()
- @models = {}
-
- # Add to store of indexes
- @constructor.indexes[@modelClass.name] ?= {}
- @constructor.indexes[@modelClass.name][rootKeyFor(@attrNames)] = @
-
- # Bind to model changes
- @modelClass.on 'init', (params)=>
- @addToIndex(params.instance, params.instance.attrs())
-
- @modelClass.on 'change', (params)=>
- @removeFromIndex(params.instance, params.from)
- @addToIndex(params.instance, params.to)
-
- @modelClass.on 'destroy', (params)=>
- @removeFromIndex(params.instance, params.instance.attrs())
-
- modelKey: (attrs)->
- values = []
- for key in @attrNames
- values.push attrs[key]
- values.join('-')
-
- find: (attrs)->
- @models[@modelKey(attrs)]
-
- addToIndex: (model, attrs)->
- @models[@modelKey(attrs)] = model
-
- removeFromIndex: (model, attrs)->
- delete @models[@modelKey(attrs)]
View
10 src/set.coffee
@@ -16,4 +16,12 @@ class egg.Set
i
has: (item)->
- item.eggID && (item.eggID() of @items)
+ item.eggID && (item.eggID() of @items)
+
+ toArray: ->
+ array = []
+ array.push v for k, v of @items
+ array
+
+ takeOne: ->
+ return v for k, v of @items
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()
View
21 test/src/set.coffee
@@ -32,3 +32,24 @@ describe 'egg.Set', ->
set.remove b1
expect(set.has(b1)).toBeFalsy()
expect(set.count()).toEqual(1)
+
+ it "should not add things twice", ->
+ bean = Bean.create()
+ set.add bean
+ set.add bean
+ expect(set.count()).toEqual(1)
+
+ it "should give an array in the order added", ->
+ bean1 = Bean.create()
+ bean2 = Bean.create()
+ set.add bean1
+ set.add bean2
+ expect(set.toArray()).toEqual([bean1, bean2])
+
+ it "should allow taking one (in fact the first added)", ->
+ bean1 = Bean.create()
+ bean2 = Bean.create()
+ set.add bean1
+ set.add bean2
+ expect( set.takeOne() ).toEqual(bean1)
+

No commit comments for this range

Something went wrong with that request. Please try again.