<commit_before>/*jslint browser: true */
/*globals define, require */

define(['angular', 'lodash', "./registry"], function (angular, lodash, registry) {
    'use strict';

    var api_url = '',
        ModelService = 'ModelManager';

    return angular.module('RestTopModel', ['ng'])

        .factory('ModelManager', ['$q', function ($q) {

            return {
                get: function (key) {
                    return registry.getModel(key);
                },

                getInstance: function (modelPath, data) {
                    var that = this,
                        create_model = function (Model, rawData, relations) {
                            var instance = new Model(rawData);

                            // On a filtré parmi les relations du modèle, on ne traite que celles qui correspondent
                            // à des données dans cet objet

                            return $q.all(lodash.map(relations, function (modelPath2, relationName) {
                                return that.getInstance(modelPath2, instance[relationName]).then(function (whatINeed) {
                                    instance[relationName] = whatINeed;
                                });
                            })).then(function () {
                                return instance;
                            });
                        };

                    // si on n'a pas reçu de données, on s'assure quand même qu'on ait un objet.
                    if (!data) {
                        data = {};
                    }

                    return registry.getModel(modelPath).then(function (Model) {
                        var relations;
                        if (lodash.isArray(data)) {
                            return $q.all(lodash.map(data, function (subdata) {
                                relations = lodash.pick(Model.relations, function (value, key) {
                                    /*jslint unparam:true*/
                                    return subdata[key] !== undefined;
                                });
                                return create_model(Model, subdata, relations);
                            })).then(function (collection) {
                                return collection;
                            });
                        }

                        relations = lodash.pick(Model.relations, function (value, key) {
                            /*jslint unparam:true*/
                            return data[key] !== undefined && data[key];
                        });
                        return create_model(Model, data, relations);
                    });
                },

                inject: function (httpResponse) {
                    var Model = httpResponse.config.model || false,
                        defered = $q.defer();

                    if (!Model) {
                        defered.resolve(httpResponse.data);
                        return defered.promise;
                    }

                    return this.getInstance(Model, httpResponse.data);
                }
            };
        }])

        .provider('Api', function () {
            /**
             *  Définition du chemin de base de l'API REST.
             *
             *  @param {String} api_url Lien de base de l'API.
             *  @return {this}
             */
            this.setApiUrl = function (new_api_url) {
                api_url = /\/$/.test(new_api_url) ?
                        new_api_url.substring(0, new_api_url.length - 1) :
                        new_api_url;

                return this;
            };
<commit_msg>- added support for empty rest response<commit_after>/*jslint browser: true */
/*globals define, require */

define(['angular', 'lodash', "./registry"], function (angular, lodash, registry) {
    'use strict';

    var api_url = '',
        ModelService = 'ModelManager';

    return angular.module('RestTopModel', ['ng'])

        .factory('ModelManager', ['$q', function ($q) {

            return {
                get: function (key) {
                    return registry.getModel(key);
                },

                getInstance: function (modelPath, data) {
                    var that = this,
                        create_model = function (Model, rawData, relations) {
                            var instance = new Model(rawData);

                            // On a filtré parmi les relations du modèle, on ne traite que celles qui correspondent
                            // à des données dans cet objet

                            return $q.all(lodash.map(relations, function (modelPath2, relationName) {
                                return that.getInstance(modelPath2, instance[relationName]).then(function (whatINeed) {
                                    instance[relationName] = whatINeed;
                                });
                            })).then(function () {
                                return instance;
                            });
                        };

                    // si on n'a pas reçu de données, on s'assure quand même qu'on ait un objet.
                    if (!data) {
                        data = {};
                    }

                    return registry.getModel(modelPath).then(function (Model) {
                        var relations;
                        if (lodash.isArray(data)) {
                            return $q.all(lodash.map(data, function (subdata) {
                                relations = lodash.pick(Model.relations, function (value, key) {
                                    /*jslint unparam:true*/
                                    return subdata[key] !== undefined;
                                });
                                return create_model(Model, subdata, relations);
                            })).then(function (collection) {
                                return collection;
                            });
                        }

                        relations = lodash.pick(Model.relations, function (value, key) {
                            /*jslint unparam:true*/
                            return data[key] !== undefined && data[key];
                        });
                        return create_model(Model, data, relations);
                    });
                },

                inject: function (httpResponse) {
                    var Model = httpResponse.config.model || false,
                        defered = $q.defer();

                    if (!httpResponse.data) {
                        defered.resolve(null);
                        return defered.promise;
                    }

                    if (!Model) {
                        defered.resolve(httpResponse.data);
                        return defered.promise;
                    }

                    return this.getInstance(Model, httpResponse.data);
                }
            };
        }])

        .provider('Api', function () {
            /**
             *  Définition du chemin de base de l'API REST.
             *
             *  @param {String} api_url Lien de base de l'API.
             *  @return {this}
             */
            this.setApiUrl = function (new_api_url) {
                api_url = /\/$/.test(new_api_url) ?
                        new_api_url.substring(0, new_api_url.length - 1) :
                        new_api_url;

                return this;
            };

{'fraction_non_alphanumeric': 0.09604595244456318, 'fraction_numerical': 0.0010686615014694097, 'mean_word_length': 0.9843625761993109, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1918488', 'n_tokens_mistral': 1781, 'n_tokens_neox': 1638, 'n_words': 397}