abstract generic logic from blueprinters to blueprinters.js

The logic in the files in the 'blueprinters' folder is now _only_ the data transformation logic. Instead of taking in arguments like the sheetId, the tabName, and the sheetName, the function now takes a single argument: the list of lists that represents the raw data from the sheet.

This setup gives datasheet-server greater value, as it allows developers to only specify the transformation logic, and not worry about the other stuff that datasheet server is doing.
This commit is contained in:
Lachlan Kermode
2018-12-13 15:56:54 +00:00
parent 7636db4f41
commit 5431b2be3f
9 changed files with 84 additions and 161 deletions

View File

@@ -30,6 +30,8 @@ class Controller {
} else {
throw new Error(copy.errors.update)
}
}).catch(err => {
console.log(err)
})
}

View File

@@ -91,9 +91,9 @@ class Fetcher {
*/
_saveViaBlueprinter (tab, data, blueprinter) {
const saturatedBp = blueprinter(
tab,
this.sheetName,
this.sheetId,
this.sheetName,
tab,
data
)

View File

@@ -24,13 +24,28 @@ export function buildDesaturated (sheetId, sheetName, tab, resource) {
return bp
}
const buildBlueprinter = R.curry((datafierName, datafier, sheetId, sheetName, tabName, data) => {
console.log(`blueprinter ${datafierName} called: ${tabName}`)
const bp = R.clone(defaultBlueprint)
bp.sheet = {
name: sheetName,
id: sheetId
}
bp.name = tabName
bp.resources[datafierName] = R.clone(defaultResource)
bp.resources[datafierName].data = datafier(data)
return bp
})
// import all default exports from 'blueprinters' folder
const allBps = {}
const REL_PATH_TO_BPS = '../blueprinters'
const normalizedPath = path.join(__dirname, REL_PATH_TO_BPS)
fs.readdirSync(normalizedPath).forEach(file => {
const bpName = file.replace('.js', '')
allBps[bpName] = require(`${REL_PATH_TO_BPS}/${file}`).default
const datafier = require(`${REL_PATH_TO_BPS}/${file}`).default
allBps[bpName] = buildBlueprinter(bpName, datafier)
})
// NB: revert to ES5 'module.exports' required to make blueprinters from