All files / src/Config snconfigfieldmodelstore.ts

84.21% Statements 16/19
54.55% Branches 6/11
100% Functions 4/4
84.21% Lines 16/19
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        1x           1x         1x               15x 1x   14x 1x   13x                 2x 2x 1x   1x               14x               1x 1x           1x      
/**
 * @module Config
 *//** */
 
import { SnConfigBehavior } from './snconfigbehavior';
import { SnConfigFieldModel } from './snconfigfieldmodel';
 
/**
 * Class that stores the model data for the SnConfigModel's fields, it's values are, filled by the SnConfigField decorator.
 */
export class SnConfigFieldModelStore {
 
    /**
     * An array that contains the field definitions.
     */
    private static _store: Map<string, SnConfigFieldModel> = new Map();
 
    /**
     * Adds a new model to the store
     * @param newModel {SnConfigFieldModel} The field model to be added
     * @throws error if a field with the same name already exists
     */
    public static Add(newModel: SnConfigFieldModel) {
        if (!newModel.StoreKey) {
            throw Error('No Store key defined');
        }
        if (this.Contains(newModel.StoreKey)) {
            throw new Error(`Field ${newModel.StoreKey} for configuration model already in the store!`);
        }
        this._store.set(newModel.StoreKey, newModel);
    }
 
    /**
     * Returns an entry for the specified field
     * @param fieldName {string} The field's name to search for
     * @throws error {error} if the store doesn't contain entry for the field.
     */
    public static Get(storeKey: string): SnConfigFieldModel {
        const found = this._store.get(storeKey);
        if (!found) {
            throw new Error(`No entry found with the field name '${storeKey}'`);
        }
        return found;
    }
 
    /**
     * Checks if the store contains value with the specified field
     * @param fieldName fieldName {string} The field's name to search for
     */
    public static Contains(fieldName: string): boolean {
        return this._store.has(fieldName);
    }
 
    /**
     * Gets the fields which are available for command line option input
     * @returns {SnCofigFieldModel[]} The listof the fields
     */
    public static GetCommandOptions(): SnConfigFieldModel[] {
        const items: SnConfigFieldModel[] = [];
        for (const field in this._store) {
            const found = this._store.get(field);
            if (field && found && (found.Behavior & SnConfigBehavior.AllowFromCommandLine) === SnConfigBehavior.AllowFromCommandLine) {
                items.push(found);
            }
        }
        return items;
    }
}