Created
September 11, 2018 10:34
-
-
Save taburetkin/2368fcf8190fabe26c8237cb35716102 to your computer and use it in GitHub Desktop.
model-schema
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { betterResult } from 'bbmn/utils'; | |
//import { flat, unflat } from '../../utils/index.js'; | |
// function deepClone(arg = {}){ | |
// return unflat(flat(arg || {})); | |
// } | |
export default function ModelSchema(properties = {}){ | |
this.properties = _.reduce(properties, (memo, property, name) => { | |
memo[name] = this._createProperty(name, property); | |
return memo; | |
}, {}); | |
} | |
_.extend(ModelSchema.prototype, { | |
_createProperty(name, property = {}){ | |
return new PropertySchema({ name, property, modelSchema: this }); | |
}, | |
getProperty(name, { create = false } = {}){ | |
let property = this.properties[name]; | |
if(property || !create) { | |
return property; | |
} | |
property = this.properties[name] = this._createProperty(name); | |
return property; | |
}, | |
getValidation(name) { | |
let property = this.getProperty(name); | |
return property && property.getValidation() || {}; | |
}, | |
getType(name) { | |
let property = this.getProperty(name); | |
return property && property.getType() || {}; | |
}, | |
getLabel(name){ | |
let property = this.getProperty(name); | |
return property && property.getLabel() || ''; | |
} | |
}); | |
export function PropertySchema({ name, property, modelSchema }){ | |
_.extend(this, property); | |
this.name = name; | |
this.modelSchema = modelSchema; | |
} | |
_.extend(PropertySchema.prototype, { | |
getValidation() { | |
return this.validation || {}; | |
}, | |
getType() { | |
return this.value || {}; | |
}, | |
getDisplay(){ | |
return this.display || {}; | |
}, | |
getLabel(value, hash){ | |
let label = this.getDisplay().label; | |
return betterResult({ label },'label', { args: [value, hash] }); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment