vault-rails 0.0.8 → 0.0.9
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/vault-rails/version.rb +1 -1
- data/vendor/assets/javascripts/vault.js.coffee +59 -29
- metadata +2 -2
data/lib/vault-rails/version.rb
CHANGED
@@ -3,8 +3,11 @@ class Vault
|
|
3
3
|
# Setup some internal variables.
|
4
4
|
@objects = []
|
5
5
|
@dirty_object_count = 0
|
6
|
-
@errors = []
|
7
6
|
@save_error_count = 0
|
7
|
+
@messages =
|
8
|
+
notices: []
|
9
|
+
warnings: []
|
10
|
+
errors: []
|
8
11
|
|
9
12
|
# This property is used to temporarily lock the vault during mutation methods.
|
10
13
|
@locked = false
|
@@ -38,36 +41,63 @@ class Vault
|
|
38
41
|
if @load()
|
39
42
|
if @dirty_object_count > 0
|
40
43
|
# Offline data loaded and modifications found; keep existing data.
|
41
|
-
|
44
|
+
@messages.notices.push "Found and using dirty offline data."
|
45
|
+
|
42
46
|
# Detach the callback to after_load so that the call to the
|
43
47
|
# vault constructor can complete/return, allowing any post-load code
|
44
48
|
# to use the newly instantiated vault object as required.
|
45
49
|
window.setTimeout @options.after_load, 100
|
46
50
|
else
|
47
51
|
# No modifications in offline data; reload fresh data.
|
52
|
+
@messages.notices.push "No modifications found in offline data. Reloading..."
|
53
|
+
|
48
54
|
if @urls.list?
|
49
55
|
@reload(@options.after_load)
|
50
56
|
else
|
51
|
-
# Can't reload without a list url; use the offline data.
|
52
|
-
@
|
57
|
+
# Can't reload without a list url; use the offline data we've loaded.
|
58
|
+
@messages.notices.push "List url not configured; using offline data instead."
|
59
|
+
|
60
|
+
# Detach the callback to after_load so that the call to the
|
61
|
+
# vault constructor can complete/return, allowing any post-load code
|
62
|
+
# to use the newly instantiated vault object as required.
|
63
|
+
window.setTimeout @options.after_load, 100
|
53
64
|
else
|
54
65
|
if navigator.onLine
|
55
66
|
# Load failed, but we're connected; reload fresh data.
|
67
|
+
@messages.warnings.push "Offline data load failed. Reloading..."
|
68
|
+
|
56
69
|
if @urls.list?
|
57
70
|
@reload(@options.after_load)
|
58
71
|
else
|
59
72
|
# Can't reload without a list url; use an empty dataset.
|
60
|
-
@
|
73
|
+
@messages.warnings.push "List url not configured; using empty dataset instead."
|
74
|
+
|
75
|
+
# Detach the callback to after_load so that the call to the
|
76
|
+
# vault constructor can complete/return, allowing any post-load code
|
77
|
+
# to use the newly instantiated vault object as required.
|
78
|
+
window.setTimeout @options.after_load, 100
|
61
79
|
else
|
62
|
-
# Load failed and we're offline;
|
63
|
-
@
|
80
|
+
# Load failed and we're offline; use an empty dataset.
|
81
|
+
@messages.warnings.push "Browser is offline and cannot reload; using empty dataset instead."
|
82
|
+
|
83
|
+
# Detach the callback to after_load so that the call to the
|
84
|
+
# vault constructor can complete/return, allowing any post-load code
|
85
|
+
# to use the newly instantiated vault object as required.
|
86
|
+
window.setTimeout @options.after_load, 100
|
64
87
|
else
|
65
88
|
# Not using offline data; reload fresh data.
|
89
|
+
@messages.notices.push "Not configured for offline data. Reloading..."
|
90
|
+
|
66
91
|
if @urls.list?
|
67
92
|
@reload(@options.after_load)
|
68
93
|
else
|
69
94
|
# Can't reload without a list url; use an empty dataset.
|
70
|
-
@
|
95
|
+
@messages.notices.push "List url not configured; using empty dataset instead."
|
96
|
+
|
97
|
+
# Detach the callback to after_load so that the call to the
|
98
|
+
# vault constructor can complete/return, allowing any post-load code
|
99
|
+
# to use the newly instantiated vault object as required.
|
100
|
+
window.setTimeout @options.after_load, 100
|
71
101
|
|
72
102
|
# Create convenience attributes for sub-collections.
|
73
103
|
for sub_collection in @options.sub_collections
|
@@ -93,7 +123,7 @@ class Vault
|
|
93
123
|
add: (object) ->
|
94
124
|
# Don't bother if the vault is locked.
|
95
125
|
if @locked
|
96
|
-
@errors.push 'Cannot add, vault is locked.'
|
126
|
+
@messages.errors.push 'Cannot add, vault is locked.'
|
97
127
|
return false
|
98
128
|
|
99
129
|
# If the object has no id, generate a temporary one and add it to the object.
|
@@ -128,7 +158,7 @@ class Vault
|
|
128
158
|
update: (attributes, id) ->
|
129
159
|
# Don't bother if the vault is locked.
|
130
160
|
if @locked
|
131
|
-
@errors.push 'Cannot update, vault is locked.'
|
161
|
+
@messages.errors.push 'Cannot update, vault is locked.'
|
132
162
|
return false
|
133
163
|
|
134
164
|
# Get the id of the object from the attributes if it's not explicitly defined.
|
@@ -137,7 +167,7 @@ class Vault
|
|
137
167
|
# Get the object; return if it's undefined.
|
138
168
|
object = @find(id)
|
139
169
|
unless object?
|
140
|
-
@errors.push 'Cannot update, object not found.'
|
170
|
+
@messages.errors.push 'Cannot update, object not found.'
|
141
171
|
return false
|
142
172
|
|
143
173
|
# Flag it as dirty.
|
@@ -162,7 +192,7 @@ class Vault
|
|
162
192
|
delete: (id) ->
|
163
193
|
# Don't bother if the vault is locked.
|
164
194
|
if @locked
|
165
|
-
@errors.push 'Cannot delete, vault is locked.'
|
195
|
+
@messages.errors.push 'Cannot delete, vault is locked.'
|
166
196
|
return false
|
167
197
|
|
168
198
|
for object, index in @objects
|
@@ -192,7 +222,7 @@ class Vault
|
|
192
222
|
destroy: (id) ->
|
193
223
|
# Don't bother if the vault is locked.
|
194
224
|
if @locked
|
195
|
-
@errors.push 'Cannot delete, vault is locked.'
|
225
|
+
@messages.errors.push 'Cannot delete, vault is locked.'
|
196
226
|
return false
|
197
227
|
|
198
228
|
for object, index in @objects
|
@@ -219,13 +249,13 @@ class Vault
|
|
219
249
|
save: (id, after_save = ->) ->
|
220
250
|
# Don't bother if the vault is locked, we're offline or there's nothing to sync.
|
221
251
|
if @locked
|
222
|
-
@errors.push 'Cannot save, vault is locked.'
|
252
|
+
@messages.errors.push 'Cannot save, vault is locked.'
|
223
253
|
return after_save()
|
224
254
|
else if not navigator.onLine
|
225
|
-
@errors.push 'Cannot save, navigator is offline.'
|
255
|
+
@messages.errors.push 'Cannot save, navigator is offline.'
|
226
256
|
return after_save()
|
227
257
|
else if @dirty_object_count is 0
|
228
|
-
@errors.push 'Nothing to save.'
|
258
|
+
@messages.errors.push 'Nothing to save.'
|
229
259
|
return after_save()
|
230
260
|
|
231
261
|
# Lock the vault until the save is complete.
|
@@ -253,7 +283,7 @@ class Vault
|
|
253
283
|
@objects.splice(index, 1)
|
254
284
|
@dirty_object_count--
|
255
285
|
error: =>
|
256
|
-
@errors.push 'Failed to delete.'
|
286
|
+
@messages.errors.push 'Failed to delete.'
|
257
287
|
complete: =>
|
258
288
|
# Store the collection, unlock the vault, and execute the callback method.
|
259
289
|
@store
|
@@ -281,7 +311,7 @@ class Vault
|
|
281
311
|
object.status = "clean"
|
282
312
|
@dirty_object_count--
|
283
313
|
error: =>
|
284
|
-
@errors.push 'Failed to create.'
|
314
|
+
@messages.errors.push 'Failed to create.'
|
285
315
|
complete: =>
|
286
316
|
# Store the collection, unlock the vault, and execute the callback method.
|
287
317
|
@store
|
@@ -299,7 +329,7 @@ class Vault
|
|
299
329
|
object.status = "clean"
|
300
330
|
@dirty_object_count--
|
301
331
|
error: =>
|
302
|
-
@errors.push 'Failed to update.'
|
332
|
+
@messages.errors.push 'Failed to update.'
|
303
333
|
complete: =>
|
304
334
|
# Store the collection, unlock the vault, and execute the callback method.
|
305
335
|
@store
|
@@ -311,13 +341,13 @@ class Vault
|
|
311
341
|
reload: (after_load = ->) ->
|
312
342
|
# Don't bother if the vault is locked or we're offline.
|
313
343
|
if @locked
|
314
|
-
@errors.push 'Cannot reload, vault is locked.'
|
344
|
+
@messages.errors.push 'Cannot reload, vault is locked.'
|
315
345
|
return after_load()
|
316
346
|
else if not navigator.onLine
|
317
|
-
@errors.push 'Cannot reload, navigator is offline.'
|
347
|
+
@messages.errors.push 'Cannot reload, navigator is offline.'
|
318
348
|
return after_load()
|
319
349
|
else if not @urls.list?
|
320
|
-
@errors.push 'Cannot reload, list url is not configured.'
|
350
|
+
@messages.errors.push 'Cannot reload, list url is not configured.'
|
321
351
|
return after_load()
|
322
352
|
|
323
353
|
# Lock the vault until the reload is complete.
|
@@ -343,7 +373,7 @@ class Vault
|
|
343
373
|
# Call the callback function as the reload is complete.
|
344
374
|
after_load()
|
345
375
|
error: =>
|
346
|
-
@errors.push 'Failed to list.'
|
376
|
+
@messages.errors.push 'Failed to list.'
|
347
377
|
|
348
378
|
# Call the callback function as the reload is complete (albeit unsuccessful).
|
349
379
|
after_load()
|
@@ -355,12 +385,12 @@ class Vault
|
|
355
385
|
synchronize: (after_sync = ->) ->
|
356
386
|
# Don't bother if we're offline.
|
357
387
|
unless navigator.onLine
|
358
|
-
@errors.push 'Cannot synchronize, navigator is offline.'
|
388
|
+
@messages.errors.push 'Cannot synchronize, navigator is offline.'
|
359
389
|
return after_sync()
|
360
390
|
|
361
391
|
@save =>
|
362
392
|
# Only reload the collection if there were no save errors.
|
363
|
-
if @errors.length is 0
|
393
|
+
if @messages.errors.length is 0
|
364
394
|
@reload(after_sync)
|
365
395
|
else
|
366
396
|
after_sync()
|
@@ -439,7 +469,7 @@ class Vault
|
|
439
469
|
object[sub_collection].add = (sub_object) =>
|
440
470
|
# Don't bother if the vault is locked.
|
441
471
|
if @locked
|
442
|
-
@errors.push 'Cannot add sub-object, vault is locked.'
|
472
|
+
@messages.errors.push 'Cannot add sub-object, vault is locked.'
|
443
473
|
return false
|
444
474
|
|
445
475
|
# Set a status on the object.
|
@@ -474,7 +504,7 @@ class Vault
|
|
474
504
|
object[sub_collection].delete = (id) =>
|
475
505
|
# Don't bother if the vault is locked.
|
476
506
|
if @locked
|
477
|
-
@errors.push 'Cannot delete sub-object, vault is locked.'
|
507
|
+
@messages.errors.push 'Cannot delete sub-object, vault is locked.'
|
478
508
|
return false
|
479
509
|
|
480
510
|
# Remove the sub-object from its collection.
|
@@ -499,7 +529,7 @@ class Vault
|
|
499
529
|
object[sub_collection].update = (attributes, id) =>
|
500
530
|
# Don't bother if the vault is locked.
|
501
531
|
if @locked
|
502
|
-
@errors.push 'Cannot update sub-object, vault is locked.'
|
532
|
+
@messages.errors.push 'Cannot update sub-object, vault is locked.'
|
503
533
|
return false
|
504
534
|
|
505
535
|
# Get the id of the sub-object from the attributes if it's not explicitly defined.
|
@@ -508,7 +538,7 @@ class Vault
|
|
508
538
|
# Get the sub-object; return if it's undefined.
|
509
539
|
sub_object = object[sub_collection].find(id)
|
510
540
|
unless sub_object?
|
511
|
-
@errors.push 'Cannot update, sub-object not found.'
|
541
|
+
@messages.errors.push 'Cannot update, sub-object not found.'
|
512
542
|
return false
|
513
543
|
|
514
544
|
# If the root object was clean, flag it and increase the count of dirty objects.
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: vault-rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.9
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2011-12-
|
12
|
+
date: 2011-12-02 00:00:00.000000000Z
|
13
13
|
dependencies: []
|
14
14
|
description: Store and manage collections of objects without a connection.
|
15
15
|
email:
|