All files / mobx-decorated-models/lib property-decorators.js

100% Statements 55/55
95.65% Branches 22/23
100% Functions 18/18
100% Lines 47/47

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130                68x               68x       13x     12x   12x 12x 19x                       12x 11x 11x   1x 1x     4x 41x       4x         84x 84x 15x 122x         27x   27x 206x 206x                     84x 84x 84x 15x   84x 84x 9x   84x 27x       84x 168x 168x 76x 76x     84x       84x 37x 37x   47x     9x     15x     38x     9x     13x          
import { observable, intercept } from 'mobx';
import createCollection from './collection';
import { getSchema, setupModel, addLazyInitializer, attachInterceptors } from './setup-model';
 
function onPropertySet(
    change,
    { modelClass, defaultAttributes, inverseOf, parentModel, parentModelProp },
) {
    change.newValue = setupModel({
        attrs: change.newValue,
        modelClass,
        defaultAttributes,
        inverseOf,
        parentModel,
        parentModelProp,
    });
    return change;
}
 
function onHasManySet(change, options) {
    if (change.type !== 'update' || !change.newValue) { return change; }
    const {
        modelClass, defaultAttributes, inverseOf, parentModel, parentModelProp,
    } = options;
 
    const array = observable.array([]);
    for (let i = 0; i < change.newValue.length; i += 1) {
        array.push(
            setupModel({
                attrs: change.newValue[i],
                array: change.newValue,
                modelClass,
                defaultAttributes,
                inverseOf,
                parentModel,
                parentModelProp,
            }),
        );
    }
    if (parentModel[parentModelProp]) {
        parentModel[parentModelProp].replace(array);
        return null;
    }
    change.newValue = observable.array(array);
    return change;
}
 
const Initializers = {
    object: () => observable.object({}),
    array: createCollection,
};
 
const TypeInitializers = {
    hasMany: createCollection,
};
 
function getInitializer(type, options, propName) {
    const fn = TypeInitializers[type] || Initializers[options.type];
    if (!fn) { return undefined; }
    return function() { // eslint-disable-line func-names
        return fn(options, this, propName);
    };
}
 
function installModelInterceptor(interceptingFn, { target, property }) {
    const { properties } = getSchema(target.constructor);
 
    addLazyInitializer(target, (model) => {
        const schemaProps = properties.get(property).options;
        intercept(model, property, (change => interceptingFn(change, {
            inverseOf:         schemaProps.inverseOf,
            modelClass:        schemaProps.model,
            parentModel:       model,
            parentModelProp:   property,
            defaultAttributes: schemaProps.defaults,
        })));
    });
}
 
function addAttribute(type, setAttributeFn, target, property, descriptor, options = {}) {
    getSchema(target.constructor).properties.set(property, { name: property, type, options });
    const initializer = getInitializer(type, options, property);
    if (initializer) {
        descriptor.initializer = initializer;
    }
    const definition = observable(target, property, descriptor);
    if ('belongsTo' === type && !options.model) {
        options.model = property;
    }
    if (options.model) {
        installModelInterceptor(setAttributeFn, {
            target, property, descriptor, model: options.model,
        });
    }
    ['get', 'set'].forEach((k) => {
        const orig = definition[k];
        definition[k] = function(...args) {
            attachInterceptors(this);
            return orig.call(this, ...args);
        };
    });
    return definition;
}
 
function buildAttributeDecorator(type, args, setAttributeFn) {
    if (('object' === typeof args[0]) && 1 === args.length) {
        return (target, property, descriptor) =>
            addAttribute(type, setAttributeFn, target, property, descriptor, args[0]);
    }
    return addAttribute(type, setAttributeFn, ...args, {});
}
 
const hasMany = (...args) => buildAttributeDecorator(
    'hasMany', args, onHasManySet,
);
const belongsTo = (...args) => buildAttributeDecorator(
    'belongsTo', args, onPropertySet,
);
const field = (...args) => buildAttributeDecorator(
    'field', args, onPropertySet,
);
const session = (...args) => buildAttributeDecorator(
    'session', args, onPropertySet,
);
const identifier = (...args) => buildAttributeDecorator(
    'identifier', args, onPropertySet,
);
 
export { field, session, belongsTo, hasMany, identifier, createCollection };