renum 0.0.1 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/Manifest.txt +1 -0
- data/lib/renum.rb +6 -7
- data/lib/renum/enumerated_value.rb +5 -0
- data/lib/renum/enumerated_value_type_factory.rb +15 -0
- data/lib/renum/version.rb +1 -1
- data/spec/renum_spec.rb +12 -0
- data/website/index.html +16 -8
- data/website/index.txt +15 -5
- metadata +2 -1
data/Manifest.txt
CHANGED
data/lib/renum.rb
CHANGED
@@ -1,15 +1,14 @@
|
|
1
1
|
$:.unshift File.dirname(__FILE__)
|
2
|
-
require 'renum/
|
2
|
+
require 'renum/enumerated_value_type_factory'
|
3
3
|
|
4
4
|
module Renum
|
5
5
|
def enum type_name, values, &block
|
6
|
-
|
7
|
-
|
8
|
-
klass.class_eval &block if block_given?
|
9
|
-
values.each do |name|
|
10
|
-
klass.const_set(name, klass.new(name))
|
11
|
-
end
|
6
|
+
nest = self.is_a?(Module) ? self : Object
|
7
|
+
EnumeratedValueTypeFactory.create(nest, type_name, values, &block)
|
12
8
|
end
|
13
9
|
end
|
14
10
|
|
15
11
|
extend Renum
|
12
|
+
Module.module_eval do
|
13
|
+
include Renum
|
14
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'renum/enumerated_value'
|
2
|
+
|
3
|
+
module Renum
|
4
|
+
module EnumeratedValueTypeFactory
|
5
|
+
def create nest, type_name, values, &block
|
6
|
+
klass = Class.new EnumeratedValue
|
7
|
+
nest.const_set(type_name, klass)
|
8
|
+
klass.class_eval &block if block_given?
|
9
|
+
values.each do |name|
|
10
|
+
klass.const_set(name, klass.new(name))
|
11
|
+
end
|
12
|
+
end
|
13
|
+
module_function :create
|
14
|
+
end
|
15
|
+
end
|
data/lib/renum/version.rb
CHANGED
data/spec/renum_spec.rb
CHANGED
@@ -9,6 +9,10 @@ enum :Color, [ :RED, :GREEN, :BLUE ] do
|
|
9
9
|
end
|
10
10
|
end
|
11
11
|
|
12
|
+
module MyNamespace
|
13
|
+
enum :FooValue, [ :Bar, :Baz, :Bat ]
|
14
|
+
end
|
15
|
+
|
12
16
|
describe "enum" do
|
13
17
|
|
14
18
|
it "creates a class for the value type" do
|
@@ -45,4 +49,12 @@ describe "enum" do
|
|
45
49
|
Status::NOT_STARTED.to_s.should == "Status::NOT_STARTED"
|
46
50
|
end
|
47
51
|
|
52
|
+
it "makes values comparable" do
|
53
|
+
Color::RED.should < Color::GREEN
|
54
|
+
end
|
55
|
+
|
56
|
+
it "allows enums to be nested in other modules or classes" do
|
57
|
+
MyNamespace::FooValue::Bar.class.should == MyNamespace::FooValue
|
58
|
+
end
|
59
|
+
|
48
60
|
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">0.0.
|
36
|
+
<a href="http://rubyforge.org/projects/renum" class="numbers">0.0.3</a>
|
37
37
|
</div>
|
38
38
|
<h1>→ ‘renum’</h1>
|
39
39
|
|
@@ -53,19 +53,19 @@
|
|
53
53
|
<h2>Demonstration of usage</h2>
|
54
54
|
|
55
55
|
|
56
|
-
<p>Renum allows you to do
|
56
|
+
<p>Renum allows you to do things like this:</p>
|
57
57
|
|
58
58
|
|
59
|
-
<p><pre class='syntax'><span class="ident">enum</span> <span class="symbol">:Status</span><span class="punct">,</span> <span class="punct">[</span> <span class="symbol">:NOT_STARTED</span><span class="punct">,</span> <span class="symbol">:IN_PROGRESS</span><span class="punct">,</span> <span class="symbol">:COMPLETE</span> <span class="punct">]</span
|
59
|
+
<p><pre class='syntax'><span class="ident">enum</span> <span class="symbol">:Status</span><span class="punct">,</span> <span class="punct">[</span> <span class="symbol">:NOT_STARTED</span><span class="punct">,</span> <span class="symbol">:IN_PROGRESS</span><span class="punct">,</span> <span class="symbol">:COMPLETE</span> <span class="punct">]</span>
|
60
60
|
|
61
|
-
|
62
|
-
<p>and this:</p>
|
63
|
-
|
64
|
-
|
65
|
-
<p><pre class='syntax'><span class="ident">enum</span> <span class="symbol">:Color</span><span class="punct">,</span> <span class="punct">[</span> <span class="symbol">:RED</span><span class="punct">,</span> <span class="symbol">:GREEN</span><span class="punct">,</span> <span class="symbol">:BLUE</span> <span class="punct">]</span> <span class="keyword">do</span>
|
61
|
+
<span class="ident">enum</span> <span class="symbol">:Color</span><span class="punct">,</span> <span class="punct">[</span> <span class="symbol">:RED</span><span class="punct">,</span> <span class="symbol">:GREEN</span><span class="punct">,</span> <span class="symbol">:BLUE</span> <span class="punct">]</span> <span class="keyword">do</span>
|
66
62
|
<span class="keyword">def </span><span class="method">abbr</span>
|
67
63
|
<span class="ident">name</span><span class="punct">[</span><span class="number">0</span><span class="punct">..</span><span class="number">0</span><span class="punct">]</span>
|
68
64
|
<span class="keyword">end</span>
|
65
|
+
<span class="keyword">end</span>
|
66
|
+
|
67
|
+
<span class="keyword">module </span><span class="module">MyNamespace</span>
|
68
|
+
<span class="ident">enum</span> <span class="symbol">:FooValue</span><span class="punct">,</span> <span class="punct">[</span> <span class="symbol">:Bar</span><span class="punct">,</span> <span class="symbol">:Baz</span><span class="punct">,</span> <span class="symbol">:Bat</span> <span class="punct">]</span>
|
69
69
|
<span class="keyword">end</span></pre></p>
|
70
70
|
|
71
71
|
|
@@ -108,6 +108,14 @@
|
|
108
108
|
<span class="constant">Status</span><span class="punct">::</span><span class="constant">NOT_STARTED</span><span class="punct">.</span><span class="ident">to_s</span><span class="punct">.</span><span class="ident">should</span> <span class="punct">==</span> <span class="punct">"</span><span class="string">Status::NOT_STARTED</span><span class="punct">"</span>
|
109
109
|
<span class="keyword">end</span>
|
110
110
|
|
111
|
+
<span class="ident">it</span> <span class="punct">"</span><span class="string">makes values comparable</span><span class="punct">"</span> <span class="keyword">do</span>
|
112
|
+
<span class="constant">Color</span><span class="punct">::</span><span class="constant">RED</span><span class="punct">.</span><span class="ident">should</span> <span class="punct"><</span> <span class="constant">Color</span><span class="punct">::</span><span class="constant">GREEN</span>
|
113
|
+
<span class="keyword">end</span>
|
114
|
+
|
115
|
+
<span class="ident">it</span> <span class="punct">"</span><span class="string">allows enums to be nested in other modules or classes</span><span class="punct">"</span> <span class="keyword">do</span>
|
116
|
+
<span class="constant">MyNamespace</span><span class="punct">::</span><span class="constant">FooValue</span><span class="punct">::</span><span class="constant">Bar</span><span class="punct">.</span><span class="ident">class</span><span class="punct">.</span><span class="ident">should</span> <span class="punct">==</span> <span class="constant">MyNamespace</span><span class="punct">::</span><span class="constant">FooValue</span>
|
117
|
+
<span class="keyword">end</span>
|
118
|
+
|
111
119
|
<span class="keyword">end</span></pre></p>
|
112
120
|
|
113
121
|
|
data/website/index.txt
CHANGED
@@ -15,16 +15,18 @@ h2. Installing
|
|
15
15
|
|
16
16
|
h2. Demonstration of usage
|
17
17
|
|
18
|
-
Renum allows you to do
|
18
|
+
Renum allows you to do things like this:
|
19
19
|
|
20
|
-
<pre syntax="ruby">enum :Status, [ :NOT_STARTED, :IN_PROGRESS, :COMPLETE ]
|
20
|
+
<pre syntax="ruby">enum :Status, [ :NOT_STARTED, :IN_PROGRESS, :COMPLETE ]
|
21
21
|
|
22
|
-
|
23
|
-
|
24
|
-
<pre syntax="ruby">enum :Color, [ :RED, :GREEN, :BLUE ] do
|
22
|
+
enum :Color, [ :RED, :GREEN, :BLUE ] do
|
25
23
|
def abbr
|
26
24
|
name[0..0]
|
27
25
|
end
|
26
|
+
end
|
27
|
+
|
28
|
+
module MyNamespace
|
29
|
+
enum :FooValue, [ :Bar, :Baz, :Bat ]
|
28
30
|
end</pre>
|
29
31
|
|
30
32
|
Giving you something that satisfies this spec:
|
@@ -65,6 +67,14 @@ Giving you something that satisfies this spec:
|
|
65
67
|
Status::NOT_STARTED.to_s.should == "Status::NOT_STARTED"
|
66
68
|
end
|
67
69
|
|
70
|
+
it "makes values comparable" do
|
71
|
+
Color::RED.should < Color::GREEN
|
72
|
+
end
|
73
|
+
|
74
|
+
it "allows enums to be nested in other modules or classes" do
|
75
|
+
MyNamespace::FooValue::Bar.class.should == MyNamespace::FooValue
|
76
|
+
end
|
77
|
+
|
68
78
|
end</pre>
|
69
79
|
|
70
80
|
h2. License
|
metadata
CHANGED
@@ -3,7 +3,7 @@ rubygems_version: 0.9.0
|
|
3
3
|
specification_version: 1
|
4
4
|
name: renum
|
5
5
|
version: !ruby/object:Gem::Version
|
6
|
-
version: 0.0.
|
6
|
+
version: 0.0.3
|
7
7
|
date: 2007-11-02 00:00:00 -04:00
|
8
8
|
summary: Renum is a Ruby enum implementation
|
9
9
|
require_paths:
|
@@ -38,6 +38,7 @@ files:
|
|
38
38
|
- config/requirements.rb
|
39
39
|
- lib/renum.rb
|
40
40
|
- lib/renum/enumerated_value.rb
|
41
|
+
- lib/renum/enumerated_value_type_factory.rb
|
41
42
|
- lib/renum/version.rb
|
42
43
|
- log/debug.log
|
43
44
|
- script/destroy
|