renum 0.1.0 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -1,4 +1,7 @@
1
1
  require 'config/requirements'
2
2
  require 'config/hoe' # setup Hoe + all gem configuration
3
3
 
4
- Dir['tasks/**/*.rake'].each { |rake| load rake }
4
+ Dir['tasks/**/*.rake'].each { |rake| load rake }
5
+
6
+ Rake::Task[:default].prerequisites.clear
7
+ task :default => :spec
@@ -2,14 +2,53 @@ require 'renum/enumerated_value'
2
2
 
3
3
  module Renum
4
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))
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
@@ -1,7 +1,7 @@
1
1
  module Renum #:nodoc:
2
2
  module VERSION #:nodoc:
3
- MAJOR = 0
4
- MINOR = 1
3
+ MAJOR = 1
4
+ MINOR = 0
5
5
  TINY = 0
6
6
 
7
7
  STRING = [MAJOR, MINOR, TINY].join('.')
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.rb')
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
- module MyNamespace
18
- enum :FooValue, [ :Bar, :Baz, :Bat ]
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
- it "allows enums to be nested in other modules or classes" do
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
@@ -4,4 +4,11 @@ rescue LoadError
4
4
  require 'rubygems'
5
5
  gem 'rspec'
6
6
  require 'spec'
7
- end
7
+ end
8
+
9
+ if ENV['USE_GEM']
10
+ require 'rubygems'
11
+ require 'renum'
12
+ else
13
+ require File.expand_path(File.dirname(__FILE__) + '/../lib/renum')
14
+ end
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">0.1.0</a>
36
+ <a href="http://rubyforge.org/projects/renum" class="numbers">1.0.0</a>
37
37
  </div>
38
- <h1>&#x2192; &#8216;renum&#8217;</h1>
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&#8217;s flexibility means there&#8217;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">[</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>
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">(&quot;</span><span class="string">Really really tiny</span><span class="punct">&quot;)</span>
57
+ <span class="constant">Medium</span><span class="punct">(&quot;</span><span class="string">Sort of in the middle</span><span class="punct">&quot;)</span>
58
+ <span class="constant">Large</span><span class="punct">(&quot;</span><span class="string">Quite big</span><span class="punct">&quot;)</span>
60
59
 
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>
62
- <span class="keyword">def </span><span class="method">abbr</span>
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>
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">&quot;</span><span class="string">enum</span><span class="punct">&quot;</span> <span class="keyword">do</span>
@@ -85,6 +85,10 @@
85
85
  <span class="ident">it</span> <span class="punct">&quot;</span><span class="string">exposes array of values</span><span class="punct">&quot;</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">&quot;</span><span class="string">provides an alternative means of declaring values where extra information can be provided for initialization</span><span class="punct">&quot;</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">&quot;</span><span class="string">Really really tiny</span><span class="punct">&quot;</span>
91
+ <span class="keyword">end</span>
88
92
 
89
93
  <span class="ident">it</span> <span class="punct">&quot;</span><span class="string">enumerates over values</span><span class="punct">&quot;</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">&quot;</span><span class="string">indexes values</span><span class="punct">&quot;</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">&quot;</span><span class="string">provides index lookup on values</span><span class="punct">&quot;</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">&quot;</span><span class="string">allows an associated block to define instance methods</span><span class="punct">&quot;</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">&quot;</span><span class="string">R</span><span class="punct">&quot;</span>
105
103
  <span class="keyword">end</span>
106
104
 
107
105
  <span class="ident">it</span> <span class="punct">&quot;</span><span class="string">provides a reasonable to_s for values</span><span class="punct">&quot;</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">&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>
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">&lt;</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">&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>
@@ -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>, 2nd November 2007<br>
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
- h1. &#x2192; 'renum'
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, [ :NOT_STARTED, :IN_PROGRESS, :COMPLETE ]
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
- enum :Color, [ :RED, :GREEN, :BLUE ] do
23
- def abbr
24
- name[0..0]
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
- Color::RED.should < Color::GREEN
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
@@ -1,5 +1,5 @@
1
1
  body {
2
- background-color: #E1D1F1;
2
+ background-color: #eee;
3
3
  font-family: "Georgia", sans-serif;
4
4
  font-size: 16px;
5
5
  line-height: 1.6em;
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: 0.1.0
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-11 00:00:00 -05:00
12
+ date: 2008-01-25 00:00:00 -05:00
13
13
  default_executable:
14
14
  dependencies: []
15
15