renum 1.0.1 → 1.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +5 -0
- data/lib/renum/version.rb +1 -1
- data/script/destroy +0 -0
- data/script/generate +0 -0
- data/script/txt2html +0 -0
- data/spec/renum_spec.rb +1 -1
- data/website/index.html +42 -2
- data/website/index.txt +37 -1
- metadata +3 -3
data/History.txt
CHANGED
data/lib/renum/version.rb
CHANGED
data/script/destroy
CHANGED
File without changes
|
data/script/generate
CHANGED
File without changes
|
data/script/txt2html
CHANGED
File without changes
|
data/spec/renum_spec.rb
CHANGED
@@ -92,7 +92,7 @@ describe "enum with no values array and values declared in the block" do
|
|
92
92
|
lambda { Size.ExtraLarge() }.should raise_error(NoMethodError)
|
93
93
|
end
|
94
94
|
|
95
|
-
it "
|
95
|
+
it "supports there being no extra data and no init() method defined, if you don't need them" do
|
96
96
|
HairColor::BLONDE.name.should == "BLONDE"
|
97
97
|
end
|
98
98
|
end
|
data/website/index.html
CHANGED
@@ -33,7 +33,7 @@
|
|
33
33
|
<h1>renum</h1>
|
34
34
|
<div id="version" class="clickable" onclick='document.location = "http://rubyforge.org/projects/renum"; return false'>
|
35
35
|
<p>Get Version</p>
|
36
|
-
<a href="http://rubyforge.org/projects/renum" class="numbers">1.0.
|
36
|
+
<a href="http://rubyforge.org/projects/renum" class="numbers">1.0.2</a>
|
37
37
|
</div>
|
38
38
|
<p>Renum provides a readable but terse enum facility for Ruby. Enums are sometimes called object constants and are analogous to the type-safe enum pattern in Java, though obviously Ruby’s flexibility means there’s no such thing as type-safety.</p>
|
39
39
|
|
@@ -117,6 +117,46 @@
|
|
117
117
|
<span class="keyword">end</span></pre></p>
|
118
118
|
|
119
119
|
|
120
|
+
<h2><a href="http://www.rubyonrails.com/">Rails</a> Integration</h2>
|
121
|
+
|
122
|
+
|
123
|
+
<p>To use enumerated values as ActiveRecord attribute values, <a href="https://github.com/duelinmarkers/constantize_attribute/tree">use the constantize_attribute plugin</a> (also by me).</p>
|
124
|
+
|
125
|
+
|
126
|
+
<p><pre class='syntax'><span class="keyword">class </span><span class="class">Vehicle</span> <span class="punct"><</span> <span class="constant">ActiveRecord</span><span class="punct">::</span><span class="constant">Base</span>
|
127
|
+
<span class="ident">enum</span> <span class="symbol">:Status</span> <span class="keyword">do</span>
|
128
|
+
<span class="constant">New</span><span class="punct">()</span>
|
129
|
+
<span class="constant">Used</span><span class="punct">()</span>
|
130
|
+
<span class="constant">Salvage</span><span class="punct">(</span><span class="constant">true</span><span class="punct">)</span>
|
131
|
+
|
132
|
+
<span class="keyword">def </span><span class="method">init</span><span class="punct">(</span><span class="ident">warn</span> <span class="punct">=</span> <span class="constant">false</span><span class="punct">)</span>
|
133
|
+
<span class="attribute">@warn</span> <span class="punct">=</span> <span class="ident">warn</span>
|
134
|
+
<span class="keyword">end</span>
|
135
|
+
|
136
|
+
<span class="keyword">def </span><span class="method">requires_warning_buyer?</span>
|
137
|
+
<span class="attribute">@warn</span>
|
138
|
+
<span class="keyword">end</span>
|
139
|
+
<span class="keyword">end</span>
|
140
|
+
|
141
|
+
<span class="ident">constantize_attribute</span> <span class="symbol">:status</span>
|
142
|
+
|
143
|
+
<span class="keyword">end</span>
|
144
|
+
|
145
|
+
<span class="ident">v</span> <span class="punct">=</span> <span class="constant">Vehicle</span><span class="punct">.</span><span class="ident">create!</span> <span class="symbol">:status</span> <span class="punct">=></span> <span class="constant">Vehicle</span><span class="punct">::</span><span class="constant">Status</span><span class="punct">::</span><span class="constant">New</span>
|
146
|
+
<span class="comment"># Now the database has the string "Vehicle::Status::New",</span>
|
147
|
+
<span class="comment"># but your record object exposes the Status object:</span>
|
148
|
+
<span class="ident">v</span><span class="punct">.</span><span class="ident">status</span><span class="punct">.</span><span class="ident">requires_warning_buyer?</span> <span class="comment"># => false</span>
|
149
|
+
|
150
|
+
<span class="ident">v</span><span class="punct">.</span><span class="ident">update_attribute</span> <span class="symbol">:status</span><span class="punct">,</span> <span class="constant">Vehicle</span><span class="punct">::</span><span class="constant">Status</span><span class="punct">::</span><span class="constant">Salvage</span>
|
151
|
+
<span class="comment"># Now the database has the string "Vehicle::Status::Salvage".</span>
|
152
|
+
<span class="ident">v</span><span class="punct">.</span><span class="ident">status</span><span class="punct">.</span><span class="ident">requires_warning_buyer?</span> <span class="comment"># => true</span>
|
153
|
+
|
154
|
+
<span class="comment"># Since constantize_attribute also accepts strings, it's easy</span>
|
155
|
+
<span class="comment"># to use enumerated values with forms.</span>
|
156
|
+
<span class="ident">v</span><span class="punct">.</span><span class="ident">status</span> <span class="punct">=</span> <span class="punct">"</span><span class="string">Vehicle::Status::Used</span><span class="punct">"</span>
|
157
|
+
<span class="ident">v</span><span class="punct">.</span><span class="ident">status</span><span class="punct">.</span><span class="ident">requires_warning_buyer?</span> <span class="comment"># => false</span></pre></p>
|
158
|
+
|
159
|
+
|
120
160
|
<h2>License</h2>
|
121
161
|
|
122
162
|
|
@@ -128,7 +168,7 @@
|
|
128
168
|
|
129
169
|
<p>Renum was created by John D. Hume. Comments are welcome. Send an email to duelin dot markers at gmail or <a href="http://elhumidor.blogspot.com/">contact me via my blog</a>.</p>
|
130
170
|
<p class="coda">
|
131
|
-
<a href="http://elhumidor.blogspot.com/">John D. Hume</a>,
|
171
|
+
<a href="http://elhumidor.blogspot.com/">John D. Hume</a>, 7th November 2008<br>
|
132
172
|
Theme extended from <a href="http://rb2js.rubyforge.org/">Paul Battley</a>
|
133
173
|
</p>
|
134
174
|
</div>
|
data/website/index.txt
CHANGED
@@ -74,6 +74,43 @@ Giving you something that satisfies this spec, plus a bit more:
|
|
74
74
|
|
75
75
|
end</pre>
|
76
76
|
|
77
|
+
h2. "Rails":http://www.rubyonrails.com/ Integration
|
78
|
+
|
79
|
+
To use enumerated values as ActiveRecord attribute values, "use the constantize_attribute plugin":https://github.com/duelinmarkers/constantize_attribute/tree (also by me).
|
80
|
+
|
81
|
+
<pre syntax="ruby">class Vehicle < ActiveRecord::Base
|
82
|
+
enum :Status do
|
83
|
+
New()
|
84
|
+
Used()
|
85
|
+
Salvage(true)
|
86
|
+
|
87
|
+
def init(warn = false)
|
88
|
+
@warn = warn
|
89
|
+
end
|
90
|
+
|
91
|
+
def requires_warning_buyer?
|
92
|
+
@warn
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
constantize_attribute :status
|
97
|
+
|
98
|
+
end
|
99
|
+
|
100
|
+
v = Vehicle.create! :status => Vehicle::Status::New
|
101
|
+
# Now the database has the string "Vehicle::Status::New",
|
102
|
+
# but your record object exposes the Status object:
|
103
|
+
v.status.requires_warning_buyer? # => false
|
104
|
+
|
105
|
+
v.update_attribute :status, Vehicle::Status::Salvage
|
106
|
+
# Now the database has the string "Vehicle::Status::Salvage".
|
107
|
+
v.status.requires_warning_buyer? # => true
|
108
|
+
|
109
|
+
# Since constantize_attribute also accepts strings, it's easy
|
110
|
+
# to use enumerated values with forms.
|
111
|
+
v.status = "Vehicle::Status::Used"
|
112
|
+
v.status.requires_warning_buyer? # => false</pre>
|
113
|
+
|
77
114
|
h2. License
|
78
115
|
|
79
116
|
This code is free to use under the terms of the MIT license.
|
@@ -81,4 +118,3 @@ This code is free to use under the terms of the MIT license.
|
|
81
118
|
h2. Contact
|
82
119
|
|
83
120
|
Renum was created by John D. Hume. Comments are welcome. Send an email to duelin dot markers at gmail or "contact me via my blog":http://elhumidor.blogspot.com/.
|
84
|
-
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: renum
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- John Hume
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2008-
|
12
|
+
date: 2008-11-07 00:00:00 -05:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|
@@ -77,7 +77,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
77
77
|
requirements: []
|
78
78
|
|
79
79
|
rubyforge_project: renum
|
80
|
-
rubygems_version: 1.0
|
80
|
+
rubygems_version: 1.2.0
|
81
81
|
signing_key:
|
82
82
|
specification_version: 2
|
83
83
|
summary: Renum is a Ruby enum implementation
|