Advertisement
Guest User

Untitled

a guest
Feb 12th, 2016
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 'use strict';
  2.  
  3. Object.defineProperty(exports, "__esModule", {
  4.   value: true
  5. });
  6.  
  7. var _assign = require('babel-runtime/core-js/object/assign');
  8.  
  9. var _assign2 = _interopRequireDefault(_assign);
  10.  
  11. var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');
  12.  
  13. var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
  14.  
  15. var _createClass2 = require('babel-runtime/helpers/createClass');
  16.  
  17. var _createClass3 = _interopRequireDefault(_createClass2);
  18.  
  19. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  20.  
  21. var Seed = function () {
  22.   function Seed(name, collection, options) {
  23.     (0, _classCallCheck3.default)(this, Seed);
  24.  
  25.     if (!collection || !options) {
  26.       var msg = 'Please supply a collection to seed and options for seeding.';
  27.       throw new Error(msg);
  28.     } else {
  29.       this.name = name;
  30.       this.collection = collection;
  31.       this.options = options;
  32.       this.isDataArray = this.options.data instanceof Array;
  33.       this.context = {
  34.         name: name,
  35.         collection: collection,
  36.         startResponse: null
  37.       };
  38.  
  39.       this.seed();
  40.     }
  41.   }
  42.  
  43.   (0, _createClass3.default)(Seed, [{
  44.     key: 'shouldSeed',
  45.     value: function shouldSeed() {
  46.       var condition = this.options.condition;
  47.  
  48.       return condition ? condition.call(this.context) : true;
  49.     }
  50.   }, {
  51.     key: 'seed',
  52.     value: function seed() {
  53.       var options = this.options;
  54.       var data = options.data;
  55.       var onStart = options.onStart;
  56.       var onFinish = options.onFinish;
  57.       var onSkip = options.onSkip;
  58.  
  59.       if (this.shouldSeed()) {
  60.         if (onStart) {
  61.           this.context.startResponse = onStart.call(this.context);
  62.         }
  63.  
  64.         this.plant(data);
  65.  
  66.         if (onFinish) {
  67.           onFinish.call(this.context);
  68.         }
  69.       } else if (onSkip) {
  70.         onSkip.call(this.context);
  71.       }
  72.     }
  73.   }, {
  74.     key: 'plant',
  75.     value: function plant(data) {
  76.       var loopLength = this._loopLength();
  77.  
  78.       for (var i = 0; i < loopLength; i++) {
  79.         var value = this.isDataArray ? data[i] : data.call(this.context, i);
  80.  
  81.         this.collection.insert(value);
  82.       }
  83.     }
  84.   }, {
  85.     key: '_loopLength',
  86.     value: function _loopLength() {
  87.       var random = function random(min, max) {
  88.         return Math.floor(Math.random() * (max - min + 1)) + min;
  89.       };
  90.  
  91.       return this.isDataArray ? this.options.data.length : random(this.options.min, this.options.max);
  92.     }
  93.   }]);
  94.   return Seed;
  95. }();
  96.  
  97. var Seeder = {
  98.   settings: {
  99.     condition: function condition() {
  100.       return this.collection.find().count() === 0;
  101.     },
  102.  
  103.     min: 1,
  104.     max: 20,
  105.     onStart: function onStart() {
  106.       console.log('Seeder: ' + this.name + '\t => Started');
  107.     },
  108.     onFinish: function onFinish() {
  109.       console.log('Seeder: ' + this.name + '\t => Finished');
  110.     },
  111.     onSkip: function onSkip() {
  112.       console.log('Seeder: ' + this.name + '\t => Skipped');
  113.     }
  114.   },
  115.  
  116.   config: function config(options) {
  117.     (0, _assign2.default)(this.settings, options);
  118.   },
  119.   seed: function seed(name, collection, options) {
  120.     var finalOptions = (0, _assign2.default)({}, this.settings, options);
  121.     return new Seed(name, collection, finalOptions);
  122.   }
  123. };
  124.  
  125. exports.default = Seeder;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement