web3.js/lib/web3/method.js

71 lines
2.1 KiB
JavaScript
Raw Normal View History

2015-03-22 17:12:52 +00:00
/*
This file is part of ethereum.js.
ethereum.js is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
ethereum.js is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with ethereum.js. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @file method.js
* @author Marek Kotewicz <marek@ethdev.com>
* @date 2015
*/
var utils = require('../utils/utils');
var errors = require('./errors');
var Method = function (options) {
this.name = options.name;
this.call = options.call;
this.params = options.params || 0;
this.inputFormatter = options.inputFormatter;
this.outputFormatter = options.outputFormatter;
};
Method.prototype.getCall = function (args) {
return utils.isFunction(this.call) ? this.call(args) : this.call;
};
Method.prototype.extractCallback = function (args) {
if (utils.isFunction(args[args.length - 1])) {
return args.pop(); // modify the args array!
}
return null;
};
Method.prototype.validateArgs = function (args) {
if (args.length !== this.params) {
throw errors.InvalidNumberOfParams;
}
};
Method.prototype.formatInput = function (args) {
return this.inputFormatter ? this.inputFormatter(args) : args;
};
Method.prototype.formatOutput = function (result) {
return this.outputFormatter && result ? this.outputFormatter(result) : result;
};
Method.prototype.attachToObject = function (obj, func) {
var name = this.name.split('.');
if (name.length > 1) {
obj[name[0]] = obj[name[0]] || {};
obj[name[0]][name[1]] = func;
} else {
obj[name[0]] = func;
}
};
module.exports = Method;