source: trip-planner-front/node_modules/delegates/index.js@ 1ad8e64

Last change on this file since 1ad8e64 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 2.0 KB
Line 
1
2/**
3 * Expose `Delegator`.
4 */
5
6module.exports = Delegator;
7
8/**
9 * Initialize a delegator.
10 *
11 * @param {Object} proto
12 * @param {String} target
13 * @api public
14 */
15
16function Delegator(proto, target) {
17 if (!(this instanceof Delegator)) return new Delegator(proto, target);
18 this.proto = proto;
19 this.target = target;
20 this.methods = [];
21 this.getters = [];
22 this.setters = [];
23 this.fluents = [];
24}
25
26/**
27 * Delegate method `name`.
28 *
29 * @param {String} name
30 * @return {Delegator} self
31 * @api public
32 */
33
34Delegator.prototype.method = function(name){
35 var proto = this.proto;
36 var target = this.target;
37 this.methods.push(name);
38
39 proto[name] = function(){
40 return this[target][name].apply(this[target], arguments);
41 };
42
43 return this;
44};
45
46/**
47 * Delegator accessor `name`.
48 *
49 * @param {String} name
50 * @return {Delegator} self
51 * @api public
52 */
53
54Delegator.prototype.access = function(name){
55 return this.getter(name).setter(name);
56};
57
58/**
59 * Delegator getter `name`.
60 *
61 * @param {String} name
62 * @return {Delegator} self
63 * @api public
64 */
65
66Delegator.prototype.getter = function(name){
67 var proto = this.proto;
68 var target = this.target;
69 this.getters.push(name);
70
71 proto.__defineGetter__(name, function(){
72 return this[target][name];
73 });
74
75 return this;
76};
77
78/**
79 * Delegator setter `name`.
80 *
81 * @param {String} name
82 * @return {Delegator} self
83 * @api public
84 */
85
86Delegator.prototype.setter = function(name){
87 var proto = this.proto;
88 var target = this.target;
89 this.setters.push(name);
90
91 proto.__defineSetter__(name, function(val){
92 return this[target][name] = val;
93 });
94
95 return this;
96};
97
98/**
99 * Delegator fluent accessor
100 *
101 * @param {String} name
102 * @return {Delegator} self
103 * @api public
104 */
105
106Delegator.prototype.fluent = function (name) {
107 var proto = this.proto;
108 var target = this.target;
109 this.fluents.push(name);
110
111 proto[name] = function(val){
112 if ('undefined' != typeof val) {
113 this[target][name] = val;
114 return this;
115 } else {
116 return this[target][name];
117 }
118 };
119
120 return this;
121};
Note: See TracBrowser for help on using the repository browser.