Clean master commit

This commit is contained in:
Lachlan Kermode
2018-10-31 19:35:15 +00:00
commit 2cbfbc33ef
24 changed files with 5400 additions and 0 deletions

23
src/models/Interface.js Normal file
View File

@@ -0,0 +1,23 @@
/**
* Model is a class whose sole responsibility is to save and load blueprints.
* It allows for different storage mechanisms for different kinds of blueprints.
*/
class Model {
/**
* save - save a Blueprint, using the information it contains.
*
* @param {type} blueprint the Blueprint to be saved.
* @return {type} Promise which returns True.
*/
save(blueprint) {}
/**
* load - load a resource from a data model, using a Blueprint object as
* well as a REST-like URL of the format /:source/:tab/:resource.
*
* @param {type} url String that represents the path to resource.
* @param {type} blueprint Blueprint object (desaturated?).
* @return {type} Object containing the resource data.
*/
load(url, blueprint) {}
}

65
src/models/StoreJson.js Normal file
View File

@@ -0,0 +1,65 @@
import fs from "mz/fs";
import hash from "object-hash";
import {fmtSourceTitle} from "../lib/util";
import path from "path";
const STORAGE_DIRNAME = "temp";
class StoreJson {
save(bp) {
return Promise.all(
Object.keys(bp.routes).map(route =>
fs.writeFile(
`${STORAGE_DIRNAME}/${fmtSourceTitle(
bp.source.name
)}__${fmtSourceTitle(bp.name)}__${route}.json`,
JSON.stringify(bp.routes[route].data)
)
)
);
}
load(url, bp) {
const parts = url.split("/");
const fname = `${STORAGE_DIRNAME}/${parts[0]}__${parts[1]}__${
parts[2]
}.json`;
return fs
.exists(fname)
.then(isAvailable => {
if (isAvailable) return fs.readFile(fname, "utf8");
else {
throw new Error("No resource exists");
}
})
.then(data => JSON.parse(data))
.then(data => {
if (parts.length === 3) {
// No lookup if the requested url doesn't have a fragment
return data;
} else if (parts[2] === "ids") {
// Do a lookup if fragment is included to filter a relevant item
// When the resource requested is 'ids'
const id = parseInt(parts[3]);
if (id !== NaN && id >= 0 && id < data.length) {
return data[id];
} else {
throw new Error(`Fragment index does not exist`);
}
} else {
// Do a lookup if fragment is included to filter a relevant item
const index = parseInt(parts[3]);
if (index !== NaN && index >= 0 && index < data.length) {
console.log(data, index);
return data.filter((vl, idx) => idx === index)[0];
} else {
throw new Error(`Fragment index does not exist`);
}
}
});
}
// TODO: add method to build blueprint from data source
}
export default StoreJson;