All files / libs/storage Config.js

100% Statements 20/20
100% Branches 6/6
100% Functions 5/5
100% Lines 19/19
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                  1x             2x   2x 2x               1x 2x               1x 5x               1x 2x 2x 1x   2x                 1x 3x 3x 2x   3x     1x  
/**
 * 简化版的缓存配置
 *
 * @module      libs/storage/Config
 * @createdAt   2016-07-21
 *
 * @copyright   Copyright (c) 2016 Zhonglei Qiu
 * @license     Licensed under the MIT license.
 */
var DotProp = require('../lang/DotProp')
 
/**
 * @class
 * @param {Storage} storage
 */
function Config(storage) {
  if (!storage.data) storage.initSync()
 
  this.dp = new DotProp(storage.data)
  this.storage = storage
}
 
/**
 * 是否存在某个 key
 * @param  {String}  key
 * @return {Boolean}
 */
Config.prototype.has = function(key) {
  return this.dp.has(key)
}
 
/**
 * 获取某个 key 对应的值
 * @param  {String} key
 * @return {*}
 */
Config.prototype.get = function(key) {
  return this.dp.get(key)
}
 
/**
 * 删除某个 key
 * @param  {String} key
 * @return {Boolean}     是否删除成功
 */
Config.prototype.del = function(key) {
  var rtn = this.dp.del(key)
  if (rtn) {
    this.storage.updateSync()
  }
  return rtn
}
 
/**
 * 修改某个 key 上对应的值
 * @param {String} key
 * @param {*} val
 * @return {Boolean} 是否修改成功
 */
Config.prototype.set = function(key, val) {
  var rtn = this.dp.set(key, val)
  if (rtn) {
    this.storage.updateSync()
  }
  return rtn
}
 
module.exports = Config