renum 0.1.0 → 1.0.0
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/Rakefile +4 -1
- data/lib/renum/enumerated_value_type_factory.rb +46 -7
- data/lib/renum/version.rb +2 -2
- data/lib/renum.rb +1 -1
- data/spec/renum_spec.rb +49 -16
- data/spec/spec_helper.rb +8 -1
- data/website/index.html +19 -21
- data/website/index.txt +17 -20
- data/website/stylesheets/screen.css +1 -1
- metadata +2 -2
data/Rakefile
CHANGED
@@ -2,14 +2,53 @@ require 'renum/enumerated_value'
|
|
2
2
|
|
3
3
|
module Renum
|
4
4
|
module EnumeratedValueTypeFactory
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
5
|
+
class << self
|
6
|
+
def create nest, type_name, values, &block
|
7
|
+
klass = create_class nest, type_name
|
8
|
+
create_values klass, values, &block
|
9
|
+
end
|
10
|
+
|
11
|
+
def create_class nest, type_name
|
12
|
+
klass = Class.new EnumeratedValue
|
13
|
+
nest.const_set(type_name, klass)
|
14
|
+
klass
|
15
|
+
end
|
16
|
+
|
17
|
+
def create_values klass, values, &block
|
18
|
+
setup_for_definition_in_block(klass) if values == :defined_in_block
|
19
|
+
klass.class_eval &block if block_given?
|
20
|
+
if values == :defined_in_block
|
21
|
+
klass.block_defined_values.each do |value_name, init_args|
|
22
|
+
value = klass.new(value_name)
|
23
|
+
klass.const_set(value_name, value)
|
24
|
+
value.init *init_args if init_args.any?
|
25
|
+
end
|
26
|
+
teardown_from_definition_in_block(klass)
|
27
|
+
else
|
28
|
+
values.each do |name|
|
29
|
+
klass.const_set(name, klass.new(name))
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def setup_for_definition_in_block klass
|
35
|
+
klass.class_eval do
|
36
|
+
def self.block_defined_values
|
37
|
+
@block_defined_values ||= []
|
38
|
+
end
|
39
|
+
|
40
|
+
def self.method_missing value_name, *init_args
|
41
|
+
block_defined_values << [value_name, init_args]
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def teardown_from_definition_in_block klass
|
47
|
+
class << klass
|
48
|
+
remove_method :block_defined_values
|
49
|
+
remove_method :method_missing
|
50
|
+
end
|
11
51
|
end
|
12
52
|
end
|
13
|
-
module_function :create
|
14
53
|
end
|
15
54
|
end
|
data/lib/renum/version.rb
CHANGED
data/lib/renum.rb
CHANGED
@@ -2,7 +2,7 @@ $:.unshift File.dirname(__FILE__)
|
|
2
2
|
require 'renum/enumerated_value_type_factory'
|
3
3
|
|
4
4
|
module Renum
|
5
|
-
def enum type_name, values, &block
|
5
|
+
def enum type_name, values = :defined_in_block, &block
|
6
6
|
nest = self.is_a?(Module) ? self : Object
|
7
7
|
EnumeratedValueTypeFactory.create(nest, type_name, values, &block)
|
8
8
|
end
|
data/spec/renum_spec.rb
CHANGED
@@ -1,24 +1,36 @@
|
|
1
|
-
require File.expand_path(File.dirname(__FILE__) + '/spec_helper
|
2
|
-
if ENV['USE_GEM']
|
3
|
-
require 'rubygems'
|
4
|
-
require 'renum'
|
5
|
-
else
|
6
|
-
require File.expand_path(File.dirname(__FILE__) + '/../lib/renum')
|
7
|
-
end
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
8
2
|
|
9
3
|
enum :Status, [ :NOT_STARTED, :IN_PROGRESS, :COMPLETE ]
|
10
4
|
|
5
|
+
module MyNamespace
|
6
|
+
enum :FooValue, %w( Bar Baz Bat )
|
7
|
+
end
|
8
|
+
|
11
9
|
enum :Color, [ :RED, :GREEN, :BLUE ] do
|
12
10
|
def abbr
|
13
11
|
name[0..0]
|
14
12
|
end
|
15
13
|
end
|
16
14
|
|
17
|
-
|
18
|
-
|
15
|
+
enum :Size do
|
16
|
+
Small("Really really tiny")
|
17
|
+
Medium("Sort of in the middle")
|
18
|
+
Large("Quite big")
|
19
|
+
|
20
|
+
attr_reader :description
|
21
|
+
|
22
|
+
def init description
|
23
|
+
@description = description
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
enum :HairColor do
|
28
|
+
BLONDE()
|
29
|
+
BRUNETTE()
|
30
|
+
RED()
|
19
31
|
end
|
20
32
|
|
21
|
-
describe "enum" do
|
33
|
+
describe "basic enum" do
|
22
34
|
|
23
35
|
it "creates a class for the value type" do
|
24
36
|
Status.should be_an_instance_of(Class)
|
@@ -46,10 +58,6 @@ describe "enum" do
|
|
46
58
|
Color::GREEN.index.should == 1
|
47
59
|
end
|
48
60
|
|
49
|
-
it "allows an associated block to define instance methods" do
|
50
|
-
Color::RED.abbr.should == "R"
|
51
|
-
end
|
52
|
-
|
53
61
|
it "provides a reasonable to_s for values" do
|
54
62
|
Status::NOT_STARTED.to_s.should == "Status::NOT_STARTED"
|
55
63
|
end
|
@@ -57,9 +65,34 @@ describe "enum" do
|
|
57
65
|
it "makes values comparable" do
|
58
66
|
Color::RED.should < Color::GREEN
|
59
67
|
end
|
60
|
-
|
61
|
-
|
68
|
+
end
|
69
|
+
|
70
|
+
describe "nested enum" do
|
71
|
+
it "is namespaced in the containing module or class" do
|
62
72
|
MyNamespace::FooValue::Bar.class.should == MyNamespace::FooValue
|
63
73
|
end
|
74
|
+
end
|
75
|
+
|
76
|
+
describe "enum with a block" do
|
77
|
+
it "can define additional instance methods" do
|
78
|
+
Color::RED.abbr.should == "R"
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
describe "enum with no values array and values declared in the block" do
|
83
|
+
it "provides an alternative means of declaring values where extra information can be provided for initialization" do
|
84
|
+
Size::Small.description.should == "Really really tiny"
|
85
|
+
end
|
86
|
+
|
87
|
+
it "works the same as the basic form with respect to ordering" do
|
88
|
+
Size.values.should == [Size::Small, Size::Medium, Size::Large]
|
89
|
+
end
|
64
90
|
|
91
|
+
it "responds as expected to arbitrary method calls, in spite of using method_missing for value definition" do
|
92
|
+
lambda { Size.ExtraLarge() }.should raise_error(NoMethodError)
|
93
|
+
end
|
94
|
+
|
95
|
+
it "supprts there being no extra data and no init() method defined, if you don't need them" do
|
96
|
+
HairColor::BLONDE.name.should == "BLONDE"
|
97
|
+
end
|
65
98
|
end
|
data/spec/spec_helper.rb
CHANGED
data/website/index.html
CHANGED
@@ -33,15 +33,9 @@
|
|
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">
|
36
|
+
<a href="http://rubyforge.org/projects/renum" class="numbers">1.0.0</a>
|
37
37
|
</div>
|
38
|
-
<
|
39
|
-
|
40
|
-
|
41
|
-
<h2>What</h2>
|
42
|
-
|
43
|
-
|
44
|
-
<p>Renum provides a readable but terse enum facility for Ruby.</p>
|
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>
|
45
39
|
|
46
40
|
|
47
41
|
<h2>Installing</h2>
|
@@ -56,11 +50,17 @@
|
|
56
50
|
<p>Renum allows you to do things like this:</p>
|
57
51
|
|
58
52
|
|
59
|
-
<p><pre class='syntax'><span class="ident">enum</span> <span class="symbol">:Status</span><span class="punct">,</span> <span class="punct"
|
53
|
+
<p><pre class='syntax'><span class="ident">enum</span> <span class="symbol">:Status</span><span class="punct">,</span> <span class="punct">%w(</span><span class="string"> NOT_STARTED IN_PROGRESS COMPLETE </span><span class="punct">)</span>
|
54
|
+
|
55
|
+
<span class="ident">enum</span> <span class="symbol">:Size</span> <span class="keyword">do</span>
|
56
|
+
<span class="constant">Small</span><span class="punct">("</span><span class="string">Really really tiny</span><span class="punct">")</span>
|
57
|
+
<span class="constant">Medium</span><span class="punct">("</span><span class="string">Sort of in the middle</span><span class="punct">")</span>
|
58
|
+
<span class="constant">Large</span><span class="punct">("</span><span class="string">Quite big</span><span class="punct">")</span>
|
60
59
|
|
61
|
-
<span class="ident">
|
62
|
-
|
63
|
-
|
60
|
+
<span class="ident">attr_reader</span> <span class="symbol">:description</span>
|
61
|
+
|
62
|
+
<span class="keyword">def </span><span class="method">init</span> <span class="ident">description</span>
|
63
|
+
<span class="attribute">@description</span> <span class="punct">=</span> <span class="ident">description</span>
|
64
64
|
<span class="keyword">end</span>
|
65
65
|
<span class="keyword">end</span>
|
66
66
|
|
@@ -69,7 +69,7 @@
|
|
69
69
|
<span class="keyword">end</span></pre></p>
|
70
70
|
|
71
71
|
|
72
|
-
<p>Giving you something that satisfies this spec:</p>
|
72
|
+
<p>Giving you something that satisfies this spec, plus a bit more:</p>
|
73
73
|
|
74
74
|
|
75
75
|
<p><pre class='syntax'><span class="ident">describe</span> <span class="punct">"</span><span class="string">enum</span><span class="punct">"</span> <span class="keyword">do</span>
|
@@ -85,6 +85,10 @@
|
|
85
85
|
<span class="ident">it</span> <span class="punct">"</span><span class="string">exposes array of values</span><span class="punct">"</span> <span class="keyword">do</span>
|
86
86
|
<span class="constant">Status</span><span class="punct">.</span><span class="ident">values</span><span class="punct">.</span><span class="ident">should</span> <span class="punct">==</span> <span class="punct">[</span><span class="constant">Status</span><span class="punct">::</span><span class="constant">NOT_STARTED</span><span class="punct">,</span> <span class="constant">Status</span><span class="punct">::</span><span class="constant">IN_PROGRESS</span><span class="punct">,</span> <span class="constant">Status</span><span class="punct">::</span><span class="constant">COMPLETE</span><span class="punct">]</span>
|
87
87
|
<span class="keyword">end</span>
|
88
|
+
|
89
|
+
<span class="ident">it</span> <span class="punct">"</span><span class="string">provides an alternative means of declaring values where extra information can be provided for initialization</span><span class="punct">"</span> <span class="keyword">do</span>
|
90
|
+
<span class="constant">Size</span><span class="punct">::</span><span class="constant">Small</span><span class="punct">.</span><span class="ident">description</span><span class="punct">.</span><span class="ident">should</span> <span class="punct">==</span> <span class="punct">"</span><span class="string">Really really tiny</span><span class="punct">"</span>
|
91
|
+
<span class="keyword">end</span>
|
88
92
|
|
89
93
|
<span class="ident">it</span> <span class="punct">"</span><span class="string">enumerates over values</span><span class="punct">"</span> <span class="keyword">do</span>
|
90
94
|
<span class="constant">Status</span><span class="punct">.</span><span class="ident">map</span> <span class="punct">{|</span><span class="ident">s</span><span class="punct">|</span> <span class="ident">s</span><span class="punct">.</span><span class="ident">name</span><span class="punct">}.</span><span class="ident">should</span> <span class="punct">==</span> <span class="punct">%w[</span><span class="string">NOT_STARTED IN_PROGRESS COMPLETE</span><span class="punct">]</span>
|
@@ -92,16 +96,10 @@
|
|
92
96
|
|
93
97
|
<span class="ident">it</span> <span class="punct">"</span><span class="string">indexes values</span><span class="punct">"</span> <span class="keyword">do</span>
|
94
98
|
<span class="constant">Status</span><span class="punct">[</span><span class="number">2</span><span class="punct">].</span><span class="ident">should</span> <span class="punct">==</span> <span class="constant">Status</span><span class="punct">::</span><span class="constant">COMPLETE</span>
|
95
|
-
<span class="constant">Color</span><span class="punct">[</span><span class="number">0</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">RED</span>
|
96
99
|
<span class="keyword">end</span>
|
97
100
|
|
98
101
|
<span class="ident">it</span> <span class="punct">"</span><span class="string">provides index lookup on values</span><span class="punct">"</span> <span class="keyword">do</span>
|
99
102
|
<span class="constant">Status</span><span class="punct">::</span><span class="constant">IN_PROGRESS</span><span class="punct">.</span><span class="ident">index</span><span class="punct">.</span><span class="ident">should</span> <span class="punct">==</span> <span class="number">1</span>
|
100
|
-
<span class="constant">Color</span><span class="punct">::</span><span class="constant">GREEN</span><span class="punct">.</span><span class="ident">index</span><span class="punct">.</span><span class="ident">should</span> <span class="punct">==</span> <span class="number">1</span>
|
101
|
-
<span class="keyword">end</span>
|
102
|
-
|
103
|
-
<span class="ident">it</span> <span class="punct">"</span><span class="string">allows an associated block to define instance methods</span><span class="punct">"</span> <span class="keyword">do</span>
|
104
|
-
<span class="constant">Color</span><span class="punct">::</span><span class="constant">RED</span><span class="punct">.</span><span class="ident">abbr</span><span class="punct">.</span><span class="ident">should</span> <span class="punct">==</span> <span class="punct">"</span><span class="string">R</span><span class="punct">"</span>
|
105
103
|
<span class="keyword">end</span>
|
106
104
|
|
107
105
|
<span class="ident">it</span> <span class="punct">"</span><span class="string">provides a reasonable to_s for values</span><span class="punct">"</span> <span class="keyword">do</span>
|
@@ -109,7 +107,7 @@
|
|
109
107
|
<span class="keyword">end</span>
|
110
108
|
|
111
109
|
<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">
|
110
|
+
<span class="constant">Status</span><span class="punct">::</span><span class="constant">NOT_STARTED</span><span class="punct">.</span><span class="ident">should</span> <span class="punct"><</span> <span class="constant">Status</span><span class="punct">::</span><span class="constant">COMPLETE</span>
|
113
111
|
<span class="keyword">end</span>
|
114
112
|
|
115
113
|
<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>
|
@@ -130,7 +128,7 @@
|
|
130
128
|
|
131
129
|
<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>
|
132
130
|
<p class="coda">
|
133
|
-
<a href="http://elhumidor.blogspot.com/">John D. Hume</a>,
|
131
|
+
<a href="http://elhumidor.blogspot.com/">John D. Hume</a>, 25th January 2008<br>
|
134
132
|
Theme extended from <a href="http://rb2js.rubyforge.org/">Paul Battley</a>
|
135
133
|
</p>
|
136
134
|
</div>
|
data/website/index.txt
CHANGED
@@ -1,27 +1,26 @@
|
|
1
1
|
h1. renum
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
h2. What
|
7
|
-
|
8
|
-
Renum provides a readable but terse enum facility for Ruby.
|
9
|
-
|
3
|
+
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.
|
10
4
|
|
11
5
|
h2. Installing
|
12
6
|
|
13
7
|
<pre syntax="ruby">sudo gem install renum</pre>
|
14
8
|
|
15
|
-
|
16
9
|
h2. Demonstration of usage
|
17
10
|
|
18
11
|
Renum allows you to do things like this:
|
19
12
|
|
20
|
-
<pre syntax="ruby">enum :Status,
|
13
|
+
<pre syntax="ruby">enum :Status, %w( NOT_STARTED IN_PROGRESS COMPLETE )
|
14
|
+
|
15
|
+
enum :Size do
|
16
|
+
Small("Really really tiny")
|
17
|
+
Medium("Sort of in the middle")
|
18
|
+
Large("Quite big")
|
21
19
|
|
22
|
-
|
23
|
-
|
24
|
-
|
20
|
+
attr_reader :description
|
21
|
+
|
22
|
+
def init description
|
23
|
+
@description = description
|
25
24
|
end
|
26
25
|
end
|
27
26
|
|
@@ -29,7 +28,7 @@ module MyNamespace
|
|
29
28
|
enum :FooValue, [ :Bar, :Baz, :Bat ]
|
30
29
|
end</pre>
|
31
30
|
|
32
|
-
Giving you something that satisfies this spec:
|
31
|
+
Giving you something that satisfies this spec, plus a bit more:
|
33
32
|
|
34
33
|
<pre syntax="ruby">describe "enum" do
|
35
34
|
|
@@ -44,6 +43,10 @@ Giving you something that satisfies this spec:
|
|
44
43
|
it "exposes array of values" do
|
45
44
|
Status.values.should == [Status::NOT_STARTED, Status::IN_PROGRESS, Status::COMPLETE]
|
46
45
|
end
|
46
|
+
|
47
|
+
it "provides an alternative means of declaring values where extra information can be provided for initialization" do
|
48
|
+
Size::Small.description.should == "Really really tiny"
|
49
|
+
end
|
47
50
|
|
48
51
|
it "enumerates over values" do
|
49
52
|
Status.map {|s| s.name}.should == %w[NOT_STARTED IN_PROGRESS COMPLETE]
|
@@ -51,16 +54,10 @@ Giving you something that satisfies this spec:
|
|
51
54
|
|
52
55
|
it "indexes values" do
|
53
56
|
Status[2].should == Status::COMPLETE
|
54
|
-
Color[0].should == Color::RED
|
55
57
|
end
|
56
58
|
|
57
59
|
it "provides index lookup on values" do
|
58
60
|
Status::IN_PROGRESS.index.should == 1
|
59
|
-
Color::GREEN.index.should == 1
|
60
|
-
end
|
61
|
-
|
62
|
-
it "allows an associated block to define instance methods" do
|
63
|
-
Color::RED.abbr.should == "R"
|
64
61
|
end
|
65
62
|
|
66
63
|
it "provides a reasonable to_s for values" do
|
@@ -68,7 +65,7 @@ Giving you something that satisfies this spec:
|
|
68
65
|
end
|
69
66
|
|
70
67
|
it "makes values comparable" do
|
71
|
-
|
68
|
+
Status::NOT_STARTED.should < Status::COMPLETE
|
72
69
|
end
|
73
70
|
|
74
71
|
it "allows enums to be nested in other modules or classes" do
|
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:
|
4
|
+
version: 1.0.0
|
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-01-
|
12
|
+
date: 2008-01-25 00:00:00 -05:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|