|
"use strict"; |
|
module.exports = ReflectionObject; |
|
|
|
ReflectionObject.className = "ReflectionObject"; |
|
|
|
var util = require("./util"); |
|
|
|
var Root; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function ReflectionObject(name, options) { |
|
|
|
if (!util.isString(name)) |
|
throw TypeError("name must be a string"); |
|
|
|
if (options && !util.isObject(options)) |
|
throw TypeError("options must be an object"); |
|
|
|
|
|
|
|
|
|
|
|
this.options = options; |
|
|
|
|
|
|
|
|
|
|
|
this.parsedOptions = null; |
|
|
|
|
|
|
|
|
|
|
|
this.name = name; |
|
|
|
|
|
|
|
|
|
|
|
this.parent = null; |
|
|
|
|
|
|
|
|
|
|
|
this.resolved = false; |
|
|
|
|
|
|
|
|
|
|
|
this.comment = null; |
|
|
|
|
|
|
|
|
|
|
|
this.filename = null; |
|
} |
|
|
|
Object.defineProperties(ReflectionObject.prototype, { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
root: { |
|
get: function() { |
|
var ptr = this; |
|
while (ptr.parent !== null) |
|
ptr = ptr.parent; |
|
return ptr; |
|
} |
|
}, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fullName: { |
|
get: function() { |
|
var path = [ this.name ], |
|
ptr = this.parent; |
|
while (ptr) { |
|
path.unshift(ptr.name); |
|
ptr = ptr.parent; |
|
} |
|
return path.join("."); |
|
} |
|
} |
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
ReflectionObject.prototype.toJSON = function toJSON() { |
|
throw Error(); |
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
ReflectionObject.prototype.onAdd = function onAdd(parent) { |
|
if (this.parent && this.parent !== parent) |
|
this.parent.remove(this); |
|
this.parent = parent; |
|
this.resolved = false; |
|
var root = parent.root; |
|
if (root instanceof Root) |
|
root._handleAdd(this); |
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
ReflectionObject.prototype.onRemove = function onRemove(parent) { |
|
var root = parent.root; |
|
if (root instanceof Root) |
|
root._handleRemove(this); |
|
this.parent = null; |
|
this.resolved = false; |
|
}; |
|
|
|
|
|
|
|
|
|
|
|
ReflectionObject.prototype.resolve = function resolve() { |
|
if (this.resolved) |
|
return this; |
|
if (this.root instanceof Root) |
|
this.resolved = true; |
|
return this; |
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
ReflectionObject.prototype.getOption = function getOption(name) { |
|
if (this.options) |
|
return this.options[name]; |
|
return undefined; |
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ReflectionObject.prototype.setOption = function setOption(name, value, ifNotSet) { |
|
if (!ifNotSet || !this.options || this.options[name] === undefined) |
|
(this.options || (this.options = {}))[name] = value; |
|
return this; |
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ReflectionObject.prototype.setParsedOption = function setParsedOption(name, value, propName) { |
|
if (!this.parsedOptions) { |
|
this.parsedOptions = []; |
|
} |
|
var parsedOptions = this.parsedOptions; |
|
if (propName) { |
|
|
|
|
|
var opt = parsedOptions.find(function (opt) { |
|
return Object.prototype.hasOwnProperty.call(opt, name); |
|
}); |
|
if (opt) { |
|
|
|
var newValue = opt[name]; |
|
util.setProperty(newValue, propName, value); |
|
} else { |
|
|
|
opt = {}; |
|
opt[name] = util.setProperty({}, propName, value); |
|
parsedOptions.push(opt); |
|
} |
|
} else { |
|
|
|
var newOpt = {}; |
|
newOpt[name] = value; |
|
parsedOptions.push(newOpt); |
|
} |
|
return this; |
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ReflectionObject.prototype.setOptions = function setOptions(options, ifNotSet) { |
|
if (options) |
|
for (var keys = Object.keys(options), i = 0; i < keys.length; ++i) |
|
this.setOption(keys[i], options[keys[i]], ifNotSet); |
|
return this; |
|
}; |
|
|
|
|
|
|
|
|
|
|
|
ReflectionObject.prototype.toString = function toString() { |
|
var className = this.constructor.className, |
|
fullName = this.fullName; |
|
if (fullName.length) |
|
return className + " " + fullName; |
|
return className; |
|
}; |
|
|
|
|
|
ReflectionObject._configure = function(Root_) { |
|
Root = Root_; |
|
}; |
|
|