Code coverage report for lib/Page.js

Statements: 97.73% (43 / 44)      Branches: 50% (3 / 6)      Functions: 100% (12 / 12)      Lines: 97.73% (43 / 44)      Ignored: none     

All files » lib/ » Page.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111          1 1   1     1   11   11   11   11   11   11   11       1           1 5 5 5 5     5 5 15     5 5                     1 3             1 1 1 1             1 1 1 1 1               1 1 1 1 3                 1 1 1 1         1
'use strict';
/*
    Page Module
*/
 
var ApiRequest = require('./shared/ApiRequest.js');
var utils = require('./utils');
 
var DEFAULT_PAGE_ITEM_COUNT = 50;
 
// Page class
function Page(options,path,model,count,page) {
 
    this._options = options;
 
    this._template = path;
 
    this._supportedMethods = ['get'];
 
    this._model = model;
 
    this._count = count;
 
    this._page = page;
 
    this._entries = null;
}
 
 
Page.prototype = new ApiRequest();
 
 
// @aysnc
// @private
// Creates the page corresponding to the Model class
Page.prototype._createPage = function(callback) {
    Eif (!this._entries) {
        var that = this;
        that._entries = [];
        return that.query({count : that._count.toString(), page : that._page.toString()})
                   .get()
                   .then(function(resp) {
                        Eif ( 'entries' in resp ) {
                            resp.entries.forEach(function(entry) {
                                that._entries.push(new that._model(that._options,entry.self_link));
                            });
                        }
                        that.length = that._entries.length;
                        return utils.promisify(that,callback);
                   });
    } else {
        return utils.promisify(this,callback);
    }
}
 
 
// @async
// @public
// GET the current page
Page.prototype.current = function(callback) {
    return this._createPage(callback);
}
 
 
// @async
// @public
// GET the next page
Page.prototype.next = function(callback) {
    this._page++;
    this._entries = null;
    return this._createPage(callback);
}
 
 
// @aysnc
// @public
// GET the previous page
Page.prototype.previous = function(callback) {
    Eif  ( this._page > 1 ) {
        this._page--;
        this._entries = null;
        return this._createPage(callback);
    }
}
 
 
// @async
// @public
// Iterate over all the entries in the current page
Page.prototype.each = function(callback) {
    var that = this;
    that.current().then(function() {
        that._entries.forEach(function(entry) {
            callback(entry);
        });
    });
}
 
 
// @async
// @public
// Call the callback with all the entries of the current page
Page.prototype.all = function(callback) {
    var that = this;
    return that.current().then(function() {
        return utils.promisify(that._entries,callback);
    });
}
 
 
module.exports = Page;