slender_data 0.1.0 → 0.2.0

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: 1deea5d8aa969cdc2e213cb39e50b526a00615c0
4
- data.tar.gz: 5b89a6bd314958eef91ddf872db9d10763f609d9
3
+ metadata.gz: d219af00b0955ed12a4724a45274fbbdcfad22d2
4
+ data.tar.gz: 1e55017fea414a68625b7076f61bba42f46991ac
5
5
  SHA512:
6
- metadata.gz: b3df0aa35a568a60aa088320e254183d632357f5ecee53b0f47a2ba900113a3fb546e6220f3b50890388f8e67e87c71c5e71db3a2f55563dd3c1c7e4ab14ccf1
7
- data.tar.gz: 916850fc041b71ae70fc6c9d97308a7713f78d4ac41f6d4a7ac324f120db9f604f5c5fe6bb20f1c25da2b023822759b42bb745eacd6e5f4e2f665608324fe38c
6
+ metadata.gz: 003380a4bc570c13031e817ca35673904b6270dc1d1d4445e8e066258055cc43c0e3835866c557cd792232821f3cb64276c5f041bde7079c78774307a99ee103
7
+ data.tar.gz: 91d95a4d10aae259c87f1f7461808dc9c03e5fc99816f6be1a01d2a40b134f209df11715b63a9f04e5a77a2aada10553277657a9e3775c1369a8bcf9806255ef
@@ -8,6 +8,13 @@
8
8
  # In addition to the standard RESTful routes, the data API is expected to support these methods for performance reasons:
9
9
  # * DELETE /entries: Delete a whole batch of entries at once. The ids of the entries to delete are given as a JSON hash in the body of the request.
10
10
  # *
11
+ #
12
+ # Terminology:
13
+ # * "load" methods are high level public API. They try to satisfy the requests from the local cache, and load the data from the server on demand.
14
+ # * "get" methods access only the local cache, and return their results synchronously.
15
+ # * "fetch" methods encapsulate accessing remote resources.
16
+ # * "create" creates a given entry on the server
17
+ # * "add" adds the given entry to the clientside cache.
11
18
  class slender_data.PersistenceManager
12
19
 
13
20
  constructor: (params) ->
@@ -31,6 +38,15 @@ class slender_data.PersistenceManager
31
38
  @loader = new slender_data.AjaxLoader { cache: no }
32
39
 
33
40
 
41
+ # Adds the given entry to the clientside caches.
42
+ # Returns the entry to be used by the client.
43
+ add: (entry) ->
44
+ @server_data.add entry
45
+ client_entry = slender_data.clone_hash entry
46
+ @client_data.add client_entry
47
+ client_entry
48
+
49
+
34
50
  # Adds the given data objects to the server cache.
35
51
  add_all: (data) ->
36
52
  @server_data.add_all data
@@ -69,6 +85,9 @@ class slender_data.PersistenceManager
69
85
  error_callback(xhr.responseText) if error_callback
70
86
 
71
87
 
88
+ # TODO: create_many
89
+
90
+
72
91
  # Deletes the given object from the server.
73
92
  delete: (obj, success_callback, error_callback) ->
74
93
  @client_data.remove obj
@@ -106,8 +125,31 @@ class slender_data.PersistenceManager
106
125
  "#{@base_url}.json"
107
126
 
108
127
 
109
- # Returns the cached data object, or undefined.
110
- get_cached: (key) ->
128
+ # Fetches the entry with the given key from the server.
129
+ # Adds it to the clientside cache.
130
+ fetch: (key, success_callback) ->
131
+ @loader.get "#{@base_url}/#{key}.json", (server_entry) =>
132
+ client_entry = @add server_entry
133
+ success_callback client_entry
134
+
135
+
136
+ # Loads all objects from the server.
137
+ # Provides the given params as parameters to the GET request.
138
+ fetch_all: (params, success_callback, error_callback) ->
139
+ jQuery.ajax
140
+ url: @collection_url()
141
+ cache: no
142
+ data: params
143
+ success: (data) =>
144
+ @server_data.add_all data
145
+ success_callback(data) if success_callback
146
+ error: (xhr) ->
147
+ error_callback(xhr.responseText) if error_callback
148
+
149
+
150
+ # Returns the cached object with the given key, if one exists in the cache.
151
+ # Returns ndefined if no object with this key exists in the cache.
152
+ get_cached_entry: (key) ->
111
153
 
