1
+ /************************************************************/
2
+ //Class GetSet
3
+ //Purpose: Creates dynamic getters and setters
4
+ /************************************************************/
5
+
6
+ var GetSet = { } ;
7
+
8
+ //=========================================================//
9
+ //Public Method override
10
+ //Purpose: Override default values through iteration
11
+ //Parameters:
12
+ // obj: The object whose default values will be overridden
13
+ //Postcondition: options Object is altered
14
+ //=========================================================//
15
+ GetSet . override = function ( options , defaults )
16
+ {
17
+ //Store this scope
18
+ var $this = options ;
19
+
20
+
21
+ for ( var i in defaults )
22
+ {
23
+ if ( ! ( $this [ i ] ) )
24
+ {
25
+ $this [ i ] = defaults [ i ] ;
26
+ }
27
+ }
28
+ } ;
29
+
30
+ //=========================================================//
31
+ //Public getFunctions
32
+ //Purpose: Copies one objects functions to another
33
+ //Parameters:
34
+ // template: The object whose method will be copied
35
+ // recepient: The object receiving the template methods
36
+ //Postcondition: recepient object is altered
37
+ //=========================================================//
38
+ GetSet . getFunctions = function ( template , recepient )
39
+ {
40
+ for ( var i in template )
41
+ {
42
+ if ( template [ i ] . constructor == Function )
43
+ {
44
+ recepient [ i ] = template [ i ] ;
45
+ }
46
+ }
47
+ } ;
48
+
49
+
50
+ //=========================================================//
51
+ //Public Method gettters
52
+ //Purpose: Dynamically creates accessor methods(getters)
53
+ //Parameters:
54
+ // scope: The scope in which the accessor methods will be
55
+ // applied
56
+ // prefix: Goes before the property. i.e. (get)Name
57
+ // camel: whether to induce camel case
58
+ // obj: Accessors
59
+ //Postcondition: scope has been altered to include
60
+ //accessor methods
61
+ //=========================================================//
62
+ GetSet . getters = function ( options )
63
+ {
64
+ //Over-ride default values
65
+ var defaults =
66
+ {
67
+ prefix : "get" ,
68
+ camel : true
69
+ } ;
70
+
71
+ //Override defaults values
72
+ GetSet . override ( options , defaults ) ;
73
+
74
+ //If prefix is set to 'none', force blank. A blank string as a parameter
75
+ //evaluates to null for some reason.
76
+ options . prefix = ( options . prefix === "none" ) ? "" : options . prefix ;
77
+
78
+ //Iterate through the properties of the object
79
+ var str ;
80
+ for ( var i in options . obj )
81
+ {
82
+ //If camel case is enabled and no blank prefix
83
+ if ( options . camel && options . prefix != "" )
84
+ {
85
+ str = i . charAt ( 0 ) . toUpperCase ( ) + i . substr ( 1 ) ;
86
+ }
87
+ else
88
+ {
89
+ str = i ;
90
+ }
91
+ ( function ( i )
92
+ {
93
+ // Dynamically create an accessor method
94
+ options . scope [ options . prefix + str ] = function ( )
95
+ {
96
+ return options . obj [ i ] ;
97
+ } ;
98
+ } ) ( i ) ;
99
+ }
100
+ } ;
101
+
102
+ //=========================================================//
103
+ //Public Method setters
104
+ //Purpose: Dynamically creates muator methods(setters)
105
+ //Parameters:
106
+ // scope: The scope in which the mutator methods will be
107
+ // applied
108
+ // prefix: Goes before the property. i.e. (set)Name
109
+ // camel: whether to induce camel case
110
+ // obj: The object that will have mutators
111
+ //Postcondition: scope has been altered to include mutator
112
+ //methods
113
+ //=========================================================//
114
+ GetSet . setters = function ( options )
115
+ {
116
+ //Over-ride default values
117
+ var defaults =
118
+ {
119
+ prefix : "set" ,
120
+ camel : true
121
+ } ;
122
+
123
+ //Override defaults values
124
+ GetSet . override ( options , defaults ) ;
125
+
126
+ //If prefix is set to 'none', force blank. A blank string as a parameter
127
+ //evaluates to null for some reason.
128
+ options . prefix = ( options . prefix === "none" ) ? "" : options . prefix ;
129
+
130
+ //Iterate through the properties of the object
131
+ var str ;
132
+ for ( var i in options . obj )
133
+ {
134
+ //If camel case is enabled and no blank prefix
135
+ if ( options . camel && options . prefix != "" )
136
+ {
137
+ str = i . charAt ( 0 ) . toUpperCase ( ) + i . substr ( 1 ) ;
138
+ }
139
+ else
140
+ {
141
+ str = i ;
142
+ }
143
+ ( function ( i )
144
+ {
145
+ // Dynamically create an accessor method
146
+ options . scope [ options . prefix + str ] = function ( val )
147
+ {
148
+ options . obj [ i ] = val ;
149
+ } ;
150
+ } ) ( i ) ;
151
+ }
152
+ } ;
0 commit comments