Pragmatic Snippets
Previously, I showed my top seven snippets. Let us use some of the snippets together to make a new component. To follow along, you should install the snippets.
Next, we will consider our output. We will design a form bean component for a user management form consisting of the following values:
- FullName
- Status
- Username
Create a new component called snippet.cfc. Paste the four attributes listed above three times in snippet.cfc . Your file should look like the one below:
2
3 Email
4 Fullname
5 Status
6 Username
7
8 Email
9 Fullname
10 Status
11 Username
12
13 Email
14 Fullname
15 Status
16 Username
17
18
19 </cfcomponent>
Begin at the last block. This will be as block of getters and setters. To be as efficient as possible, put your cursor at the beginning of Email, select the word, cut it, type getset and your snippet key combo. (Either CTRL+SHIFT+. or CTRL+J). When the prompt appears, paste the text Email, then type an s character in each box to select 'string'. Repeat this for each of the four attributes.
When complete, and with some judicious spacing, you end up with this:
2
3
4 Email
5 Fullname
6 Status
7 Username
8
9 Email
10 Fullname
11 Status
12 Username
13
14 <!--- Usage: GetEmail / SetEmail methods for Email value --->
15 <cffunction name="getEmail" access="public" output="false" returntype="string">
16 <cfreturn variables.instance.Email />
17 </cffunction>
18
19 <cffunction name="setEmail" access="public" output="false" returntype="void">
20 <cfargument name="Email" type="string" required="true" />
21 <cfset variables.instance.Email = arguments.Email />
22 </cffunction>
23
24 <!--- Usage: GetFullname / SetFullname methods for Fullname value --->
25 <cffunction name="getFullname" access="public" output="false" returntype="string">
26 <cfreturn variables.instance.Fullname />
27 </cffunction>
28
29 <cffunction name="setFullname" access="public" output="false" returntype="void">
30 <cfargument name="Fullname" type="string" required="true" />
31 <cfset variables.instance.Fullname = arguments.Fullname />
32 </cffunction>
33
34 <!--- Usage: GetStatus / SetStatus methods for Status value --->
35 <cffunction name="getStatus" access="public" output="false" returntype="string">
36 <cfreturn variables.instance.Status />
37 </cffunction>
38
39 <cffunction name="setStatus" access="public" output="false" returntype="void">
40 <cfargument name="Status" type="string" required="true" />
41 <cfset variables.instance.Status = arguments.Status />
42 </cffunction>
43
44 <!--- Usage: GetUsername / SetUsername methods for Username value --->
45 <cffunction name="getUsername" access="public" output="false" returntype="string">
46 <cfreturn variables.instance.Username />
47 </cffunction>
48
49 <cffunction name="setUsername" access="public" output="false" returntype="void">
50 <cfargument name="Username" type="string" required="true" />
51 <cfset variables.instance.Username = arguments.Username />
52 </cffunction>
53
54
55
56</cfcomponent>
2 Name: init
3 Access: public
4 Return Type: snippet
2 Required: False
2
3
4 <!--- Date: 7/9/2007 Usage: I perform initialization --->
5 <cffunction name="init" output="false" access="public" returntype="snippet" hint="I perform initialization for this component">
6 <cfargument name="Email" type="string" required="false" default="" />
7 <cfargument name="Fullname" type="string" required="false" default="" />
8 <cfargument name="Status" type="string" required="false" default="" />
9 <cfargument name="Username" type="string" required="false" default="" />
10
11 Email
12 Fullname
13 Status
14 Username
15
16 </cffunction>
17
18
19 <!--- Usage: GetEmail / SetEmail methods for Email value --->
20 <cffunction name="getEmail" access="public" output="false" returntype="string">
21 <cfreturn variables.instance.Email />
22 </cffunction>
23
24 <cffunction name="setEmail" access="public" output="false" returntype="void">
25 <cfargument name="Email" type="string" required="true" />
26 <cfset variables.instance.Email = arguments.Email />
27 </cffunction>
28
29 <!--- Usage: GetFullname / SetFullname methods for Fullname value --->
30 <cffunction name="getFullname" access="public" output="false" returntype="string">
31 <cfreturn variables.instance.Fullname />
32 </cffunction>
33
34 <cffunction name="setFullname" access="public" output="false" returntype="void">
35 <cfargument name="Fullname" type="string" required="true" />
36 <cfset variables.instance.Fullname = arguments.Fullname />
37 </cffunction>
38
39 <!--- Usage: GetStatus / SetStatus methods for Status value --->
40 <cffunction name="getStatus" access="public" output="false" returntype="string">
41 <cfreturn variables.instance.Status />
42 </cffunction>
43
44 <cffunction name="setStatus" access="public" output="false" returntype="void">
45 <cfargument name="Status" type="string" required="true" />
46 <cfset variables.instance.Status = arguments.Status />
47 </cffunction>
48
49 <!--- Usage: GetUsername / SetUsername methods for Username value --->
50 <cffunction name="getUsername" access="public" output="false" returntype="string">
51 <cfreturn variables.instance.Username />
52 </cffunction>
53
54 <cffunction name="setUsername" access="public" output="false" returntype="void">
55 <cfargument name="Username" type="string" required="true" />
56 <cfset variables.instance.Username = arguments.Username />
57 </cffunction>
58
59</cfcomponent>
We will finish off the component by adding the cfset tags that execute the setters. Paste this block 4 times:
2 <cfset setFullname( arguments.Fullname ) />
3 <cfset setStatus( arguments.Status ) />
4 <cfset setUsername( arguments.Username ) />
5
6 <cfreturn this />
Congratulations! Wasn't that fun? When complete, you should have a nice Form Bean ready to use. For posterity, the complete code looks like this:
2
3
4 <!--- Date: 7/9/2007 Usage: I perform initialization --->
5 <cffunction name="init" output="false" access="public" returntype="snippet" hint="I perform initialization for this component">
6 <cfargument name="Email" type="string" required="false" default="" />
7 <cfargument name="Fullname" type="string" required="false" default="" />
8 <cfargument name="Status" type="string" required="false" default="" />
9 <cfargument name="Username" type="string" required="false" default="" />
10
11 <cfset setEmail( arguments.Email ) />
12 <cfset setFullname( arguments.Fullname ) />
13 <cfset setStatus( arguments.Status ) />
14 <cfset setUsername( arguments.Username ) />
15
16 <cfreturn this />
17
18 </cffunction>
19
20
21 <!--- Usage: GetEmail / SetEmail methods for Email value --->
22 <cffunction name="getEmail" access="public" output="false" returntype="string">
23 <cfreturn variables.instance.Email />
24 </cffunction>
25
26 <cffunction name="setEmail" access="public" output="false" returntype="void">
27 <cfargument name="Email" type="string" required="true" />
28 <cfset variables.instance.Email = arguments.Email />
29 </cffunction>
30
31 <!--- Usage: GetFullname / SetFullname methods for Fullname value --->
32 <cffunction name="getFullname" access="public" output="false" returntype="string">
33 <cfreturn variables.instance.Fullname />
34 </cffunction>
35
36 <cffunction name="setFullname" access="public" output="false" returntype="void">
37 <cfargument name="Fullname" type="string" required="true" />
38 <cfset variables.instance.Fullname = arguments.Fullname />
39 </cffunction>
40
41 <!--- Usage: GetStatus / SetStatus methods for Status value --->
42 <cffunction name="getStatus" access="public" output="false" returntype="string">
43 <cfreturn variables.instance.Status />
44 </cffunction>
45
46 <cffunction name="setStatus" access="public" output="false" returntype="void">
47 <cfargument name="Status" type="string" required="true" />
48 <cfset variables.instance.Status = arguments.Status />
49 </cffunction>
50
51 <!--- Usage: GetUsername / SetUsername methods for Username value --->
52 <cffunction name="getUsername" access="public" output="false" returntype="string">
53 <cfreturn variables.instance.Username />
54 </cffunction>
55
56 <cffunction name="setUsername" access="public" output="false" returntype="void">
57 <cfargument name="Username" type="string" required="true" />
58 <cfset variables.instance.Username = arguments.Username />
59 </cffunction>
60
61
62
63</cfcomponent>
There are no comments for this entry.


@justin,
Thanks for pointing this out. I've updated the post.
Happy Snippeting!
DW
I know this post is old, but it is one of the top results for cfeclipse snippets searches in google.
<cfset setX() = arguments.x />
should be
<cfset setX(arguments.x) />