Skip to content
This repository has been archived by the owner on Nov 29, 2017. It is now read-only.

Commit

Permalink
Squashed 'archived_PhantRepos/phant-meta-test/' content from commit f…
Browse files Browse the repository at this point in the history
…148b14

git-subtree-dir: archived_PhantRepos/phant-meta-test
git-subtree-split: f148b147d8d75c2f43a7d66a2bbde368bb8028e8
  • Loading branch information
bboyho committed Nov 15, 2017
0 parents commit d1ba519
Show file tree
Hide file tree
Showing 8 changed files with 506 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/node_modules/
*.swp
npm-debug.log
14 changes: 14 additions & 0 deletions .jshintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"curly": true,
"eqeqeq": true,
"immed": true,
"latedef": "func",
"newcap": false,
"noarg": true,
"sub": true,
"undef": true,
"unused": "vars",
"boss": true,
"eqnull": true,
"node": true
}
11 changes: 11 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
language: node_js
node_js:
- '0.10'
before_script:
- npm install -g grunt-cli
notifications:
email:
recipients:
- [email protected]
on_success: change
on_failure: change
48 changes: 48 additions & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
'use strict';

module.exports = function (grunt) {
// Show elapsed time at the end
require('time-grunt')(grunt);
// Load all grunt tasks
require('load-grunt-tasks')(grunt);

// Project configuration.
grunt.initConfig({
nodeunit: {
files: ['test/**/*_test.js']
},
jshint: {
options: {
jshintrc: '.jshintrc',
reporter: require('jshint-stylish')
},
gruntfile: {
src: 'Gruntfile.js'
},
lib: {
src: ['lib/**/*.js']
},
test: {
src: ['test/**/*.js']
}
},
watch: {
gruntfile: {
files: '<%= jshint.gruntfile.src %>',
tasks: ['jshint:gruntfile']
},
lib: {
files: '<%= jshint.lib.src %>',
tasks: ['jshint:lib', 'nodeunit']
},
test: {
files: '<%= jshint.test.src %>',
tasks: ['jshint:test', 'nodeunit']
}
}
});

// Default task.
grunt.registerTask('default', ['jshint', 'nodeunit']);

};
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# phant-meta-test [![Build Status](https://secure.travis-ci.org/sparkfun/phant-meta-test.png?branch=master)](http://travis-ci.org/sparkfun/phant-meta-test)

memory based phant metadata storage for phant development

