# all files / features/ComponentEditor/Models/ ArgumentModel.js

100% Statements 38/38
100% Branches 26/26
100% Functions 14/14
100% Lines 38/38

    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

    1

    1

    1
    1

    1

    70
    26

    27

    3

    2

    2

    2

    7

    26

    70

    1
    7

    7
    7
    7

    7
    1
    6
    1
    5
    1
    4
    1

    3

    1
    7
    1

    1
    7
    4
    4

    1

    70

     'use strict';

    // Utilities:
    var _ = require('lodash');

    // Module:
    var ComponentEditor = require('../ComponentEditor');

    // Dependencies:
    require('../../../Core/Services/ASTCreatorService');
    require('../../../Core/Services/StringToLiteralService');

    var createArgumentModelConstructor = function (
        astCreatorService,
        stringToLiteralService
    ) {
        var ArgumentModel = function ArgumentModel (method, argument) {
            Object.defineProperties(this, {
                method: {
                    get: function () {
                        return method || null;
                    }
                },
                name: {
                    get: function () {
                        return argument ? argument.name : false;
                    }
                },
                description: {
                    get: function () {
                        return argument ? argument.description : false;
                    }
                },
                type: {
                    get: function () {
                        return argument ? argument.type : false;
                    }
                },
                required: {
                    get: function () {
                        return argument ? !!argument.required : false;
                    }
                },
                ast: {
                    get: function () {
                        return toAST.call(this);
                    }
                }
            });

            this.value = '';
        };

        return ArgumentModel;

        function toAST () {
            var ast = astCreatorService;

            var literal = stringToLiteralService.toLiteral(this.value);
            var parameter = findParameter.call(this);
            var result = findResult.call(this);

            if (!_.isUndefined(literal) && literal !== this.value) {
                return ast.literal(literal);
            } else if (parameter) {
                return ast.identifier(parameter.variableName);
            } else if (result) {
                return ast.identifier(this.value);
            } else if (this.value) {
                return ast.literal(this.value);
            } else {
                return ast.literal(null);
            }
        }

        function findParameter () {
            return this.method && _.find(this.method.interaction.action.parameters, function (parameter) {
                return parameter.name === this.value;
            }, this);
        }

        function findResult () {
            return this.method && _.find(this.method.interaction.action.interactions, function (interaction) {
                var returns = interaction.method[interaction.method.returns];
                return returns ? returns.name === this.value : false;
            }, this);
        }
    };

    ComponentEditor.factory('ArgumentModel', function (
        astCreatorService,
        stringToLiteralService
    ) {
        return createArgumentModelConstructor(astCreatorService, stringToLiteralService);
    });