coded_attribute 0.0.3 → 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/Manifest +8 -29
- data/README +83 -7
- data/README.rdoc +89 -0
- data/Rakefile +4 -3
- data/coded_attribute.gemspec +12 -6
- data/lib/coded_attribute.rb +19 -23
- data/rdoc/classes/CodedAttribute.html +167 -0
- data/rdoc/created.rid +1 -3
- data/rdoc/files/README.html +212 -0
- data/rdoc/files/lib/coded_attribute_rb.html +107 -0
- data/rdoc/fr_class_index.html +27 -0
- data/rdoc/fr_file_index.html +28 -0
- data/rdoc/fr_method_index.html +27 -0
- data/rdoc/index.html +20 -54
- data/rdoc/rdoc-style.css +208 -0
- metadata +51 -36
- data/rdoc/CodedAttributes.html +0 -226
- data/rdoc/README.html +0 -96
- data/rdoc/images/brick.png +0 -0
- data/rdoc/images/brick_link.png +0 -0
- data/rdoc/images/bug.png +0 -0
- data/rdoc/images/bullet_black.png +0 -0
- data/rdoc/images/bullet_toggle_minus.png +0 -0
- data/rdoc/images/bullet_toggle_plus.png +0 -0
- data/rdoc/images/date.png +0 -0
- data/rdoc/images/find.png +0 -0
- data/rdoc/images/loadingAnimation.gif +0 -0
- data/rdoc/images/macFFBgHack.png +0 -0
- data/rdoc/images/package.png +0 -0
- data/rdoc/images/page_green.png +0 -0
- data/rdoc/images/page_white_text.png +0 -0
- data/rdoc/images/page_white_width.png +0 -0
- data/rdoc/images/plugin.png +0 -0
- data/rdoc/images/ruby.png +0 -0
- data/rdoc/images/tag_green.png +0 -0
- data/rdoc/images/wrench.png +0 -0
- data/rdoc/images/wrench_orange.png +0 -0
- data/rdoc/images/zoom.png +0 -0
- data/rdoc/js/darkfish.js +0 -116
- data/rdoc/js/jquery.js +0 -32
- data/rdoc/js/quicksearch.js +0 -114
- data/rdoc/js/thickbox-compressed.js +0 -10
- data/rdoc/lib/coded_attributes_rb.html +0 -55
- data/rdoc/rdoc.css +0 -706
data/rdoc/created.rid
CHANGED
@@ -0,0 +1,212 @@
|
|
1
|
+
<?xml version="1.0" encoding="iso-8859-1"?>
|
2
|
+
<!DOCTYPE html
|
3
|
+
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
|
4
|
+
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
5
|
+
|
6
|
+
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
|
7
|
+
<head>
|
8
|
+
<title>File: README</title>
|
9
|
+
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
|
10
|
+
<meta http-equiv="Content-Script-Type" content="text/javascript" />
|
11
|
+
<link rel="stylesheet" href=".././rdoc-style.css" type="text/css" media="screen" />
|
12
|
+
<script type="text/javascript">
|
13
|
+
// <![CDATA[
|
14
|
+
|
15
|
+
function popupCode( url ) {
|
16
|
+
window.open(url, "Code", "resizable=yes,scrollbars=yes,toolbar=no,status=no,height=150,width=400")
|
17
|
+
}
|
18
|
+
|
19
|
+
function toggleCode( id ) {
|
20
|
+
if ( document.getElementById )
|
21
|
+
elem = document.getElementById( id );
|
22
|
+
else if ( document.all )
|
23
|
+
elem = eval( "document.all." + id );
|
24
|
+
else
|
25
|
+
return false;
|
26
|
+
|
27
|
+
elemStyle = elem.style;
|
28
|
+
|
29
|
+
if ( elemStyle.display != "block" ) {
|
30
|
+
elemStyle.display = "block"
|
31
|
+
} else {
|
32
|
+
elemStyle.display = "none"
|
33
|
+
}
|
34
|
+
|
35
|
+
return true;
|
36
|
+
}
|
37
|
+
|
38
|
+
// Make codeblocks hidden by default
|
39
|
+
document.writeln( "<style type=\"text/css\">div.method-source-code { display: none }</style>" )
|
40
|
+
|
41
|
+
// ]]>
|
42
|
+
</script>
|
43
|
+
|
44
|
+
</head>
|
45
|
+
<body>
|
46
|
+
|
47
|
+
|
48
|
+
|
49
|
+
<div id="fileHeader">
|
50
|
+
<h1>README</h1>
|
51
|
+
<table class="header-table">
|
52
|
+
<tr class="top-aligned-row">
|
53
|
+
<td><strong>Path:</strong></td>
|
54
|
+
<td>README
|
55
|
+
</td>
|
56
|
+
</tr>
|
57
|
+
<tr class="top-aligned-row">
|
58
|
+
<td><strong>Last Update:</strong></td>
|
59
|
+
<td>Mon Nov 08 11:37:25 -0700 2010</td>
|
60
|
+
</tr>
|
61
|
+
</table>
|
62
|
+
</div>
|
63
|
+
<!-- banner header -->
|
64
|
+
|
65
|
+
<div id="bodyContent">
|
66
|
+
|
67
|
+
|
68
|
+
|
69
|
+
<div id="contextContent">
|
70
|
+
|
71
|
+
<div id="description">
|
72
|
+
<h1><a href="../classes/CodedAttribute.html">CodedAttribute</a></h1>
|
73
|
+
<p>
|
74
|
+
Two of my favorite MySQL datatypes are ENUMs and SETs. Unfortunately,
|
75
|
+
ActiveRecord doesn‘t appear to support these fields well, or provide
|
76
|
+
a good and simple alternative to the problem of coded values. In the past,
|
77
|
+
I used to write getters and setters that looked something like this to
|
78
|
+
solve my problem.
|
79
|
+
</p>
|
80
|
+
<pre>
|
81
|
+
class Article
|
82
|
+
STATUS_CODES => {
|
83
|
+
:deleted => 0, 0 => :deleted
|
84
|
+
:pending => 1, 1 => :pending,
|
85
|
+
:completed => 2, 2 => :completed,
|
86
|
+
:published => 3, 3 => :published
|
87
|
+
}
|
88
|
+
|
89
|
+
def status=(value)
|
90
|
+
value = value.to_sym if value.is_a?(String)
|
91
|
+
status_code = STATUS_CODES[value]
|
92
|
+
end
|
93
|
+
|
94
|
+
def status
|
95
|
+
STATUS_CODES[status_code]
|
96
|
+
end
|
97
|
+
end
|
98
|
+
</pre>
|
99
|
+
<p>
|
100
|
+
Doesn‘t that code look horrible? Wouldn‘t it be better if it
|
101
|
+
looked like this?!
|
102
|
+
</p>
|
103
|
+
<pre>
|
104
|
+
class Article
|
105
|
+
coded_attribute :status {
|
106
|
+
0 => :deleted
|
107
|
+
1 => :pending
|
108
|
+
2 => :completed
|
109
|
+
3 => :published
|
110
|
+
end
|
111
|
+
end
|
112
|
+
</pre>
|
113
|
+
<p>
|
114
|
+
Or, if we want to get even lazier!
|
115
|
+
</p>
|
116
|
+
<pre>
|
117
|
+
class MyClass
|
118
|
+
coded_attribute :status, [ :deleted, :active, :pending, :deleted ]
|
119
|
+
end
|
120
|
+
</pre>
|
121
|
+
<p>
|
122
|
+
If so, then you‘ve come to the right place.
|
123
|
+
</p>
|
124
|
+
<p>
|
125
|
+
This plugin makes doing all of the above super easy. And your code (and
|
126
|
+
other programmers) will love you for using it! Like seriously love you!
|
127
|
+
</p>
|
128
|
+
<h2>Coded Attributes</h2>
|
129
|
+
<p>
|
130
|
+
Lets get down to it. Suppose we have a car, and it can be any one of a
|
131
|
+
variety of colors. We can indicate this in the model with the following
|
132
|
+
code:
|
133
|
+
</p>
|
134
|
+
<pre>
|
135
|
+
class Car < ActiveRecord::Base
|
136
|
+
coded_attribute :color, [ :red, :orange, :yellow, :green, :blue, :indigo, :violet ]
|
137
|
+
end
|
138
|
+
</pre>
|
139
|
+
<p>
|
140
|
+
But we aren‘t finished just yet. We still need to create a column to
|
141
|
+
store our attribute data in. There are two ways to go about this.
|
142
|
+
</p>
|
143
|
+
<h3>Storing the Coded Value</h3>
|
144
|
+
<p>
|
145
|
+
The easiest way to store the coded value is to create an integer column in
|
146
|
+
the database by appending ‘_code’ to the name of the attribute.
|
147
|
+
So in our ‘cars’ table, we could create a
|
148
|
+
‘color_code’ integer field. If you do not want to name the
|
149
|
+
column in your database ‘color_code’, you can replace :color,
|
150
|
+
with a :method => :column pair, for example:
|
151
|
+
</p>
|
152
|
+
<pre>
|
153
|
+
class Car < ActiveRecord::Base
|
154
|
+
coded_attribute :color => :color_id, [ :red, orange, :yellow, :green, :blue, :indigo, :violet ]
|
155
|
+
end
|
156
|
+
</pre>
|
157
|
+
<h2>Coded Attribute Sets</h2>
|
158
|
+
<p>
|
159
|
+
If you do not want to use a SET datatype, coded_attribute_set an make use
|
160
|
+
of an integer bitmask to store the values.
|
161
|
+
</p>
|
162
|
+
<pre>
|
163
|
+
class Car < ActiveRecord::Base
|
164
|
+
coded_attribute_set :color, [ :red, :orange, :yellow, :green, :blue, :indigo, :violet ]
|
165
|
+
end
|
166
|
+
</pre>
|
167
|
+
<h1>Planned Features</h1>
|
168
|
+
<p>
|
169
|
+
Future versions should hopefully support auto-detecting and setup of enum
|
170
|
+
and set types (so you don‘t need to even define the coded_attribute,
|
171
|
+
it will be done automatically on all classes).
|
172
|
+
</p>
|
173
|
+
<p>
|
174
|
+
I also plan on a method added to Migrations that allow you to easily recode
|
175
|
+
data
|
176
|
+
</p>
|
177
|
+
<p>
|
178
|
+
Copyright (c) 2010 Jaden Carver, released under the MIT license
|
179
|
+
</p>
|
180
|
+
|
181
|
+
</div>
|
182
|
+
|
183
|
+
|
184
|
+
</div>
|
185
|
+
|
186
|
+
|
187
|
+
</div>
|
188
|
+
|
189
|
+
|
190
|
+
<!-- if includes -->
|
191
|
+
|
192
|
+
<div id="section">
|
193
|
+
|
194
|
+
|
195
|
+
|
196
|
+
|
197
|
+
|
198
|
+
|
199
|
+
|
200
|
+
|
201
|
+
<!-- if method_list -->
|
202
|
+
|
203
|
+
|
204
|
+
</div>
|
205
|
+
|
206
|
+
|
207
|
+
<div id="validator-badges">
|
208
|
+
<p><small><a href="http://validator.w3.org/check/referer">[Validate]</a></small></p>
|
209
|
+
</div>
|
210
|
+
|
211
|
+
</body>
|
212
|
+
</html>
|
@@ -0,0 +1,107 @@
|
|
1
|
+
<?xml version="1.0" encoding="iso-8859-1"?>
|
2
|
+
<!DOCTYPE html
|
3
|
+
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
|
4
|
+
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
5
|
+
|
6
|
+
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
|
7
|
+
<head>
|
8
|
+
<title>File: coded_attribute.rb</title>
|
9
|
+
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
|
10
|
+
<meta http-equiv="Content-Script-Type" content="text/javascript" />
|
11
|
+
<link rel="stylesheet" href="../.././rdoc-style.css" type="text/css" media="screen" />
|
12
|
+
<script type="text/javascript">
|
13
|
+
// <![CDATA[
|
14
|
+
|
15
|
+
function popupCode( url ) {
|
16
|
+
window.open(url, "Code", "resizable=yes,scrollbars=yes,toolbar=no,status=no,height=150,width=400")
|
17
|
+
}
|
18
|
+
|
19
|
+
function toggleCode( id ) {
|
20
|
+
if ( document.getElementById )
|
21
|
+
elem = document.getElementById( id );
|
22
|
+
else if ( document.all )
|
23
|
+
elem = eval( "document.all." + id );
|
24
|
+
else
|
25
|
+
return false;
|
26
|
+
|
27
|
+
elemStyle = elem.style;
|
28
|
+
|
29
|
+
if ( elemStyle.display != "block" ) {
|
30
|
+
elemStyle.display = "block"
|
31
|
+
} else {
|
32
|
+
elemStyle.display = "none"
|
33
|
+
}
|
34
|
+
|
35
|
+
return true;
|
36
|
+
}
|
37
|
+
|
38
|
+
// Make codeblocks hidden by default
|
39
|
+
document.writeln( "<style type=\"text/css\">div.method-source-code { display: none }</style>" )
|
40
|
+
|
41
|
+
// ]]>
|
42
|
+
</script>
|
43
|
+
|
44
|
+
</head>
|
45
|
+
<body>
|
46
|
+
|
47
|
+
|
48
|
+
|
49
|
+
<div id="fileHeader">
|
50
|
+
<h1>coded_attribute.rb</h1>
|
51
|
+
<table class="header-table">
|
52
|
+
<tr class="top-aligned-row">
|
53
|
+
<td><strong>Path:</strong></td>
|
54
|
+
<td>lib/coded_attribute.rb
|
55
|
+
</td>
|
56
|
+
</tr>
|
57
|
+
<tr class="top-aligned-row">
|
58
|
+
<td><strong>Last Update:</strong></td>
|
59
|
+
<td>Mon Nov 08 12:02:53 -0700 2010</td>
|
60
|
+
</tr>
|
61
|
+
</table>
|
62
|
+
</div>
|
63
|
+
<!-- banner header -->
|
64
|
+
|
65
|
+
<div id="bodyContent">
|
66
|
+
|
67
|
+
|
68
|
+
|
69
|
+
<div id="contextContent">
|
70
|
+
|
71
|
+
<div id="description">
|
72
|
+
<p>
|
73
|
+
CodedAttributes
|
74
|
+
</p>
|
75
|
+
|
76
|
+
</div>
|
77
|
+
|
78
|
+
|
79
|
+
</div>
|
80
|
+
|
81
|
+
|
82
|
+
</div>
|
83
|
+
|
84
|
+
|
85
|
+
<!-- if includes -->
|
86
|
+
|
87
|
+
<div id="section">
|
88
|
+
|
89
|
+
|
90
|
+
|
91
|
+
|
92
|
+
|
93
|
+
|
94
|
+
|
95
|
+
|
96
|
+
<!-- if method_list -->
|
97
|
+
|
98
|
+
|
99
|
+
</div>
|
100
|
+
|
101
|
+
|
102
|
+
<div id="validator-badges">
|
103
|
+
<p><small><a href="http://validator.w3.org/check/referer">[Validate]</a></small></p>
|
104
|
+
</div>
|
105
|
+
|
106
|
+
</body>
|
107
|
+
</html>
|
@@ -0,0 +1,27 @@
|
|
1
|
+
|
2
|
+
<?xml version="1.0" encoding="iso-8859-1"?>
|
3
|
+
<!DOCTYPE html
|
4
|
+
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
|
5
|
+
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
6
|
+
|
7
|
+
<!--
|
8
|
+
|
9
|
+
Classes
|
10
|
+
|
11
|
+
-->
|
12
|
+
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
|
13
|
+
<head>
|
14
|
+
<title>Classes</title>
|
15
|
+
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
|
16
|
+
<link rel="stylesheet" href="rdoc-style.css" type="text/css" />
|
17
|
+
<base target="docwin" />
|
18
|
+
</head>
|
19
|
+
<body>
|
20
|
+
<div id="index">
|
21
|
+
<h1 class="section-bar">Classes</h1>
|
22
|
+
<div id="index-entries">
|
23
|
+
<a href="classes/CodedAttribute.html">CodedAttribute</a><br />
|
24
|
+
</div>
|
25
|
+
</div>
|
26
|
+
</body>
|
27
|
+
</html>
|
@@ -0,0 +1,28 @@
|
|
1
|
+
|
2
|
+
<?xml version="1.0" encoding="iso-8859-1"?>
|
3
|
+
<!DOCTYPE html
|
4
|
+
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
|
5
|
+
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
6
|
+
|
7
|
+
<!--
|
8
|
+
|
9
|
+
Files
|
10
|
+
|
11
|
+
-->
|
12
|
+
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
|
13
|
+
<head>
|
14
|
+
<title>Files</title>
|
15
|
+
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
|
16
|
+
<link rel="stylesheet" href="rdoc-style.css" type="text/css" />
|
17
|
+
<base target="docwin" />
|
18
|
+
</head>
|
19
|
+
<body>
|
20
|
+
<div id="index">
|
21
|
+
<h1 class="section-bar">Files</h1>
|
22
|
+
<div id="index-entries">
|
23
|
+
<a href="files/README.html">README</a><br />
|
24
|
+
<a href="files/lib/coded_attribute_rb.html">lib/coded_attribute.rb</a><br />
|
25
|
+
</div>
|
26
|
+
</div>
|
27
|
+
</body>
|
28
|
+
</html>
|
@@ -0,0 +1,27 @@
|
|
1
|
+
|
2
|
+
<?xml version="1.0" encoding="iso-8859-1"?>
|
3
|
+
<!DOCTYPE html
|
4
|
+
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
|
5
|
+
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
6
|
+
|
7
|
+
<!--
|
8
|
+
|
9
|
+
Methods
|
10
|
+
|
11
|
+
-->
|
12
|
+
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
|
13
|
+
<head>
|
14
|
+
<title>Methods</title>
|
15
|
+
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
|
16
|
+
<link rel="stylesheet" href="rdoc-style.css" type="text/css" />
|
17
|
+
<base target="docwin" />
|
18
|
+
</head>
|
19
|
+
<body>
|
20
|
+
<div id="index">
|
21
|
+
<h1 class="section-bar">Methods</h1>
|
22
|
+
<div id="index-entries">
|
23
|
+
<a href="classes/CodedAttribute.html#M000001">coded_attribute (CodedAttribute)</a><br />
|
24
|
+
</div>
|
25
|
+
</div>
|
26
|
+
</body>
|
27
|
+
</html>
|
data/rdoc/index.html
CHANGED
@@ -1,58 +1,24 @@
|
|
1
|
-
<?xml version="1.0" encoding="
|
2
|
-
<!DOCTYPE html
|
3
|
-
|
1
|
+
<?xml version="1.0" encoding="iso-8859-1"?>
|
2
|
+
<!DOCTYPE html
|
3
|
+
PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN"
|
4
|
+
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">
|
4
5
|
|
5
|
-
|
6
|
-
<head>
|
7
|
-
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
|
8
|
-
|
9
|
-
<title>CodedAttributes</title>
|
10
|
-
|
11
|
-
<link type="text/css" media="screen" href="rdoc.css" rel="stylesheet" />
|
6
|
+
<!--
|
12
7
|
|
13
|
-
|
14
|
-
<script src="js/thickbox-compressed.js" type="text/javascript" charset="utf-8"></script>
|
15
|
-
<script src="js/quicksearch.js" type="text/javascript" charset="utf-8"></script>
|
16
|
-
<script src="js/darkfish.js" type="text/javascript" charset="utf-8"></script>
|
8
|
+
CodedAttributes
|
17
9
|
|
10
|
+
-->
|
11
|
+
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
|
12
|
+
<head>
|
13
|
+
<title>CodedAttributes</title>
|
14
|
+
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
|
18
15
|
</head>
|
19
|
-
<
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
<h2>Files</h2>
|
31
|
-
<ul>
|
32
|
-
|
33
|
-
<li class="file"><a href="README.html">README</a></li>
|
34
|
-
|
35
|
-
</ul>
|
36
|
-
|
37
|
-
|
38
|
-
<h2 id="classes">Classes/Modules</h2>
|
39
|
-
<ul>
|
40
|
-
|
41
|
-
<li class="module"><a href="CodedAttributes.html">CodedAttributes</a></li>
|
42
|
-
|
43
|
-
</ul>
|
44
|
-
|
45
|
-
<h2 id="methods">Methods</h2>
|
46
|
-
<ul>
|
47
|
-
|
48
|
-
<li><a href="CodedAttributes.html#method-i-coded_attribute">#coded_attribute — CodedAttributes</a></li>
|
49
|
-
|
50
|
-
</ul>
|
51
|
-
|
52
|
-
<div id="validator-badges">
|
53
|
-
<p><small><a href="http://validator.w3.org/check/referer">[Validate]</a></small></p>
|
54
|
-
<p><small>Generated with the <a href="http://deveiate.org/projects/Darkfish-Rdoc/">Darkfish
|
55
|
-
Rdoc Generator</a> 1.1.6</small>.</p>
|
56
|
-
</div>
|
57
|
-
</body>
|
58
|
-
</html>
|
16
|
+
<frameset rows="20%, 80%">
|
17
|
+
<frameset cols="25%,35%,45%">
|
18
|
+
<frame src="fr_file_index.html" title="Files" name="Files" />
|
19
|
+
<frame src="fr_class_index.html" name="Classes" />
|
20
|
+
<frame src="fr_method_index.html" name="Methods" />
|
21
|
+
</frameset>
|
22
|
+
<frame src="files/README.html" name="docwin" />
|
23
|
+
</frameset>
|
24
|
+
</html>
|