agt 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 907efa700bf068d2d6143df8bd544ccb06bbb7e1
4
- data.tar.gz: 1379aee9576449ae0096a86819aec4abcd3233ba
3
+ metadata.gz: a406216ffe4c22a137bafce7daf8091192ef9ff8
4
+ data.tar.gz: b50acc2eb11e37e86047c8d40514c23ddc80373d
5
5
  SHA512:
6
- metadata.gz: dfa83250404df0ab969df7aad87e044802ce05a025f1572aca7972071c085aaaa279c43bf80cb30a3b5c95817d6881e40c1d7611eebfc852e0b11b9eee2ae226
7
- data.tar.gz: 021341fda32e429e14dc4b378868bee6d5c2a8ec2f556b9239d1674ea396c517a417a3321a1ddcaef5e7701135700b07d288e396decab1304f3681f1fcbe215c
6
+ metadata.gz: 21cb2c74a2c0c2b83c9391d9f965c11358dfcd3159313dc404ccb428a2c43928fef081845073f58ca09887ab6e73ffb00577e04eda95dc940207c754ee70e88f
7
+ data.tar.gz: 5323ced22723deb1f3691e71a84bf6dab6a6c04dbdbbdc9010d6d287ba30f01b701dfac4e256b5444238608a1b858ccbe51cf6c42719648ff5f94d8497b39e39
data/CHANGELOG.md CHANGED
@@ -1,3 +1,11 @@
1
+ <a name="0.0.4"></a>
2
+ # 0.0.4 (2014-09-09)
3
+
4
+ ## :sparkles: Features
5
+
6
+ - Add support for toProperty and toMethod options in delegation ([fdd08fc2](https://github.com/abe33/agt.git/commit/fdd08fc29e6cd2404e62bc8fbd63e611c958e48e))
7
+ - Add draft of models mixing ([3101ce1d](https://github.com/abe33/agt.git/commit/3101ce1d855cdb40c088bcb7e5ce50a174fbb25b))
8
+
1
9
  <a name="v0.0.3"></a>
2
10
  # v0.0.3 (2014-08-28)
3
11
 
@@ -1,5 +1,12 @@
1
+ namespace('agt.i18n')
1
2
  # Public
2
- class window.I18n
3
+ class agt.i18n.I18n
4
+ @attachToWindow: ->
5
+ instance = new I18n locales, $('html').attr('lang') or 'en'
6
+ window.t = instance.getHelper()
7
+
8
+ String::t = -> window.t(this)
9
+
3
10
  ### Public ###
4
11
 
5
12
  constructor: (@locales={}, @defaultLanguage='en') ->
@@ -15,20 +22,27 @@ class window.I18n
15
22
  # in the path as a capitalized sentence.
16
23
  #
17
24
  # i18n.get (path.that.do_not_exist) # Do Not Exist
18
- get: (language, path) ->
19
- [language, path] = [@defaultLanguage, language] unless path?
25
+ get: (language, path, tokens={}) ->
26
+ if !path? or typeof path is 'object'
27
+ [language, path, tokens] = [@defaultLanguage, language, path]
28
+
20
29
  lang = @locales[language]
21
30
 
22
31
  throw new Error "Language #{language} not found" unless lang?
23
32
  els = path.split('.')
24
33
  (lang = lang[v]; break unless lang?) for v in els
25
34
 
35
+ if typeof lang is 'object' and tokens.count?
36
+ lang = lang[tokens.count] ? lang.other
37
+
26
38
  unless lang?
27
39
  lang = els[-1..][0].replace(/[-_]/g, ' ')
28
40
  .replace(/(^|\s)(\w)/g, (m,sp,s) ->
29
41
  "#{sp}#{s.toUpperCase()}")
30
42
 
31
- lang
43
+ lang.replace /\#\{([^\}]+)\}/g, (token, key) ->
44
+ return token unless tokens[key]?
45
+ tokens[key]
32
46
 
33
47
  # Returns a helper function bound to the current instance that allow
34
48
  # to retrieve localized string from the `I18n` instance as well as doing
@@ -39,15 +53,4 @@ class window.I18n
39
53
  # _('path.to.string')
40
54
  # _('path.to.string_with_token', token: 'token substitute')
41
55
  # ```
42
- getHelper: -> (path, tokens={}) =>
43
- @get(path).replace /\#\{([^\}]+)\}/g, (token, key) ->
44
- return token unless tokens[key]?
45
- tokens[key]
46
-
47
- if window? and document?
48
- document.addEventListener 'load', f = ->
49
- instance = new I18n locales, $('html').attr('lang') or 'en'
50
- window.t = instance.getHelper()
51
- document.removeEventListener 'load', f
52
-
53
- String::t = -> window.t(this)
56
+ getHelper: -> (path, tokens={}) => @get(path, tokens)
@@ -64,7 +64,11 @@ class agt.mixins.Delegation
64
64
  # :case - An optional {String} to define the case to use to generate
65
65
  # a prefixed delegated property.
66
66
  @delegate: (properties..., options={}) ->
67
- delegated = options.to
67
+ {toProperty, toMethod} = options
68
+ toProperty ||= options.to
69
+
70
+ delegated = toMethod ? toProperty
71
+
68
72
  prefixed = options.prefix
69
73
  _case = options.case or agt.CAMEL_CASE
70
74
 
@@ -80,11 +84,22 @@ class agt.mixins.Delegation
80
84
  localAlias = delegated + property.replace /^./, (m) ->
81
85
  m.toUpperCase()
82
86
 
83
- # The `Delegation` mixin rely on `Object.property` and thus can't
84
- # be used on IE8.
85
- Object.defineProperty @prototype, localAlias, {
86
- enumerable: true
87
- configurable: true
88
- get: -> @[ delegated ][ property ]
89
- set: (value) -> @[ delegated ][ property ] = value
90
- }
87
+ if delegated is toMethod
88
+ Object.defineProperty @prototype, localAlias, {
89
+ enumerable: true
90
+ configurable: true
91
+ get: -> @[ delegated ]()
92
+ set: (value) -> @[ delegated ](value)
93
+ }
94
+
95
+ else
96
+ # The `Delegation` mixin rely on `Object.property` and thus can't
97
+ # be used on IE8.
98
+ Object.defineProperty @prototype, localAlias, {
99
+ enumerable: true
100
+ configurable: true
101
+ get: -> @[ delegated ][ property ]
102
+ set: (value) -> @[ delegated ][ property ] = value
103
+ }
104
+
105
+ @delegates: @delegate
@@ -0,0 +1,151 @@
1
+ namespace('agt.models')
2
+
3
+ # Creates a collection class for the given model. This class
4
+ # will be decorated with the scopes defined on the model class
5
+ # so it have to be specific to the model class.
6
+ buildCollectionClass = (model) ->
7
+
8
+ # The Collection class behaves mostly like an array except that
9
+ # every methods that should return an array return a collection
10
+ # instead.
11
+ class Collection
12
+ @extend agt.mixins.Delegation
13
+
14
+ @model: model
15
+
16
+ # We can't use `new Collection` because Collection's instances
17
+ # are not proxy. Use `Collection.create` instead.
18
+ @create: (content) ->
19
+ collection = new @(content)
20
+ return collection unless typeof Proxy is 'function'
21
+ new Proxy collection, {
22
+ get: (target, name) ->
23
+ # Methods are simply bound to the target and returned.
24
+ if typeof target[name] is 'function'
25
+ target[name].bind(target)
26
+ else
27
+ if /-?\d+/.test(name)
28
+ # With this we can call `collection[index]` and access the content
29
+ # of the collection array.
30
+ index = parseInt(name)
31
+ index = target.length + index if index < 0
32
+
33
+ target.array[index]
34
+ else
35
+ target[name]
36
+ }
37
+
38
+ @delegatesArrayMethods: (methods...) ->
39
+ methods.forEach (method) =>
40
+ @::[method] = -> @array[method](arguments...)
41
+
42
+ @delegatesArrayReturningMethods: (methods...) ->
43
+ methods.forEach (method) =>
44
+ @::[method] = -> @constructor.create(@array[method](arguments...))
45
+
46
+ @delegates 'length', toProperty: 'array'
47
+
48
+ @delegatesArrayMethods 'push', 'pop', 'shift', 'unshift', 'forEach', 'some', 'every', 'indexOf', 'reduce', 'join'
49
+
50
+ @delegatesArrayReturningMethods 'concat', 'splice', 'slice', 'filter', 'reverse', 'sort'
51
+
52
+ model: model
53
+
54
+ constructor: (@array=[]) ->
55
+
56
+ first: -> @array[0]
57
+
58
+ last: -> @array[@length - 1]
59
+
60
+ map: (block) ->
61
+ results = if typeof block is 'string'
62
+ @array.map (el) -> el[block]
63
+ else
64
+ @array.map(block)
65
+
66
+ new @constructor(results)
67
+
68
+ distinct: (field) ->
69
+ values = []
70
+
71
+ @forEach (instance) ->
72
+ values.push(instance[field]) unless instance[field] in values
73
+
74
+ values
75
+
76
+ where: (conditions={}) ->
77
+ @filter (model) => @matchConditions(model, conditions)
78
+
79
+ matchConditions: (model, conditions) ->
80
+ res = true
81
+ for k,v of conditions
82
+ if typeof v is 'function'
83
+ res &&= v(model[k])
84
+ else
85
+ res &&= model[k] is v
86
+ res
87
+
88
+ class agt.models.Model
89
+ @extend agt.mixins.Delegation
90
+
91
+ @delegatesCollectionMethods: (methods...) ->
92
+ methods.forEach (method) => @[method] = -> @instances[method](arguments...)
93
+
94
+ @delegatesCollectionMethods 'first', 'last', 'where', 'distinct'
95
+
96
+ ### Public ###
97
+
98
+ # Initializes the instances collection on the model's class.
99
+ @initializeCollection: ->
100
+ return if @initialized
101
+
102
+ @Collection = buildCollectionClass(this)
103
+
104
+ @instances = @Collection.create([])
105
+ @nextId = 1
106
+
107
+ @initialized = true
108
+
109
+ @create_collection: (content=[]) ->
110
+ @initializeCollection()
111
+
112
+ @Collection.create(content)
113
+
114
+ @register: (instance) ->
115
+ @initializeCollection()
116
+
117
+ instance.id = @nextId++
118
+ @instances.push(instance)
119
+
120
+ @unregister: (instance) ->
121
+ @instances.splice(@instances.indexOf(instance), 1) if instance in @instances
122
+
123
+ ### Scopes ###
124
+
125
+ @all: ->
126
+ @initializeCollection()
127
+
128
+ @instances.concat()
129
+
130
+ @scope: (name, block) ->
131
+ @initializeCollection()
132
+
133
+ @Collection::[name] = (args...) ->
134
+ @filter (instance, index) -> block([instance].concat(args)...)
135
+
136
+ @delegatesCollectionMethods name
137
+
138
+ ### Queries ###
139
+
140
+ @find: (id) ->
141
+ @initializeCollection()
142
+
143
+ @where({id}).first()
144
+
145
+ @find_or_create: (conditions={}) ->
146
+ @initializeCollection()
147
+
148
+ instance = @where(conditions).first()
149
+ return instance if instance?
150
+
151
+ new @(conditions)
data/lib/agt/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  module Agt
2
2
  module Rails
3
- VERSION = "0.0.3"
3
+ VERSION = "0.0.4"
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.3
4
+ version: 0.0.4
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-08-28 00:00:00.000000000 Z
11
+ date: 2014-09-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -99,6 +99,7 @@ files:
99
99
  - app/assets/javascripts/agt/mixins/sourcable.coffee
100
100
  - app/assets/javascripts/agt/mixins/state_machine.coffee
101
101
  - app/assets/javascripts/agt/mixins/subscriber.coffee
102
+ - app/assets/javascripts/agt/models/mixins/model.coffee
102
103
  - app/assets/javascripts/agt/net/router.coffee
103
104
  - app/assets/javascripts/agt/object.coffee
104
105
  - app/assets/javascripts/agt/particles/actions/base_action.coffee