agt 0.0.4 → 0.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a406216ffe4c22a137bafce7daf8091192ef9ff8
4
- data.tar.gz: b50acc2eb11e37e86047c8d40514c23ddc80373d
3
+ metadata.gz: 6d8dccd0f25aabdb783a8f30d525ba1fe9541266
4
+ data.tar.gz: 18e725c31cb93f2844ae52f68cef53286c8830ae
5
5
  SHA512:
6
- metadata.gz: 21cb2c74a2c0c2b83c9391d9f965c11358dfcd3159313dc404ccb428a2c43928fef081845073f58ca09887ab6e73ffb00577e04eda95dc940207c754ee70e88f
7
- data.tar.gz: 5323ced22723deb1f3691e71a84bf6dab6a6c04dbdbbdc9010d6d287ba30f01b701dfac4e256b5444238608a1b858ccbe51cf6c42719648ff5f94d8497b39e39
6
+ metadata.gz: b25e48db126cd1c7de237e001ffeb8579b9cf1e40f5a53e2ef07884a9bf7ed700424535b8c14c11fc0d16d5acb52c814277708b6029a8f63d79f4002c0c01141
7
+ data.tar.gz: f592ca2a69860e754c22acd9e065579c63534fbbcd9e7c22b87e687a4a8e3a4fd7d0eea57168297e28e43aa72ebf019538ad509e4be6db5e0e5448880a6d8c03
@@ -1,5 +1,13 @@
1
- <a name="0.0.4"></a>
2
- # 0.0.4 (2014-09-09)
1
+ a name="v0.0.5"></a>
2
+ # v0.0.5 (2015-01-23)
3
+
4
+ ## :bug: Bug Fixes
5
+
6
+ - Fix event related issues on IE8 ([10b771df](https://github.com/abe33/agt.git/commit/10b771dfb9a5809b2df9309d7f9b89762815ee65))
7
+ - Add a fallback for mixins used in IE8 ([faec4cdb](https://github.com/abe33/agt.git/commit/faec4cdb25e393c2e65e289ef4046cd117f46e96))
8
+
9
+ <a name="v0.0.4"></a>
10
+ # v0.0.4 (2014-09-09)
3
11
 
4
12
  ## :sparkles: Features
5
13
 
@@ -5,8 +5,13 @@ agt.domEvent = (type, data={}, options={bubbles, cancelable}={}) ->
5
5
  cancelable: cancelable ? true
6
6
  }
7
7
  catch e
8
- event = document.createEvent 'Event'
9
- event.initEvent type, bubbles ? true, cancelable ? true
8
+ if document.createEvent?
9
+ event = document.createEvent 'Event'
10
+ event.initEvent type, bubbles ? true, cancelable ? true
11
+ else if document.createEventObject?
12
+ event = document.createEventObject()
13
+ event.type = type
14
+ event[k] = v for k,v of options
10
15
 
11
16
  event.data = data
12
17
  event
@@ -1,3 +1,6 @@
1
+
2
+ warn = (msg) -> console?.warn?(msg)
3
+
1
4
  ### Public ###
2
5
 
3
6
  # Creates a new non-enumerable method on the current class prototype.
@@ -10,11 +13,15 @@
10
13
  # name - The {String} for the method name.
11
14
  # block - The {Function} body.
12
15
  Function::def = (name, block) ->
13
- Object.defineProperty @prototype, name, {
14
- value: block
15
- configurable: true
16
- enumerable: false
17
- }
16
+ if Object.NO_DEFINE_PROPERTY
17
+ warn "Using Function::def in a context where Object.defineProperty is not supported, falling back to prototype decoration."
18
+ @prototype[name] = block
19
+ else
20
+ Object.defineProperty @prototype, name, {
21
+ value: block
22
+ configurable: true
23
+ enumerable: false
24
+ }
18
25
  this
19
26
 
20
27
  # Creates a virtual property on the current class's prototype.
@@ -42,13 +49,15 @@ Function::accessor = (name, options) ->
42
49
 
43
50
  options.get ||= oldDescriptor.get if oldDescriptor?
44
51
  options.set ||= oldDescriptor.set if oldDescriptor?
45
-
46
- Object.defineProperty @prototype, name, {
47
- get: options.get
48
- set: options.set
49
- configurable: true
50
- enumerable: true
51
- }
52
+ if Object.NO_DEFINE_PROPERTY
53
+ throw new Error "Using Function::accessor in a context where Object.defineProperty is not supported."
54
+ else
55
+ Object.defineProperty @prototype, name, {
56
+ get: options.get
57
+ set: options.set
58
+ configurable: true
59
+ enumerable: true
60
+ }
52
61
  this
53
62
 
54
63
  # Creates a getter on the given class prototype.
@@ -150,72 +159,80 @@ Function::include = (mixins...) ->
150
159
  # We loop through all the enumerable properties of the mixin's
151
160
  # prototype that is not marked for exclusion.
152
161
  keys = Object.keys mixin.prototype
153
- for k in keys
154
- if k not in excl
155
-
156
- # We prefer working with property descriptors rather than with
157
- # the plain values.
158
- oldDescriptor = Object.getPropertyDescriptor @prototype, k
159
- newDescriptor = Object.getPropertyDescriptor mixin.prototype, k
160
-
161
- # If the two descriptors are available we'll have to go deeper.
162
- if oldDescriptor? and newDescriptor?
163
- oldHasAccessor = oldDescriptor.get? or oldDescriptor.set?
164
- newHasAccessor = newDescriptor.get? or newDescriptor.set?
165
- bothHaveGet = oldDescriptor.get? and newDescriptor.get?
166
- bothHaveSet = oldDescriptor.set? and newDescriptor.set?
167
- bothHaveValue = oldDescriptor.value? and newDescriptor.value?
168
-
169
- # When both properties are accessors we'll be able to follow
170
- # the super accross them.
171
- #
172
- # Super methods are registered if both are there for getters
173
- # and setters.
174
- if oldHasAccessor and newHasAccessor
175
- registerSuper k, newDescriptor.get, @, oldDescriptor.get, mixin if bothHaveGet
176
- registerSuper k, newDescriptor.set, @, oldDescriptor.set, mixin if bothHaveSet
177
-
178
- # If there was a getter or a setter and the new accessor
179
- # doesn't define one them, the previous value is used.
180
- newDescriptor.get ||= oldDescriptor.get
181
- newDescriptor.set ||= oldDescriptor.set
182
-
183
- # When both have a value, the super is also available.
184
- else if bothHaveValue
185
- registerSuper k, newDescriptor.value, @, oldDescriptor.value, mixin
186
162
 
187
- else
188
- throw new Error "Can't mix accessors and plain values inheritance"
189
-
190
- # We also have to create the property on the class `__super__`
191
- # property. It'll allow the method defined on the class itself
192
- # and overriding the property to have access to its super property
193
- # through the `super` keyword or with `this.super` method.
194
- Object.defineProperty @__super__, k, newDescriptor
195
-
196
- # We only have a descriptor for the new property, the previous
197
- # one is just added to the class `__super__` property.
198
- else if newDescriptor?
199
- @__super__[k] = mixin[k]
200
-
201
- # We only have a descriptor for the previous property, we'll
202
- # create it on the class `__super__` property.
203
- else if oldDescriptor?
204
- Object.defineProperty @__super__, k, newDescriptor
205
-
206
- # No descriptors at all. The super property is attached directly
207
- # to the value.
208
- else if @::[k]?
209
- registerSuper k, mixin[k], @, @::[k], mixin
210
- @__super__[k] = mixin[k]
211
-
212
- # With a descriptor the new property is created using
213
- # `Object.defineProperty` or by affecting the value
214
- # to the prototype.
215
- if newDescriptor?
216
- Object.defineProperty @prototype, k, newDescriptor
217
- else
163
+ if Object.NO_DEFINE_PROPERTY
164
+ warn "Using Function::include in a context where Object.defineProperty is not supported. Falling back to direct prototype decoration."
165
+ for k in keys
166
+ if k not in excl
167
+ @__super__[k] = mixin::[k]
218
168
  @::[k] = mixin::[k]
169
+ else
170
+ for k in keys
171
+ if k not in excl
172
+
173
+ # We prefer working with property descriptors rather than with
174
+ # the plain values.
175
+ oldDescriptor = Object.getPropertyDescriptor @prototype, k
176
+ newDescriptor = Object.getPropertyDescriptor mixin.prototype, k
177
+
178
+ # If the two descriptors are available we'll have to go deeper.
179
+ if oldDescriptor? and newDescriptor?
180
+ oldHasAccessor = oldDescriptor.get? or oldDescriptor.set?
181
+ newHasAccessor = newDescriptor.get? or newDescriptor.set?
182
+ bothHaveGet = oldDescriptor.get? and newDescriptor.get?
183
+ bothHaveSet = oldDescriptor.set? and newDescriptor.set?
184
+ bothHaveValue = oldDescriptor.value? and newDescriptor.value?
185
+
186
+ # When both properties are accessors we'll be able to follow
187
+ # the super accross them.
188
+ #
189
+ # Super methods are registered if both are there for getters
190
+ # and setters.
191
+ if oldHasAccessor and newHasAccessor
192
+ registerSuper k, newDescriptor.get, @, oldDescriptor.get, mixin if bothHaveGet
193
+ registerSuper k, newDescriptor.set, @, oldDescriptor.set, mixin if bothHaveSet
194
+
195
+ # If there was a getter or a setter and the new accessor
196
+ # doesn't define one them, the previous value is used.
197
+ newDescriptor.get ||= oldDescriptor.get
198
+ newDescriptor.set ||= oldDescriptor.set
199
+
200
+ # When both have a value, the super is also available.
201
+ else if bothHaveValue
202
+ registerSuper k, newDescriptor.value, @, oldDescriptor.value, mixin
203
+
204
+ else
205
+ throw new Error "Can't mix accessors and plain values inheritance"
206
+
207
+ # We also have to create the property on the class `__super__`
208
+ # property. It'll allow the method defined on the class itself
209
+ # and overriding the property to have access to its super property
210
+ # through the `super` keyword or with `this.super` method.
211
+ Object.defineProperty @__super__, k, newDescriptor
212
+
213
+ # We only have a descriptor for the new property, the previous
214
+ # one is just added to the class `__super__` property.
215
+ else if newDescriptor?
216
+ Object.defineProperty @__super__, k, newDescriptor
217
+
218
+ # We only have a descriptor for the previous property, we'll
219
+ # create it on the class `__super__` property.
220
+ else if oldDescriptor?
221
+ @__super__[k] = mixins::[k]
222
+
223
+ # No descriptors at all. The super property is attached directly
224
+ # to the value.
225
+ else if @::[k]?
226
+ registerSuper k, mixin::[k], @, @::[k], mixin
227
+ @__super__[k] = mixin::[k]
228
+
229
+ # With a descriptor the new property is created using
230
+ # `Object.defineProperty` or by affecting the value
231
+ # to the prototype.
232
+ if newDescriptor?
233
+ Object.defineProperty @prototype, k, newDescriptor
234
+ else
235
+ @::[k] = mixin::[k]
219
236
 
220
237
  # The `included` hook is triggered on the mixin.
221
238
  mixin.included? this
@@ -250,46 +267,51 @@ Function::extend = (mixins...) ->
250
267
  excl = excl.concat mixin.excluded if mixin.excluded?
251
268
 
252
269
  keys = Object.keys mixin
253
- for k in keys
254
- if k not in excl
255
- oldDescriptor = Object.getPropertyDescriptor this, k
256
- newDescriptor = Object.getPropertyDescriptor mixin, k
257
-
258
- if oldDescriptor? and newDescriptor?
259
- oldHasAccessor = oldDescriptor.get? or oldDescriptor.set?
260
- newHasAccessor = newDescriptor.get? or newDescriptor.set?
261
- bothHaveGet = oldDescriptor.get? and newDescriptor.get?
262
- bothHaveSet = oldDescriptor.set? and newDescriptor.set?
263
- bothHaveValue = oldDescriptor.value? and newDescriptor.value?
264
-
265
- # When both properties are accessors we'll be able to follow
266
- # the super accross them
267
- #
268
- # Super methods are registered if both are there for getters
269
- # and setters.
270
- if oldHasAccessor and newHasAccessor
271
- registerSuper k, newDescriptor.get, @, oldDescriptor.get, mixin if bothHaveGet
272
- registerSuper k, newDescriptor.set, @, oldDescriptor.set, mixin if bothHaveSet
273
-
274
- # If there was a getter or a setter and the new accessor
275
- # doesn't define one them, the previous value is used.
276
- newDescriptor.get ||= oldDescriptor.get
277
- newDescriptor.set ||= oldDescriptor.set
278
-
279
- # When both have a value, the super is also available.
280
- else if bothHaveValue
281
- registerSuper k, newDescriptor.value, @, oldDescriptor.value, mixin
282
270
 
271
+ if Object.NO_DEFINE_PROPERTY
272
+ warn "Using Function::include in a context where Object.defineProperty is not supported. Falling back to direct prototype decoration."
273
+ @[k] = mixin[k] for k in keys when k not in excl
274
+ else
275
+ for k in keys
276
+ if k not in excl
277
+ oldDescriptor = Object.getPropertyDescriptor this, k
278
+ newDescriptor = Object.getPropertyDescriptor mixin, k
279
+
280
+ if oldDescriptor? and newDescriptor?
281
+ oldHasAccessor = oldDescriptor.get? or oldDescriptor.set?
282
+ newHasAccessor = newDescriptor.get? or newDescriptor.set?
283
+ bothHaveGet = oldDescriptor.get? and newDescriptor.get?
284
+ bothHaveSet = oldDescriptor.set? and newDescriptor.set?
285
+ bothHaveValue = oldDescriptor.value? and newDescriptor.value?
286
+
287
+ # When both properties are accessors we'll be able to follow
288
+ # the super accross them
289
+ #
290
+ # Super methods are registered if both are there for getters
291
+ # and setters.
292
+ if oldHasAccessor and newHasAccessor
293
+ registerSuper k, newDescriptor.get, @, oldDescriptor.get, mixin if bothHaveGet
294
+ registerSuper k, newDescriptor.set, @, oldDescriptor.set, mixin if bothHaveSet
295
+
296
+ # If there was a getter or a setter and the new accessor
297
+ # doesn't define one them, the previous value is used.
298
+ newDescriptor.get ||= oldDescriptor.get
299
+ newDescriptor.set ||= oldDescriptor.set
300
+
301
+ # When both have a value, the super is also available.
302
+ else if bothHaveValue
303
+ registerSuper k, newDescriptor.value, @, oldDescriptor.value, mixin
304
+
305
+ else
306
+ throw new Error "Can't mix accessors and plain values inheritance"
307
+
308
+ # With a descriptor the new property is created using
309
+ # `Object.defineProperty` or by affecting the value
310
+ # to the prototype.
311
+ if newDescriptor?
312
+ Object.defineProperty this, k, newDescriptor
283
313
  else
284
- throw new Error "Can't mix accessors and plain values inheritance"
285
-
286
- # With a descriptor the new property is created using
287
- # `Object.defineProperty` or by affecting the value
288
- # to the prototype.
289
- if newDescriptor?
290
- Object.defineProperty this, k, newDescriptor
291
- else
292
- @[k] = mixin[k]
314
+ @[k] = mixin[k]
293
315
 
294
316
  mixin.extended? this
295
317
 
@@ -1,11 +1,11 @@
1
- namespace('agt.models')
1
+ namespace('agt.mixins')
2
2
 
3
- # Creates a collection class for the given model. This class
3
+ # Internal: Creates a collection class for the given model. This class
4
4
  # will be decorated with the scopes defined on the model class
5
5
  # so it have to be specific to the model class.
6
6
  buildCollectionClass = (model) ->
7
7
 
8
- # The Collection class behaves mostly like an array except that
8
+ # Public: The Collection class behaves mostly like an array except that
9
9
  # every methods that should return an array return a collection
10
10
  # instead.
11
11
  class Collection
@@ -28,9 +28,7 @@ buildCollectionClass = (model) ->
28
28
  # With this we can call `collection[index]` and access the content
29
29
  # of the collection array.
30
30
  index = parseInt(name)
31
- index = target.length + index if index < 0
32
-
33
- target.array[index]
31
+ target.get(index)
34
32
  else
35
33
  target[name]
36
34
  }
@@ -53,17 +51,36 @@ buildCollectionClass = (model) ->
53
51
 
54
52
  constructor: (@array=[]) ->
55
53
 
56
- first: -> @array[0]
54
+ get: (index) ->
55
+ index = @length + index if index < 0
56
+ @array[index]
57
+
58
+ first: -> @get(0)
57
59
 
58
- last: -> @array[@length - 1]
60
+ last: -> @get(-1)
61
+
62
+ where: (conditions={}) ->
63
+ @filter (model) => @matchConditions(model, conditions)
59
64
 
60
65
  map: (block) ->
61
- results = if typeof block is 'string'
62
- @array.map (el) -> el[block]
66
+ if typeof block is 'string'
67
+ @distinct(block)
63
68
  else
64
69
  @array.map(block)
65
70
 
66
- new @constructor(results)
71
+ sum: (field) -> @reduce ((a,b) -> a + b[field]), 0
72
+
73
+ group: (key) ->
74
+ results = {}
75
+
76
+ for record in @array
77
+ slot = record[key]
78
+ slot = slot.id if typeof slot is 'object' and slot.id?
79
+ results[slot] ?= @constructor.create()
80
+ results[slot].push record
81
+
82
+ results
83
+
67
84
 
68
85
  distinct: (field) ->
69
86
  values = []
@@ -73,9 +90,6 @@ buildCollectionClass = (model) ->
73
90
 
74
91
  values
75
92
 
76
- where: (conditions={}) ->
77
- @filter (model) => @matchConditions(model, conditions)
78
-
79
93
  matchConditions: (model, conditions) ->
80
94
  res = true
81
95
  for k,v of conditions
@@ -85,13 +99,14 @@ buildCollectionClass = (model) ->
85
99
  res &&= model[k] is v
86
100
  res
87
101
 
88
- class agt.models.Model
102
+ # Public:
103
+ class agt.mixins.Collectionable
89
104
  @extend agt.mixins.Delegation
90
105
 
91
106
  @delegatesCollectionMethods: (methods...) ->
92
107
  methods.forEach (method) => @[method] = -> @instances[method](arguments...)
93
108
 
94
- @delegatesCollectionMethods 'first', 'last', 'where', 'distinct'
109
+ @delegatesCollectionMethods 'first', 'last', 'where', 'distinct', 'sum', 'group'
95
110
 
96
111
  ### Public ###
97
112
 
@@ -101,7 +116,7 @@ class agt.models.Model
101
116
 
102
117
  @Collection = buildCollectionClass(this)
103
118
 
104
- @instances = @Collection.create([])
119
+ @instances = @Collection.create()
105
120
  @nextId = 1
106
121
 
107
122
  @initialized = true
@@ -120,6 +135,9 @@ class agt.models.Model
120
135
  @unregister: (instance) ->
121
136
  @instances.splice(@instances.indexOf(instance), 1) if instance in @instances
122
137
 
138
+ @unregisterAll: ->
139
+ @instances = @Collection.create()
140
+
123
141
  ### Scopes ###
124
142
 
125
143
  @all: ->
@@ -7,3 +7,11 @@ unless Object.getPropertyDescriptor?
7
7
  descriptor
8
8
  else
9
9
  Object.getPropertyDescriptor = -> undefined
10
+
11
+ Object.NO_DEFINE_PROPERTY = not Object.defineProperty?
12
+
13
+ if Object.defineProperty?
14
+ try
15
+ Object.defineProperty(Array.prototype,'test', value: 'test')
16
+ catch e
17
+ Object.NO_DEFINE_PROPERTY = true
@@ -107,7 +107,11 @@ agt.widgets = (name, selector, options={}, block) ->
107
107
  else
108
108
  widget.activate()
109
109
 
110
- document.dispatchEvent agt.domEvent("#{name}:handled", {element, widget})
110
+ event = agt.domEvent("#{name}:handled", {element, widget})
111
+ if document.dispatchEvent?
112
+ document.dispatchEvent(event)
113
+ else
114
+ console?.warn? 'HTMLElement::dispatchEvent is not available on this platform. Unable to dispatch custom events on DOM nodes.'
111
115
 
112
116
  block?.call element, element, widget
113
117
 
@@ -1,5 +1,5 @@
1
1
  module Agt
2
2
  module Rails
3
- VERSION = "0.0.4"
3
+ VERSION = "0.0.5"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: agt
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Cédric Néhémie
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-09-09 00:00:00.000000000 Z
11
+ date: 2015-01-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -84,6 +84,7 @@ files:
84
84
  - app/assets/javascripts/agt/mixins/aliasable.coffee
85
85
  - app/assets/javascripts/agt/mixins/alternate_case.coffee
86
86
  - app/assets/javascripts/agt/mixins/cloneable.coffee
87
+ - app/assets/javascripts/agt/mixins/collectionable.coffee
87
88
  - app/assets/javascripts/agt/mixins/delegation.coffee
88
89
  - app/assets/javascripts/agt/mixins/disposable.coffee
89
90
  - app/assets/javascripts/agt/mixins/emitter.coffee
@@ -99,7 +100,6 @@ files:
99
100
  - app/assets/javascripts/agt/mixins/sourcable.coffee
100
101
  - app/assets/javascripts/agt/mixins/state_machine.coffee
101
102
  - app/assets/javascripts/agt/mixins/subscriber.coffee
102
- - app/assets/javascripts/agt/models/mixins/model.coffee
103
103
  - app/assets/javascripts/agt/net/router.coffee
104
104
  - app/assets/javascripts/agt/object.coffee
105
105
  - app/assets/javascripts/agt/particles/actions/base_action.coffee