<commit_before>var nodeca = require('nodeca-lib'),
    Store = nodeca.Settings.Store,
    $$ = nodeca.Utilities;


var Promise = require('simple-promise');


module.exports = (function (app, callback) {
  var UserGroupStore = function UserGroupStore() {
    Store.call(this);


    var STORE_KEY = '__usergroup_store__',
        UserGroup = app.model('UserGroup');


    this.__.preloader = function (env, callback) {
      // environment was preloaded before
      if (env[STORE_KEY]) {
        callback();
        return;
      }

      // env.groups was neither specified directly nor found in user
      if (!Array.isArray(env.groups) || 0 == env.groups.length) {
        callback(Error("Can't find list of groups"));
        return;
      }

      UserGroup.find({_id: {$in: env.groups}}, function (err, groups) {
        env[STORE_KEY] = groups;
        callback(err);
      });
    };


    this.__.setter = function (data, env, callback) {
      var settings = {};

      // Getter works with multiple groups.
      // Setter NEVER work with moe than one for integrity reasons.
      if (1 != env.groups.length) {
        callback(Error("Can't set on multiple groups"));
        return;
      }

      $$.each(data, function (key, val) {
        settings['settings.' + key] = val;
      });

      UserGroup.update({_id: env.groups[0]}, settings, callback);
    };


    this.__.getter = function (keys, env, callback) {
      var result = {};

      keys.forEach(function (key) {
        var chains;

        if (1 == env[STORE_KEY].length) {
          result[key] = env[STORE_KEY].settings[key];
          return;
        }

        // TODO: rework to support non-boolean vals as well

        // separate strict and non-strict values
        chains = {OR: [], AND: []};
        env[STORE_KEY].forEach(function (g) {
          var op = g.settings[key].strict ? 'AND' : 'OR';
          chains[op].push(!!g.settings[key].value);
        });

        result[key] = {strict: false, value: false};

        if (0 < chains.AND.length) {
          result[key].strict = true;
        }

        if (0 == chains.OR.length) {
          result[key].value = (false == $$.includes(chains.AND, false));
        } else {
          result[key].value = (
            (true == $$.includes(chains.OR, true))
            &&
            (false == $$.includes(chains.AND, false))
          );
        }
      });

      callback(null, result);
    };
  };


  Store.adopts(UserGroupStore);


  callback(null, new UserGroupStore());
});


////////////////////////////////////////////////////////////////////////////////
// vim:ts=2:sw=2
////////////////////////////////////////////////////////////////////////////////<commit_msg>reduced conditions<commit_after>var nodeca = require('nodeca-lib'),
    Store = nodeca.Settings.Store,
    $$ = nodeca.Utilities;


var Promise = require('simple-promise');


module.exports = (function (app, callback) {
  var UserGroupStore = function UserGroupStore() {
    Store.call(this);


    var STORE_KEY = '__usergroup_store__',
        UserGroup = app.model('UserGroup');


    this.__.preloader = function (env, callback) {
      // environment was preloaded before
      if (env[STORE_KEY]) {
        callback();
        return;
      }

      // env.groups was neither specified directly nor found in user
      if (!Array.isArray(env.groups) || 0 == env.groups.length) {
        callback(Error("Can't find list of groups"));
        return;
      }

      UserGroup.find({_id: {$in: env.groups}}, function (err, groups) {
        env[STORE_KEY] = groups;
        callback(err);
      });
    };


    this.__.setter = function (data, env, callback) {
      var settings = {};

      // Getter works with multiple groups.
      // Setter NEVER work with moe than one for integrity reasons.
      if (1 != env.groups.length) {
        callback(Error("Can't set on multiple groups"));
        return;
      }

      $$.each(data, function (key, val) {
        settings['settings.' + key] = val;
      });

      UserGroup.update({_id: env.groups[0]}, settings, callback);
    };


    this.__.getter = function (keys, env, callback) {
      var result = {};

      keys.forEach(function (key) {
        var chains;

        if (1 == env[STORE_KEY].length) {
          result[key] = env[STORE_KEY].settings[key];
          return;
        }

        // TODO: rework to support non-boolean vals as well

        // separate strict and non-strict values
        chains = {OR: [], AND: []};
        env[STORE_KEY].forEach(function (g) {
          var op = g.settings[key].strict ? 'AND' : 'OR';
          chains[op].push(!!g.settings[key].value);
        });

        result[key] = {strict: false, value: false};

        if (0 < chains.AND.length) {
          result[key].strict = true;
        }

        if (0 == chains.OR.length) {
          result[key].value = !($$.includes(chains.AND, false));
        } else {
          result[key].value = (
            $$.includes(chains.OR, true)
            &&
            !($$.includes(chains.AND, false))
          );
        }
      });

      callback(null, result);
    };
  };


  Store.adopts(UserGroupStore);


  callback(null, new UserGroupStore());
});


////////////////////////////////////////////////////////////////////////////////
// vim:ts=2:sw=2
////////////////////////////////////////////////////////////////////////////////
{'fraction_non_alphanumeric': 0.2025503603770098, 'fraction_numerical': 0.0029569395675475884, 'mean_word_length': 1.7250755287009063, 'pattern_counts': {'":': 0, '<': 5, '<?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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1036776', 'n_tokens_mistral': 1590, 'n_tokens_neox': 1522, 'n_words': 354}