Skip to content

Commit

Permalink
first pass at a restructuring
Browse files Browse the repository at this point in the history
  • Loading branch information
brianleroux committed Jan 18, 2011
1 parent 58c5e75 commit e2e2093
Show file tree
Hide file tree
Showing 26 changed files with 324 additions and 330 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.DS_Store
AIRAliases.js
AIRIntrospector.js
/lib
20 changes: 20 additions & 0 deletions makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#VERSION = "0.9.0"
#BASE = "./lib/lawnchair-dom-" + $VERSION + ".js"

default: clean build min test

clean:
rm -rf ./lib

build:
mkdir -p ./lib && touch ./lib/lawnchair.js
cat ./src/lawnchair.js > ./lib/lawnchair.js
cat ./src/adaptors/dom.js >> ./lib/lawnchair.js
cat ./src/plugins/iteration.js >> ./lib/lawnchair.js
min:
echo 'TODO: min'

test:
open ./spec/public/adaptors/dom.html

.PHONY: all
11 changes: 2 additions & 9 deletions spec/public/adaptors/dom.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,8 @@
<link rel="stylesheet" href="../lib/qunit.css" type="text/css" media="screen" title="no title" charset="utf-8">
<script src="../lib/qunit.js" type="text/javascript" charset="utf-8"></script>
<script src="../lib/json2.js" type="text/javascript" charset="utf-8"></script>

<script src="../../../src/adaptors/LawnchairAdaptorHelpers.js" type="text/javascript" charset="utf-8"></script>
<script src="../../../src/adaptors/DOMStorageAdaptor.js" type="text/javascript" charset="utf-8"></script>
<script src="../../../src/Lawnchair.js" type="text/javascript" charset="utf-8"></script>

<script type="text/javascript" charset="utf-8">
store = new Lawnchair({adaptor:'dom'});
</script>
<script src="../lawnchair-spec.js" type="text/javascript" charset="utf-8"></script>
<script src="../../../lib/lawnchair.js" type="text/javascript" charset="utf-8"></script>
<script src="../lawnchair-spec.js" type="text/javascript" charset="utf-8"></script>
</head>
<body>
<h1 id="qunit-header">Lawnchair, DOM Adapter Specs</h1>
Expand Down
4 changes: 3 additions & 1 deletion spec/public/lawnchair-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ test('ctor', function() {
ok(true, 'exception raised if no callback supplied to init');
}
});

