-
Notifications
You must be signed in to change notification settings - Fork 3
/
GetSet.js
152 lines (138 loc) · 4.29 KB
/
GetSet.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/************************************************************/
//Class GetSet
//Purpose: Creates dynamic getters and setters
/************************************************************/
var GetSet = {};
//=========================================================//
//Public Method override
//Purpose: Override default values through iteration
//Parameters:
// obj: The object whose default values will be overridden
//Postcondition: options Object is altered
//=========================================================//
GetSet.override = function(options, defaults)
{
//Store this scope
var $this = options;
for (var i in defaults)
{
if(!($this[i]))
{
$this[i] = defaults[i];
}
}
};
//=========================================================//
//Public getFunctions
//Purpose: Copies one objects functions to another
//Parameters:
// template: The object whose method will be copied
// recepient: The object receiving the template methods
//Postcondition: recepient object is altered
//=========================================================//
GetSet.getFunctions = function(template, recepient)
{
for (var i in template)
{
if(template[i].constructor == Function)
{
recepient[i] = template[i];
}
}
};
//=========================================================//
//Public Method gettters
//Purpose: Dynamically creates accessor methods(getters)
//Parameters:
// scope: The scope in which the accessor methods will be
// applied
// prefix: Goes before the property. i.e. (get)Name
// camel: whether to induce camel case
// obj: Accessors
//Postcondition: scope has been altered to include
//accessor methods
//=========================================================//
GetSet.getters = function(options)
{
//Over-ride default values
var defaults =
{
prefix: "get",
camel: true
};
//Override defaults values
GetSet.override(options, defaults);
//If prefix is set to 'none', force blank. A blank string as a parameter
//evaluates to null for some reason.
options.prefix = (options.prefix === "none") ? "" : options.prefix;
//Iterate through the properties of the object
var str;
for ( var i in options.obj )
{
//If camel case is enabled and no blank prefix
if(options.camel && options.prefix != "")
{
str = i.charAt(0).toUpperCase() + i.substr(1);
}
else
{
str = i;
}
(function(i)
{
// Dynamically create an accessor method
options.scope[ options.prefix + str ] = function()
{
return options.obj[i];
};
})(i);
}
};
//=========================================================//
//Public Method setters
//Purpose: Dynamically creates muator methods(setters)
//Parameters:
// scope: The scope in which the mutator methods will be
// applied
// prefix: Goes before the property. i.e. (set)Name
// camel: whether to induce camel case
// obj: The object that will have mutators
//Postcondition: scope has been altered to include mutator
//methods
//=========================================================//
GetSet.setters = function(options)
{
//Over-ride default values
var defaults =
{
prefix: "set",
camel: true
};
//Override defaults values
GetSet.override(options, defaults);
//If prefix is set to 'none', force blank. A blank string as a parameter
//evaluates to null for some reason.
options.prefix = (options.prefix === "none") ? "" : options.prefix;
//Iterate through the properties of the object
var str;
for ( var i in options.obj )
{
//If camel case is enabled and no blank prefix
if(options.camel && options.prefix != "")
{
str = i.charAt(0).toUpperCase() + i.substr(1);
}
else
{
str = i;
}
(function(i)
{
// Dynamically create an accessor method
options.scope[ options.prefix + str ] = function(val)
{
options.obj[i] = val;
};
})(i);
}
};