## Contributing
In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using [Grunt](http://gruntjs.com/).

## License
Copyright (c) 2014 Todd Treece. Licensed under the GPL v3 license.
258 changes: 258 additions & 0 deletions lib/phant-meta-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,258 @@
/**
* phant-meta-test
* https://github.com/sparkfun/phant-meta-test
*
* Copyright (c) 2014 SparkFun Electronics
* Licensed under the GPL v3 license.
*/

'use strict';

/**** Module dependencies ****/
var _ = require('lodash'),
sift = require('sift'),
util = require('util'),
events = require('events');

/**** Make PhantMeta an event emitter ****/
util.inherits(PhantMeta, events.EventEmitter);

/**** PhantMeta prototype ****/
var app = PhantMeta.prototype;

/**** Expose PhantMeta ****/
exports = module.exports = PhantMeta;

/**** Initialize a new PhantMeta ****/
function PhantMeta(config) {

if (!(this instanceof PhantMeta)) {
return new PhantMeta(config);
}

events.EventEmitter.call(this);

util._extend(this, config || {});

}

function newId() {
return Math.floor(Math.random() * 16777215).toString(16);
}

app.moduleName = 'Metadata Test';

app.streams = [
{
id: '111aaa',
title: 'Test Weather',
description: 'Test 1 description',
fields: ['wind', 'temp'],
tags: ['weather', 'test'],
date: Date.now(),
last_push: Date.now() - 200000,
hidden: false,
flagged: false
},
{
id: '222bbb',
title: 'Test Weight',
description: 'Test 2 description',
fields: ['lbs'],
tags: ['weight', 'test'],
date: Date.now() - 200000,
last_push: Date.now(),
hidden: false,
flagged: false
}
];

/**
* list
*
* get a array of streams that match a query. calls the
* supplied callback with err and an array of stream objects
* as arguments. if err is truthy, assume the creation failed.
*/
app.list = function(callback, query, offset, limit, sort) {

var results = this.streams;

// defaults
offset = offset || 0;
limit = limit || 20;
sort = sort || { property: 'date', direction: 'desc' };

// filter by query if supplied
if(query) {
results = sift(query, results);
}

// sort by any property
results = _.sortBy(results, sort.property).slice(offset, limit + offset);

// reverse if desc
if(sort.direction.toLowerCase() === 'desc') {
results = results.reverse();
}

callback(null, results);

};

/** each
*
* get all streams one at a time that match a query. calls the
* supplied callback with err and an array of stream objects
* as arguments. if err is truthy, assume the creation failed.
*/
app.each = function(callback, query, offset, limit, sort) {

var results = this.streams;

// defaults
offset = offset || 0;
limit = limit || null;
sort = sort || { property: 'date', direction: 'desc' };

// filter by query if supplied
if(query) {
results = sift(query, results);
}

// sort by any property
results = _.sortBy(results, sort.property);

// limit the results if necessary
if(limit) {
results = results.slice(offset, limit + offset);
} else {
results = results.slice(offset);
}

// reverse if desc
if(sort.direction.toLowerCase() === 'desc') {
results = results.reverse();
}

// return one by one
results.forEach(function(result) {
callback(null, result);
});

};

/**
* get
*
* retrieves a stream by id. calls the supplied callback with
* err and the stream object as arguments.
* if err is truthy, assume the creation failed.
*/
app.get = function(id, callback) {

var stream = _.find(this.streams, { id: id });

if(! stream) {
return callback('stream not found');
}

callback(null, stream);

};

/**
* create
*
* creates a new stream with the supplied
* data. calls the supplied callback with
* err and the new stream obj as arguments.
* if err is truthy, assume the creation failed.
*/
app.create = function(data, callback) {

var diff = _.difference(['title', 'description', 'fields'], _.keys(data));

if(diff.length !== 0) {
return callback('saving stream failed');
}

data.id = newId();
data.date = Date.now();
data.last_push = 0;
data.hidden = data.hidden || false;
data.tags = data.tags || [];
data.flagged = false;

this.streams.push(data);

callback(null, data);

};

/**
* update
*
* updates existing stream by id with the supplied
* data. calls the supplied callback with
* err and the updated stream object as arguments.
* if err is truthy, assume the update failed.
*/
app.update = function(id, data, callback) {

var record = _.find(this.streams, { id: id });

if(! record) {
return callback('update failed: stream not found');
}

// remove id and date from data
data = _.omit(data, ['id', 'date']);

// remove old record
this.streams = _.reject(this.streams, {id: id});

// add updated data
record = _.assign(record, data);

// push updated record
this.streams.push(record);

callback(null, record);

};

/**
* touch
*
* updates the streams last_push
* to Date.now(). calls callback
* with err as the only argument. if err
* is truthy, assume the update failed.
*/
app.touch = function(id, callback) {

this.update(id, {last_push: Date.now()}, callback);

};

/**
* delete
*
* removes stream by id. calls callback
* with err as the only argument. if err
* is truthy, assume the delete failed.
*/
app.delete = function(id, callback) {

var record = _.find(this.streams, { id: id });

if(! record) {
return callback('delete failed: stream not found');
}

this.streams = _.reject(this.streams, {id: id});

callback(null);

};
41 changes: 41 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"name": "phant-meta-test",
"version": "2.1.3",
"main": "lib/phant-meta-test.js",
"description": "test metadata for phant development",
"homepage": "https://github.com/sparkfun/phant-meta-test",
"bugs": "https://github.com/sparkfun/phant-meta-test/issues",
"author": {
"name": "Todd Treece",
"email": "[email protected]",
"url": "http://uniontownlabs.org"
},
"repository": {
"type": "git",
"url": "git://github.com/sparkfun/phant-meta-test"
},
"dependencies": {
"lodash": "^2.4.1",
"sift": "0.0.18"
},
"license": "GPL-3.0",
"engines": {
"node": "^0.10.30"
},
"devDependencies": {
"grunt": "^0.4.4",
"grunt-contrib-jshint": "~0.7.0",
"grunt-contrib-nodeunit": "~0.2.0",
"grunt-contrib-watch": "~0.5.0",
"load-grunt-tasks": "~0.2.0",
"time-grunt": "~0.2.0",
"jshint-stylish": "~0.1.3"
},
"scripts": {
"test": "grunt"
},
"phantConfig": {
"name": "Memory",
"options": []
}
}
Loading

0 comments on commit d1ba519

Please sign in to comment.