base-domain
Version:
simple module to help build Domain-Driven Design
146 lines (109 loc) • 3.47 kB
text/coffeescript
{ camelize } = require './util'
###*
type of model's property
TypeInfo
base-domain
###
class TypeInfo
constructor: (, options = {}) ->
@[k] = v for k, v of options
###*
check if the type is the given typeName
equals
{String} typeName
{Boolean}
###
equals: (typeName) -> is typeName
###*
check if the type is not the given typeName
notEquals
{String} typeName
{Boolean}
###
notEquals: (typeName) -> isnt typeName
###*
get TypeInfo as MODEL
createModelType
{String} modelName
{String} [idPropName] by default: xxxYyyId when modelName is xxx-yyy
{TypeInfo} type
###
: (modelName, idPropName) ->
new TypeInfo 'MODEL',
model : modelName
idPropName : idPropName ? camelize(modelName, true) + 'Id'
###*
get TypeInfo as MODEL_LIST
createModelListType
{String} modelName
{String} [options.name] name of list model, by default: xxx-yyy-list when modelName is xxx-yyy
{TypeInfo} type
###
: (modelName, options = {}) ->
if typeof options is 'string'
options = name: options
new TypeInfo 'MODEL_LIST',
model : modelName
listName : options.name ? "#{modelName}-list"
###*
get TypeInfo as MODEL_DICT
createModelDictType
{String} modelName
{String} [options.name] name of dict model, by default: xxx-yyy-dict when modelName is xxx-yyy
{TypeInfo} type
###
: (modelName, options = {}) ->
if typeof options is 'string'
options = name: options
new TypeInfo 'MODEL_DICT',
model : modelName
dictName : options.name ? "#{modelName}-dict"
###*
get TypeInfo as temporary value
createTemporaryType
{String} typeName
{TypeInfo} type
###
: (typeName = 'ANY', options = {}) ->
options.tmp = true
new TypeInfo typeName, options
###
these hacky codes makes
.TMP
an object and also a function
###
TypeInfo.createTemporaryType[k] = v for k, v of TypeInfo.createTemporaryType()
###*
TYPES defines various data type, including model and array of models
key: typeName (String)
value: type TypeInfo|Function(TypeInfo)
TYPES
###
:
ANY : new TypeInfo 'ANY'
STRING : new TypeInfo 'STRING'
NUMBER : new TypeInfo 'NUMBER'
BOOLEAN : new TypeInfo 'BOOLEAN'
OBJECT : new TypeInfo 'OBJECT'
ARRAY : new TypeInfo 'ARRAY'
DATE : new TypeInfo 'DATE'
BUFFER : new TypeInfo 'BUFFER'
GEOPOINT : new TypeInfo 'GEOPOINT'
CREATED_AT : new TypeInfo 'CREATED_AT'
UPDATED_AT : new TypeInfo 'UPDATED_AT'
MODEL : TypeInfo.createModelType
MODEL_LIST : TypeInfo.createModelListType
MODEL_DICT : TypeInfo.createModelDictType
TMP : TypeInfo.createTemporaryType
module.exports = TypeInfo