<commit_before>'use babel';

import {Emitter} from 'atom';
import _ from 'underscore-plus';
import Settings from './settings';
import DB from './db';

export default class Project {

  constructor(props={}) {
    this.props = {};
    this.emitter = new Emitter();
    this.db = new DB();
    this.updateProps(props);
    this.lookForUpdates();
  }

  get requiredProperties() {
    return ['title', 'paths'];
  }

  get defaultProps() {
    return {
      title: '',
      paths: [],
      icon: 'icon-chevron-right',
      settings: {},
      group: null,
      devMode: false,
      template: null
    };
  }

  updateProps(props) {
    this.props = _.deepExtend(this.defaultProps, props);
  }

  getPropsToSave() {
    let saveProps = {};
    let value;
    for (key in this.props) {
      value = this.props[key];
      if (!this.isDefaultProp(key, value)) {
        saveProps[key] = value;
      }
    }

    return saveProps;
  }

  isDefaultProp(key, value) {
    if (!this.defaultProps[key]) {
      return false;
    }

    if (this.defaultProps[key] === value) {
      return true;
    }

    return false;
  }

  set(key, value) {
    this.props[key] = value;
    this.save();
  }

  unset(key) {
    if (_.has(this.defaultProps, key)) {
      this.props[key] = this.defaultProps[key];
    } else {
      this.props[key] = null;
    }

    this.save();
  }

  lookForUpdates() {
    if (this.props._id) {
      this.db.setSearchQuery('_id', this.props._id);
      this.db.onUpdate((props) => {
        if (props) {
          let updatedProps = _.deepExtend(this.defaultProps, props);
          if (!_.isEqual(this.props, updatedProps)) {
            this.updateProps(props);
            this.emitter.emit('updated');
            if (this.isCurrent()) {
              this.load();
            }
          }
        } else {
          this.db.setSearchQuery('paths', this.props.paths);
          this.db.find((props) => {
            this.updateProps(props);
            this.db.setSearchQuery('_id', this.props._id);
            this.emitter.emit('updated');
            if (this.isCurrent()) {
              this.load();
            }
          });
        }
      });
    }
  }

  isCurrent() {
    let activePath = atom.project.getPaths()[0];
    let mainPath = this.props.paths[0];
    if (activePath === mainPath) {
      return true;
    }

    return false;
  }

  isValid() {
    let valid = true;
    for (key of this.requiredProperties) {
      if (!this.props[key] || !this.props[key].length) {
        valid = false;
      }
    }

    return valid;
  }

  load() {
    if (this.isCurrent()) {
      let projectSettings = new Settings();
      projectSettings.load(this.props.settings);
    }
  }

  save() {
    if (this.isValid()) {
      if (this.props._id) {
        this.db.update(this.getPropsToSave());
      } else {
        this.db.add(this.getPropsToSave(), (id) => {
          this.props._id = id;
          this.lookForUpdates();
        });
      }

      return true;
    }

    return false;
  }

  remove() {
    this.db.delete(this.props._id);
  }

  open() {
    atom.open({
      pathsToOpen: this.props.paths,
      devMode: this.props.devMode
    });
  }

  onUpdate(callback) {
    this.emitter.on('updated', () => {
      callback();
    });<commit_msg>Use inline function<commit_after>'use babel';

import {Emitter} from 'atom';
import _ from 'underscore-plus';
import Settings from './settings';
import DB from './db';

export default class Project {

  constructor(props={}) {
    this.props = {};
    this.emitter = new Emitter();
    this.db = new DB();
    this.updateProps(props);
    this.lookForUpdates();
  }

  get requiredProperties() {
    return ['title', 'paths'];
  }

  get defaultProps() {
    return {
      title: '',
      paths: [],
      icon: 'icon-chevron-right',
      settings: {},
      group: null,
      devMode: false,
      template: null
    };
  }

  updateProps(props) {
    this.props = _.deepExtend(this.defaultProps, props);
  }

  getPropsToSave() {
    let saveProps = {};
    let value;
    for (key in this.props) {
      value = this.props[key];
      if (!this.isDefaultProp(key, value)) {
        saveProps[key] = value;
      }
    }

    return saveProps;
  }

  isDefaultProp(key, value) {
    if (!this.defaultProps[key]) {
      return false;
    }

    if (this.defaultProps[key] === value) {
      return true;
    }

    return false;
  }

  set(key, value) {
    this.props[key] = value;
    this.save();
  }

  unset(key) {
    if (_.has(this.defaultProps, key)) {
      this.props[key] = this.defaultProps[key];
    } else {
      this.props[key] = null;
    }

    this.save();
  }

  lookForUpdates() {
    if (this.props._id) {
      this.db.setSearchQuery('_id', this.props._id);
      this.db.onUpdate((props) => {
        if (props) {
          let updatedProps = _.deepExtend(this.defaultProps, props);
          if (!_.isEqual(this.props, updatedProps)) {
            this.updateProps(props);
            this.emitter.emit('updated');
            if (this.isCurrent()) {
              this.load();
            }
          }
        } else {
          this.db.setSearchQuery('paths', this.props.paths);
          this.db.find((props) => {
            this.updateProps(props);
            this.db.setSearchQuery('_id', this.props._id);
            this.emitter.emit('updated');
            if (this.isCurrent()) {
              this.load();
            }
          });
        }
      });
    }
  }

  isCurrent() {
    let activePath = atom.project.getPaths()[0];
    let mainPath = this.props.paths[0];
    if (activePath === mainPath) {
      return true;
    }

    return false;
  }

  isValid() {
    let valid = true;
    for (key of this.requiredProperties) {
      if (!this.props[key] || !this.props[key].length) {
        valid = false;
      }
    }

    return valid;
  }

  load() {
    if (this.isCurrent()) {
      let projectSettings = new Settings();
      projectSettings.load(this.props.settings);
    }
  }

  save() {
    if (this.isValid()) {
      if (this.props._id) {
        this.db.update(this.getPropsToSave());
      } else {
        this.db.add(this.getPropsToSave(), (id) => {
          this.props._id = id;
          this.lookForUpdates();
        });
      }

      return true;
    }

    return false;
  }

  remove() {
    this.db.delete(this.props._id);
  }

  open() {
    atom.open({
      pathsToOpen: this.props.paths,
      devMode: this.props.devMode
    });
  }

  onUpdate(callback) {
    this.emitter.on('updated', () => callback());
{'fraction_non_alphanumeric': 0.15773493244269016, 'fraction_numerical': 0.0006072567177774404, 'mean_word_length': 1.934521158129176, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, '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': '2523708', 'n_tokens_mistral': 2185, 'n_tokens_neox': 2128, 'n_words': 392}