renum 0.0.1 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -7,6 +7,7 @@ config/hoe.rb
7
7
  config/requirements.rb
8
8
  lib/renum.rb
9
9
  lib/renum/enumerated_value.rb
10
+ lib/renum/enumerated_value_type_factory.rb
10
11
  lib/renum/version.rb
11
12
  log/debug.log
12
13
  script/destroy
@@ -1,15 +1,14 @@
1
1
  $:.unshift File.dirname(__FILE__)
2
- require 'renum/enumerated_value'
2
+ require 'renum/enumerated_value_type_factory'
3
3
 
4
4
  module Renum
5
5
  def enum type_name, values, &block
6
- klass = Class.new EnumeratedValue
7
- Object.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
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
@@ -13,6 +13,7 @@ module Renum
13
13
 
14
14
  end
15
15
 
16
+ include Comparable
16
17
  attr_reader :name
17
18
 
18
19
  def initialize name
@@ -28,5 +29,9 @@ module Renum
28
29
  self.class.values.index self
29
30
  end
30
31
 
32
+ def <=> other
33
+ index <=> other.index
34
+ end
35
+
31
36
  end
32
37
  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
@@ -2,7 +2,7 @@ module Renum #:nodoc:
2
2
  module VERSION #:nodoc:
3
3
  MAJOR = 0
4
4
  MINOR = 0
5
- TINY = 1
5
+ TINY = 3
6
6
 
7
7
  STRING = [MAJOR, MINOR, TINY].join('.')
8
8
  end
@@ -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
@@ -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.1</a>
36
+ <a href="http://rubyforge.org/projects/renum" class="numbers">0.0.3</a>
37
37
  </div>
38
38
  <h1>&#x2192; &#8216;renum&#8217;</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 something like this:</p>
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></pre></p>
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">&quot;</span><span class="string">Status::NOT_STARTED</span><span class="punct">&quot;</span>
109
109
  <span class="keyword">end</span>
110
110
 
111
+ <span class="ident">it</span> <span class="punct">&quot;</span><span class="string">makes values comparable</span><span class="punct">&quot;</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">&lt;</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">&quot;</span><span class="string">allows enums to be nested in other modules or classes</span><span class="punct">&quot;</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
 
@@ -15,16 +15,18 @@ h2. Installing
15
15
 
16
16
  h2. Demonstration of usage
17
17
 
18
- Renum allows you to do something like this:
18
+ Renum allows you to do things like this:
19
19
 
20
- <pre syntax="ruby">enum :Status, [ :NOT_STARTED, :IN_PROGRESS, :COMPLETE ]</pre>
20
+ <pre syntax="ruby">enum :Status, [ :NOT_STARTED, :IN_PROGRESS, :COMPLETE ]
21
21
 
22
- and this:
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.1
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