All files / sn-client-js/src/Config snconfigfieldmodelstore.ts

89.47% Statements 17/19
80% Branches 8/10
100% Functions 4/4
89.47% Lines 17/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               6x     6x 1x   5x                 1x 1x 1x                   6x 6x               1x 1x 5x 2x     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: SnConfigFieldModel[] = [];
 
    /**
     * 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) {
        Iif (!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[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[storeKey];
        Eif (!found) {
            throw new Error(`No entry found with the field name '${storeKey}'`);
        }
        return this.store[storeKey];
    }
 
    /**
     * 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 {
        const found = this.store[fieldName];
        return found !== undefined;
    }
 
    /**
     * 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) {
            if (field && (this.store[field].Behavior & SnConfigBehavior.AllowFromCommandLine) === SnConfigBehavior.AllowFromCommandLine) {
                items.push(this.store[field]);
            }
        }
        return items;
    }
}