<commit_before>var _ = require('lodash');
var cheerio = require('cheerio');
var chalk = require('chalk');
var Table = require('cli-table');
var Utility = require('../utility');
var Model = require('../model');

var IgniteModel = function(definition, options) {
  
  if (!(this instanceof IgniteModel)) return new IgniteModel(definition, options);

  this.options = _.defaults(options || {}, this.options);
  
  _.extend(this, Model.call(this, definition, options));
  
};

_.extend(IgniteModel.prototype, Model.prototype);

IgniteModel.prototype.options = {
  table: true,
  table_item_count: 10,
  item_max_length: 50,
  item_format: false
};

IgniteModel.prototype.parse = function(html) {
  
  var $ = cheerio.load(html);
  var self = this;
  var collection;
  var iteration;

  console.log(chalk.gray('Model: ') + chalk.cyan(self.name));
  
  if (self.type === 'collection') {
    
    collection = self._parseCollection($);
    iteration = self._parseIteration($);

    // Display parsed models
    if (self.options.table) ((self.options.item_format) ? self._buildCollectionCLiTable(collection) : self._buildIterationCLiTable(iteration));

    // Display summary table
    self._buildSummaryTable(collection.length, (iteration.length - collection.length), iteration.length);
  
  }

  if (self.type === 'item') {
    
    if (self.options.item_format) {
      
      self._buildItemCLiTable(self._applyFilters(self._parseItem($)))
      
    } else {
      
      self._buildItemCLiTable(self._parseItem($))
      
    }
    
  }

  return Model.prototype.parse.call(this, html);
  
};

IgniteModel.prototype._buildItemCLiTable = function(item) {

  var table = new Table({
    style: {
      head: [ 'blue' ]
    }
  });
  var row = {};

  _.forOwn(item, function(val, key) {
    
    row = {};
    row[key] = val;
    table.push(row);
    
  });

  return console.log(table.toString())
  
};

IgniteModel.prototype._buildCollectionCLiTable = function(collection) {
  
  var self = this;
  var table = new Table({
        head: _.keys(self.elements),
        style: {
          head: [ 'blue' ]
        }
      });
  
  _.chain(collection)
    .first(_.parseInt(self.options.table_item_count))
    .map(function(item) {
      
      return _.chain(item)
        .values()
        .map(Utility.truncateString(_.parseInt(self.options.item_max_length)))
        .value()
      
    })
    .map(function(item) {
      
      table.push(item)
      
    });

  return console.log(table.toString())
  
};

IgniteModel.prototype._buildIterationCLiTable = function(iteration) {
  
  var self = this;
  var item;
  var table = new Table({
      head: _.keys(self.elements),
      style: {
        head: [ 'blue' ]
      }
    });
  
  _.chain(iteration)
    .first(_.parseInt(self.options.table_item_count))
    .map(function($) {
      
      item = self._parseItem($);
      
      return _.chain(item)
        .values()
        .map(Utility.truncateString(_.parseInt(self.options.item_max_length)))
        .map(function(property) {
          
          return self.validate(item) ? chalk.white(property) : chalk.gray(property)
          
        })
        .value()
      
    })
    .map(function(item) {
      
      table.push(item)
      
    });

  return console.log(table.toString())
  
};

IgniteModel.prototype._buildSummaryTable = function(valid, rejected, total) {

  var table = new Table({
    style: {
      head: [ 'white' ]
    }
  });
  
  table.push(
    { Total: chalk.yellow(total) },<commit_msg>Fixed issue with iteration cli table ig ignite-model Added case to handle is model.validate is not defined<commit_after>var _ = require('lodash');
var cheerio = require('cheerio');
var chalk = require('chalk');
var Table = require('cli-table');
var Utility = require('../utility');
var Model = require('../model');

var IgniteModel = function(definition, options) {
  
  if (!(this instanceof IgniteModel)) return new IgniteModel(definition, options);

  this.options = _.defaults(options || {}, this.options);
  
  _.extend(this, Model.call(this, definition, options));
  
};

_.extend(IgniteModel.prototype, Model.prototype);

IgniteModel.prototype.options = {
  table: true,
  table_item_count: 10,
  item_max_length: 50,
  item_format: false
};

IgniteModel.prototype.parse = function(html) {
  
  var $ = cheerio.load(html);
  var self = this;
  var collection;
  var iteration;

  console.log(chalk.gray('Model: ') + chalk.cyan(self.name));
  
  if (self.type === 'collection') {
    
    collection = self._parseCollection($);
    iteration = self._parseIteration($);

    // Display parsed models
    if (self.options.table) ((self.options.item_format) ? self._buildCollectionCLiTable(collection) : self._buildIterationCLiTable(iteration));

    // Display summary table
    self._buildSummaryTable(collection.length, (iteration.length - collection.length), iteration.length);
  
  }

  if (self.type === 'item') {
    
    if (self.options.item_format) {
      
      self._buildItemCLiTable(self._applyFilters(self._parseItem($)))
      
    } else {
      
      self._buildItemCLiTable(self._parseItem($))
      
    }
    
  }

  return Model.prototype.parse.call(this, html);
  
};

IgniteModel.prototype._buildItemCLiTable = function(item) {

  var table = new Table({
    style: {
      head: [ 'blue' ]
    }
  });
  var row = {};

  _.forOwn(item, function(val, key) {
    
    row = {};
    row[key] = val;
    table.push(row);
    
  });

  return console.log(table.toString())
  
};

IgniteModel.prototype._buildCollectionCLiTable = function(collection) {
  
  var self = this;
  var table = new Table({
        head: _.keys(self.elements),
        style: {
          head: [ 'blue' ]
        }
      });
  
  _.chain(collection)
    .first(_.parseInt(self.options.table_item_count))
    .map(function(item) {
      
      return _.chain(item)
        .values()
        .map(Utility.truncateString(_.parseInt(self.options.item_max_length)))
        .value()
      
    })
    .map(function(item) {
      
      table.push(item)
      
    });

  return console.log(table.toString())
  
};

IgniteModel.prototype._buildIterationCLiTable = function(iteration) {
  
  var self = this;
  var item;
  var table = new Table({
      head: _.keys(self.elements),
      style: {
        head: [ 'blue' ]
      }
    });
  
  _.chain(iteration)
    .first(_.parseInt(self.options.table_item_count))
    .map(function($) {
      
      item = self._parseItem($);
      
      return _.chain(item)
        .values()
        .map(Utility.truncateString(_.parseInt(self.options.item_max_length)))
        .map(function(property) {
          
          if (!_.isUndefined(self.validate))
            return self.validate(item) ? chalk.white(property) : chalk.gray(property)
          
          return chalk.white(property)
          
        })
        .value()
      
    })
    .map(function(item) {
      
      table.push(item)
      
    });

  return console.log(table.toString())
  
};

IgniteModel.prototype._buildSummaryTable = function(valid, rejected, total) {

  var table = new Table({
    style: {
      head: [ 'white' ]
    }
  });
  
  table.push(
    { Total: chalk.yellow(total) },
{'fraction_non_alphanumeric': 0.1450719822812846, 'fraction_numerical': 0.0011074197120708748, 'mean_word_length': 2.582052553296976, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4980529', 'n_tokens_mistral': 2349, 'n_tokens_neox': 2310, 'n_words': 396}