Skip to content

Commit

Permalink
Refactored cache implementation
Browse files Browse the repository at this point in the history
Being able to fetch relational data both ways (yes!)
  • Loading branch information
Paul Engel committed Jun 13, 2011
1 parent 91f3297 commit cc44f9a
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 31 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

== Version 0.1.2 (June xx, 2011)

* Being able to fetch relational data both ways (yes!)
* Refactored cache implementation
* Using '1' and 'n' instead of 'one' and 'many' respectively for association cardinality

== Version 0.1.1 (June 12, 2011)
Expand Down
12 changes: 6 additions & 6 deletions demo/assets/favorites/authors.csv
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
id;name
integer;string
1;Harper Lee
2;JRR Tolkien
3;William Shakespeare
4;Dan Brown
id;name;books
integer;string;books:n:author
1;Harper Lee;
2;JRR Tolkien;
3;William Shakespeare;
4;Dan Brown;
66 changes: 41 additions & 25 deletions src/csonv.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ if (typeof(Csonv) == "undefined") {
// *

Csonv = (function() {
var parsers = null, cache = {}, maps = {};
var parsers = null, cache = {};

var defineParsers = function() {
var n = function(type, values) {
Expand Down Expand Up @@ -49,18 +49,28 @@ Csonv = (function() {
"booleans": function(value) {
return n("boolean", value);
},
"relational": function(values, type, url) {
"relational": function(values, type, url, id) {
var ids = values.split(Csonv.separators.array);
var assoc = type.split(":");
var assoc_url = url.replace(/\w+\.csv$/, assoc[0] + ".csv")
var array = [];

assoc_url.toObjects();
assoc_url.toObjects(assoc[2]);
var map = cache[assoc_url].map;

for (var i = 0; i < ids.length; i++) {
var object = maps[assoc_url][parseInt(ids[i], 10)];
if (object) {
array.push(object);
if (assoc[2]) {
for (var key in map) {
var object_map = map[key];
if (object_map[assoc[2]].indexOf(id.toString()) != -1) {
array.push(object_map._object);
}
}
} else {
for (var i = 0; i < ids.length; i++) {
var object_map = map[parseInt(ids[i], 10)];
if (object_map) {
array.push(object_map._object);
}
}
}

Expand All @@ -77,32 +87,38 @@ Csonv = (function() {
return request.responseText;
};

var toObjects = function(data, url) {
var toObjects = function(data, url, exclude) {
var rows = data.split("\n");
var keys = rows.shift().csvSplit();
var types = rows.shift().csvSplit();

var methods = [];
for (var i = 0; i < types.length; i++) {
methods.push(parsers[types[i]] || parsers["relational"]);
}

var data = [];
var map = {};
var data = [], map = {};

for (var i = 0; i < rows.length; i++) {
var row = rows[i].csvSplit(), object = {};
var row = rows[i].csvSplit(), object = {}, object_map = {};
for (var j = 0; j < keys.length; j++) {
object[keys[j]] = methods[j](row[j], types[j], url);
var key = keys[j], parser = parsers[type = types[j]];

if (parser) {
object[key] = parser(row[j], type, url, object.id);
} else {
object_map[key] = type.split(":").length == 2 ? row[j].split(Csonv.separators.array) : null;
if (exclude != key) {
object[key] = parsers["relational"](row[j], type, url, object.id);
}
}

}
if (object.id) {
map[object.id] = object;
object_map._object = object;
map[object.id] = object_map;
}
data.push(object);
}

cache[url] = data;
maps[url] = map;
cache[url] = {
data: data,
map : map
};

return data;
};
Expand All @@ -114,14 +130,14 @@ Csonv = (function() {
array : ","
},
init : defineParsers,
fetch: function(url) {
return cache[url] || toObjects(ajax(url), url);
fetch: function(url, exclude) {
return cache[url] ? cache[url].data : toObjects(ajax(url), url, exclude);
}
};
}());

String.prototype.toObjects = function() {
return Csonv.fetch(this);
String.prototype.toObjects = function(exclude) {
return Csonv.fetch(this, exclude);
};

String.prototype.csvSplit = function(s) {
Expand Down

0 comments on commit cc44f9a

Please sign in to comment.