112
154
  # Try to use client_data cache.
113
155
  client_obj = @client_data.get key
@@ -121,51 +163,39 @@ class slender_data.PersistenceManager
121
163
  return client_obj
122
164
 
123
165
  # Object not found in client or server cache.
124
- return undefined
166
+ undefined
167
+
168
+
169
+ # Returns all entries that are in the cache,
170
+ # and the ids of all entries that are not cached.
171
+ get_cached_entries: (ids) ->
172
+ entries = []
173
+ missing_ids = []
174
+ for id in ids
175
+ if (entry = @get_cached_entry(id))
176
+ entries.push entry
177
+ else
178
+ missing_ids.push id
179
+ [entries, missing_ids]
125
180
 
126
181
 
127
182
  # Returns the entry with the given key.
128
183
  load: (key, success_callback) ->
129
184
 
130
185
  # Try to load from cache.
131
- return success_callback(entry) if entry = @get_cached key
186
+ return success_callback(entry) if entry = @get_cached_entry key
132
187
 
133
- # No data on client at all --> load data from server.
134
- @loader.get "#{@base_url}/#{key}", (server_entry) =>
135
- @server_data.add server_entry
136
- client_entry = slender_data.clone_hash server_entry
137
- @client_data.add client_entry
138
- success_callback client_entry
139
-
140
-
141
- # Loads all objects from the server.
142
- # Provides the given params as parameters to the GET request.
143
- load_all: (params, success_callback, error_callback) ->
144
- jQuery.ajax
145
- url: @collection_url()
146
- cache: no
147
- data: params
148
- success: (data) =>
149
- @server_data.add_all data
150
- success_callback() if success_callback
151
- error: (xhr) ->
152
- error_callback(xhr.responseText) if error_callback
188
+ # No data on client --> load from server.
189
+ @fetch key, success_callback
153
190
 
154
191
 
155
192
  # Loads all entries with the given ids.
156
193
  load_many: (ids, success_callback) ->
157
- missing_ids = []
158
- entries = []
159
- $.each ids, (pos, id) =>
160
- entry = @get_cached id
161
- if entry
162
- entries.push entry
163
- else
164
- missing_ids.push id
165
-
194
+ [cached_entries, missing_ids] = @get_cached_entries(ids)
166
195
  if missing_ids.length == 0
167
- return success_callback(entries)
196
+ return success_callback(cached_entries)
168
197
 
198
+ # TODO: implement loading the missing entries from the server.
169
199
  alert "uncached entries found: #{missing_ids}"
170
200
 
171
201
 
@@ -179,6 +209,8 @@ class slender_data.PersistenceManager
179
209
  @create obj, success_callback, error_callback
180
210
 
181
211
 
212
+ # TODO: save_many
213
+
182
214
  # Updates the given object.
183
215
  #
184
216
  # The given object must exist on the server already,
@@ -1,3 +1,3 @@
1
1
  module SlenderData
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
metadata CHANGED
@@ -1,27 +1,27 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: slender_data
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kevin Goslar
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-08-15 00:00:00.000000000 Z
11
+ date: 2013-12-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - '>='
17
+ - - '>'
18
18
  - !ruby/object:Gem::Version
19
19
  version: 3.1.0
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - '>='
24
+ - - '>'
25
25
  - !ruby/object:Gem::Version
26
26
  version: 3.1.0
27
27
  - !ruby/object:Gem::Dependency
@@ -147,7 +147,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
147
147
  version: '0'
148
148
  requirements: []
149
149
  rubyforge_project:
150
- rubygems_version: 2.0.3
150
+ rubygems_version: 2.0.14
151
151
  signing_key:
152
152
  specification_version: 4
153
153
  summary: A JS micro-framework for lightweight AJAX data management against RESTful