/*
test( 'nuke()', function() {
QUnit.stop();
expect(4);
Expand Down Expand Up @@ -169,6 +170,7 @@ test( 'remove()', function() {
test( 'Lawnchair helpers', function() {
equals(store.adaptor.uuid().length, 36, "uuid() function should create a 36 character string (is this a test, really?)");
});
*/
/*
should( 'get 10 items in a page.', function() {
Expand Down
4 changes: 2 additions & 2 deletions spec/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
end

get '/src/*' do
f = File.join(File.dirname(__FILE__), '..', 'src', params['splat'].first)
f = File.join(File.dirname(__FILE__), '..', 'lib', params['splat'].first)
IO.readlines(f,'').to_s
end
end
161 changes: 90 additions & 71 deletions src/Lawnchair.js
Original file line number Diff line number Diff line change
@@ -1,84 +1,103 @@
/**
* Lawnchair
* =========
* A lightweight JSON document store.
* TODO batch inseration
*
*/
var Lawnchair = function(opts) {
this.init(opts);
var Lawnchair = function (options, callback) {
// lawnchair requires json
if (!JSON) throw "JSON unavailable! Include http://www.json.org/json2.js to fix."
// startup plugins
this._initPlugins()
// mixin first valid adaptor
this._initAdaptor()
// init the adaptor
this.init(options, callback)
}

Lawnchair.adaptors = []

/**
* queue an adaptor for mixin
* ===
* - checks for standard methods: adaptor init, save, get, exists, all, remove, nuke
*
*/
Lawnchair.adaptor = function (id, obj) {
// add the adaptor id to the adaptor obj
// ugly here for a cleaner dsl for implementing adaptors
obj['adaptor'] = id
// methods required to implement a lawnchair adaptor
var implements = 'adaptor valid init save get exists all remove nuke'.split(' ')
// mix in the adaptor
for (var i in obj) {
if (implements.indexOf(i) === -1) throw 'Invalid adaptor! Method missing: ' + i
}
// if we made it this far the adaptor interface is valid
Lawnchair.adaptors.push(obj)
}

Lawnchair.plugins = []

/**
* generic extension for plugins
* ===
* - if an init method is found it registers it to be called when the lawnchair is inited
*
*/
Lawnchair.plugin = function(obj) {
for (var i in obj) {
if (i === 'init') {
Lawnchair.plugins.push(obj[i])
} else {
this.prototype[i] = obj[i]
}
}
}

/**
* helpers
*
*/
Lawnchair.prototype = {

init:function(opts) {
var adaptors = {
'webkit':window.WebkitSQLiteAdaptor,
'gears':window.GearsSQLiteAdaptor,
'dom':window.DOMStorageAdaptor,
'cookie':window.CookieAdaptor,
'air':window.AIRSQLiteAdaptor,
'userdata':window.UserDataAdaptor,
'air-async':window.AIRSQLiteAsyncAdaptor,
'blackberry':window.BlackBerryPersistentStorageAdaptor,
'couch':window.CouchAdaptor
};
this.adaptor = opts.adaptor ? new adaptors[opts.adaptor](opts) : new DOMStorageAdaptor(opts);

// Check for native JSON functions.
if (!JSON || !JSON.stringify) throw "Native JSON functions unavailable - please include http://www.json.org/json2.js or run on a decent browser :P";
},

// Save an object to the store. If a key is present then update. Otherwise create a new record.
save:function(obj, callback) {this.adaptor.save(obj, callback)},

// Invokes a callback on an object with the matching key.
get:function(key, callback) {this.adaptor.get(key, callback)},

// Returns whether a key exists to a callback.
exists:function(callback) {this.adaptor.exists(callback)},

// Returns all rows to a callback.
all:function(callback) {this.adaptor.all(callback)},

// Removes a json object from the store.
remove:function(keyOrObj, callback) {this.adaptor.remove(keyOrObj, callback)},

// Removes all documents from a store and returns self.
nuke:function(callback) {this.adaptor.nuke(callback);return this},

// Returns a page of results based on offset provided by user and perPage option
paged:function(page, callback) {this.adaptor.paged(page, callback)},

/**
* Iterator that accepts two paramters (methods or eval strings):
*
* - conditional test for a record
* - callback to invoke on matches
*
*/
find:function(condition, callback) {
var is = (typeof condition == 'string') ? function(r){return eval(condition)} : condition
, cb = this.adaptor.terseToVerboseCallback(callback);

this.each(function(record, index) {
if (is(record)) cb(record, index); // thats hot
});
_initPlugins: function () {
var self = this
Lawnchair.plugins.forEach(function(plugin){
plugin.call(self)
})
},

_initAdaptor: function () {
// iterate all adaptors
for (var i = 0, l = Lawnchair.adaptors.length; i < l; i++) {
// mixin the first adaptor that is valid for this env
var adaptor = Lawnchair.adaptors[i]
if (adaptor.valid()) {
for (var j in adaptor) {
this[j] = adaptor[j]
}
break
}
}
// we have failed
if (!this.adaptor) throw 'No valid adaptor.'
},

// merging default properties with user defined options in lawnchair init
merge: function (defaultOption, userOption) {
return (userOption == undefined || userOption == null) ? defaultOption: userOption;
},

// awesome shorthand callbacks as strings. this is shameless theft from dojo.
terseToVerboseCallback: function (callback) {
return (typeof arguments[0] == 'string') ? function (r, i) { eval(callback) } : callback;
},

/**
* Classic iterator.
* - Passes the record and the index as the second parameter to the callback.
* - Accepts a string for eval or a method to be invoked for each document in the collection.
*/
each:function(callback) {
var cb = this.adaptor.terseToVerboseCallback(callback);
this.all(function(results) {
var l = results.length;
for (var i = 0; i < l; i++) {
cb(results[i], i);
}
});
// returns a unique identifier (by way of Backbone.localStorage.js)
uuid: function () {
var S4 = function () {
return (((1+Math.random())*0x10000)|0).toString(16).substring(1);
}
return (S4()+S4()+"-"+S4()+"-"+S4()+"-"+S4()+"-"+S4()+S4()+S4());
}
// --
};
105 changes: 0 additions & 105 deletions src/adaptors/DOMStorageAdaptor.js

This file was deleted.

Loading

0 comments on commit e2e2093

Please sign in to comment.