renum 1.0.1 → 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,8 @@
1
+ == 1.0.2 2008-11-07
2
+
3
+ * Documentation updates regarding Rails integration
4
+ * Tiny typo fix in specs
5
+
1
6
  == 1.0.0 2008-01-25
2
7
 
3
8
  * New fancy syntax supporting extra member variables in enums
@@ -2,7 +2,7 @@ module Renum #:nodoc:
2
2
  module VERSION #:nodoc:
3
3
  MAJOR = 1
4
4
  MINOR = 0
5
- TINY = 1
5
+ TINY = 2
6
6
 
7
7
  STRING = [MAJOR, MINOR, TINY].join('.')
8
8
  end
File without changes
File without changes
File without changes
@@ -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 "supprts there being no extra data and no init() method defined, if you don't need them" do
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
@@ -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.0</a>
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&#8217;s flexibility means there&#8217;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">&lt;</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">=&gt;</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 &quot;Vehicle::Status::New&quot;,</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"># =&gt; 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 &quot;Vehicle::Status::Salvage&quot;.</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"># =&gt; 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">&quot;</span><span class="string">Vehicle::Status::Used</span><span class="punct">&quot;</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"># =&gt; 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>, 25th January 2008<br>
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>
@@ -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.1
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-05-15 00:00:00 -04:00
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.1
80
+ rubygems_version: 1.2.0
81
81
  signing_key:
82
82
  specification_version: 2
83
83
  summary: Renum is a Ruby enum implementation