renum 1.0.2 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -115,6 +115,25 @@ h2. License
115
115
 
116
116
  This code is free to use under the terms of the MIT license.
117
117
 
118
+ Permission is hereby granted, free of charge, to any person obtaining
119
+ a copy of this software and associated documentation files (the
120
+ "Software"), to deal in the Software without restriction, including
121
+ without limitation the rights to use, copy, modify, merge, publish,
122
+ distribute, sublicense, and/or sell copies of the Software, and to
123
+ permit persons to whom the Software is furnished to do so, subject to
124
+ the following conditions:
125
+
126
+ The above copyright notice and this permission notice shall be
127
+ included in all copies or substantial portions of the Software.
128
+
129
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
130
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
131
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
132
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
133
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
134
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
135
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
136
+
118
137
  h2. Contact
119
138
 
120
139
  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/.
data/Rakefile CHANGED
@@ -1,7 +1,22 @@
1
- require 'config/requirements'
2
- require 'config/hoe' # setup Hoe + all gem configuration
1
+ require 'rubygems'
2
+ require 'spec/rake/spectask'
3
3
 
4
- Dir['tasks/**/*.rake'].each { |rake| load rake }
4
+ Spec::Rake::SpecTask.new do |t|
5
+ end
5
6
 
6
- Rake::Task[:default].prerequisites.clear
7
- task :default => :spec
7
+ task :default => :spec
8
+
9
+ begin
10
+ require 'jeweler'
11
+ Jeweler::Tasks.new do |s|
12
+ s.name = "renum"
13
+ s.summary = "provides a readable but terse enum facility for Ruby"
14
+ s.email = "duelin.markers@gmail.com"
15
+ s.homepage = "http://github.com/duelinmarkers/renum"
16
+ s.description = "provides a readable but terse enum facility for Ruby"
17
+ s.authors = ["John Hume"]
18
+ s.rubyforge_project = "renum"
19
+ end
20
+ rescue LoadError
21
+ puts "Jeweler or a dependency not available. To install: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
22
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 1.2.0
@@ -1,7 +1,14 @@
1
1
  $:.unshift File.dirname(__FILE__)
2
2
  require 'renum/enumerated_value_type_factory'
3
3
 
4
+ # Requiring 'renum' mixes the Renum module into both the main Object and
5
+ # Module, so it can be called from anywhere that you might reasonably
6
+ # define an enumeration with an implicit receiver.
4
7
  module Renum
8
+
9
+ # Figures out whether the new enumeration will live in Object or the
10
+ # receiving Module, then delegates to EnumeratedValueTypeFactory#create for
11
+ # all the real work.
5
12
  def enum type_name, values = :defined_in_block, &block
6
13
  nest = self.is_a?(Module) ? self : Object
7
14
  EnumeratedValueTypeFactory.create(nest, type_name, values, &block)
@@ -1,6 +1,11 @@
1
1
  require 'forwardable'
2
2
 
3
3
  module Renum
4
+
5
+ # This is the superclass of all enumeration classes.
6
+ # An enumeration class is Enumerable over its values and also delegates []
7
+ # to the values array.
8
+ # Values are also comparable, sorting into the order in which they're declared.
4
9
  class EnumeratedValue
5
10
 
6
11
  class << self
@@ -9,6 +14,7 @@ module Renum
9
14
 
10
15
  def_delegators :values, :each, :[]
11
16
 
17
+ # Returns an array of values in the order they're declared.
12
18
  def values
13
19
  @values ||= []
14
20
  end
@@ -25,10 +31,14 @@ module Renum
25
31
  self.class.values << self
26
32
  end
27
33
 
34
+ # Returns the fully qualified constant referring to this value.
35
+ # Don't override this if you're using Renum with the constantize_attribute
36
+ # plugin, which relies on this behavior.
28
37
  def to_s
29
38
  "#{self.class}::#{name}"
30
39
  end
31
40
 
41
+ # Sorts enumerated values into the order in which they're declared.
32
42
  def <=> other
33
43
  index <=> other.index
34
44
  end
@@ -18,10 +18,11 @@ module Renum
18
18
  setup_for_definition_in_block(klass) if values == :defined_in_block
19
19
  klass.class_eval &block if block_given?
20
20
  if values == :defined_in_block
21
- klass.block_defined_values.each do |value_name, init_args|
21
+ klass.block_defined_values.each do |value_name, init_args, instance_block|
22
22
  value = klass.new(value_name)
23
23
  klass.const_set(value_name, value)
24
- value.init *init_args if init_args.any?
24
+ value.instance_eval &instance_block if instance_block
25
+ value.init *init_args if value.respond_to? :init
25
26
  end
26
27
  teardown_from_definition_in_block(klass)
27
28
  else
@@ -37,8 +38,8 @@ module Renum
37
38
  @block_defined_values ||= []
38
39
  end
39
40
 
40
- def self.method_missing value_name, *init_args
41
- block_defined_values << [value_name, init_args]
41
+ def self.method_missing value_name, *init_args, &instance_block
42
+ block_defined_values << [value_name, init_args, instance_block]
42
43
  end
43
44
  end
44
45
  end
@@ -0,0 +1,47 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{renum}
5
+ s.version = "1.2.0"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["John Hume"]
9
+ s.date = %q{2009-09-01}
10
+ s.description = %q{provides a readable but terse enum facility for Ruby}
11
+ s.email = %q{duelin.markers@gmail.com}
12
+ s.extra_rdoc_files = [
13
+ "README.textile"
14
+ ]
15
+ s.files = [
16
+ "README.textile",
17
+ "Rakefile",
18
+ "VERSION",
19
+ "lib/renum.rb",
20
+ "lib/renum/enumerated_value.rb",
21
+ "lib/renum/enumerated_value_type_factory.rb",
22
+ "renum.gemspec",
23
+ "spec/renum_spec.rb",
24
+ "spec/spec_helper.rb"
25
+ ]
26
+ s.has_rdoc = true
27
+ s.homepage = %q{http://github.com/duelinmarkers/renum}
28
+ s.rdoc_options = ["--charset=UTF-8"]
29
+ s.require_paths = ["lib"]
30
+ s.rubyforge_project = %q{renum}
31
+ s.rubygems_version = %q{1.3.1}
32
+ s.summary = %q{provides a readable but terse enum facility for Ruby}
33
+ s.test_files = [
34
+ "spec/renum_spec.rb",
35
+ "spec/spec_helper.rb"
36
+ ]
37
+
38
+ if s.respond_to? :specification_version then
39
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
40
+ s.specification_version = 2
41
+
42
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
43
+ else
44
+ end
45
+ else
46
+ end
47
+ end
@@ -2,34 +2,6 @@ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
2
 
3
3
  enum :Status, [ :NOT_STARTED, :IN_PROGRESS, :COMPLETE ]
4
4
 
5
- module MyNamespace
6
- enum :FooValue, %w( Bar Baz Bat )
7
- end
8
-
9
- enum :Color, [ :RED, :GREEN, :BLUE ] do
10
- def abbr
11
- name[0..0]
12
- end
13
- end
14
-
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()
31
- end
32
-
33
5
  describe "basic enum" do
34
6
 
35
7
  it "creates a class for the value type" do
@@ -67,25 +39,54 @@ describe "basic enum" do
67
39
  end
68
40
  end
69
41
 
42
+ module MyNamespace
43
+ enum :FooValue, %w( Bar Baz Bat )
44
+ end
45
+
70
46
  describe "nested enum" do
71
47
  it "is namespaced in the containing module or class" do
72
48
  MyNamespace::FooValue::Bar.class.should == MyNamespace::FooValue
73
49
  end
74
50
  end
75
51
 
52
+ enum :Color, [ :RED, :GREEN, :BLUE ] do
53
+ def abbr
54
+ name[0..0]
55
+ end
56
+ end
57
+
76
58
  describe "enum with a block" do
77
59
  it "can define additional instance methods" do
78
60
  Color::RED.abbr.should == "R"
79
61
  end
80
62
  end
81
63
 
64
+ enum :Size do
65
+ Small("Really really tiny")
66
+ Medium("Sort of in the middle")
67
+ Large("Quite big")
68
+ Unknown()
69
+
70
+ attr_reader :description
71
+
72
+ def init description = nil
73
+ @description = description || "NO DESCRIPTION GIVEN"
74
+ end
75
+ end
76
+
77
+ enum :HairColor do
78
+ BLONDE()
79
+ BRUNETTE()
80
+ RED()
81
+ end
82
+
82
83
  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
+ it "provides another way to declare values where an init method can take extra params" do
84
85
  Size::Small.description.should == "Really really tiny"
85
86
  end
86
87
 
87
88
  it "works the same as the basic form with respect to ordering" do
88
- Size.values.should == [Size::Small, Size::Medium, Size::Large]
89
+ Size.values.should == [Size::Small, Size::Medium, Size::Large, Size::Unknown]
89
90
  end
90
91
 
91
92
  it "responds as expected to arbitrary method calls, in spite of using method_missing for value definition" do
@@ -95,14 +96,56 @@ describe "enum with no values array and values declared in the block" do
95
96
  it "supports there being no extra data and no init() method defined, if you don't need them" do
96
97
  HairColor::BLONDE.name.should == "BLONDE"
97
98
  end
99
+
100
+ it "calls the init method even if no arguments are provided" do
101
+ Size::Unknown.description.should == "NO DESCRIPTION GIVEN"
102
+ end
103
+ end
104
+
105
+ enum :Rating do
106
+ NotRated()
107
+
108
+ ThumbsDown do
109
+ def description
110
+ "real real bad"
111
+ end
112
+ end
113
+
114
+ ThumbsUp do
115
+ def description
116
+ "so so good"
117
+ end
118
+
119
+ def thumbs_up_only_method
120
+ "this method is only defined on ThumbsUp"
121
+ end
122
+ end
123
+
124
+ def description
125
+ raise NotImplementedError
126
+ end
98
127
  end
99
128
 
100
- # It was reported on my blog that <=> was causing segfaults.
101
- # I'd love to figure out why, but first I'd love to fix that.
102
- describe "digging into this segfault/illegal instruction issue, renum" do
129
+ describe "an enum with instance-specific method definitions" do
130
+ it "allows each instance to have its own behavior" do
131
+ Rating::ThumbsDown.description.should == "real real bad"
132
+ Rating::ThumbsUp.description.should == "so so good"
133
+ end
134
+
135
+ it "uses the implementation given at the top level if no alternate definition is given for an instance" do
136
+ lambda { Rating::NotRated.description }.should raise_error(NotImplementedError)
137
+ end
138
+
139
+ it "allows definition of a method on just one instance" do
140
+ Rating::ThumbsUp.thumbs_up_only_method.should == "this method is only defined on ThumbsUp"
141
+ lambda { Rating::NotRated.thumbs_up_only_method }.should raise_error(NoMethodError)
142
+ end
143
+ end
144
+
145
+ describe "<=> comparison issue that at one time was causing segfaults on MRI" do
103
146
  it "doesn't cause the ruby process to bomb!" do
104
147
  Color::RED.should < Color::GREEN
105
148
  Color::RED.should_not > Color::GREEN
106
149
  Color::RED.should < Color::BLUE
107
150
  end
108
- end
151
+ end
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.2
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - John Hume
@@ -9,57 +9,33 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-11-07 00:00:00 -05:00
12
+ date: 2009-09-01 00:00:00 -04:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
16
- description: Renum is a Ruby enum implementation
17
- email: duelin dot markers at gmail
16
+ description: provides a readable but terse enum facility for Ruby
17
+ email: duelin.markers@gmail.com
18
18
  executables: []
19
19
 
20
20
  extensions: []
21
21
 
22
22
  extra_rdoc_files:
23
- - History.txt
24
- - License.txt
25
- - Manifest.txt
26
- - README.txt
27
- - website/index.txt
23
+ - README.textile
28
24
  files:
29
- - History.txt
30
- - License.txt
31
- - Manifest.txt
32
- - README.txt
25
+ - README.textile
33
26
  - Rakefile
34
- - config/hoe.rb
35
- - config/requirements.rb
27
+ - VERSION
36
28
  - lib/renum.rb
37
29
  - lib/renum/enumerated_value.rb
38
30
  - lib/renum/enumerated_value_type_factory.rb
39
- - lib/renum/version.rb
40
- - log/debug.log
41
- - script/destroy
42
- - script/generate
43
- - script/txt2html
44
- - setup.rb
31
+ - renum.gemspec
45
32
  - spec/renum_spec.rb
46
- - spec/spec.opts
47
33
  - spec/spec_helper.rb
48
- - tasks/deployment.rake
49
- - tasks/environment.rake
50
- - tasks/rspec.rake
51
- - tasks/website.rake
52
- - website/index.html
53
- - website/index.txt
54
- - website/javascripts/rounded_corners_lite.inc.js
55
- - website/stylesheets/screen.css
56
- - website/template.rhtml
57
34
  has_rdoc: true
58
- homepage: http://renum.rubyforge.org
35
+ homepage: http://github.com/duelinmarkers/renum
59
36
  post_install_message:
60
37
  rdoc_options:
61
- - --main
62
- - README.txt
38
+ - --charset=UTF-8
63
39
  require_paths:
64
40
  - lib
65
41
  required_ruby_version: !ruby/object:Gem::Requirement
@@ -77,9 +53,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
77
53
  requirements: []
78
54
 
79
55
  rubyforge_project: renum
80
- rubygems_version: 1.2.0
56
+ rubygems_version: 1.3.1
81
57
  signing_key:
82
58
  specification_version: 2
83
- summary: Renum is a Ruby enum implementation
84
- test_files: []
85
-
59
+ summary: provides a readable but terse enum facility for Ruby
60
+ test_files:
61
+ - spec/renum_spec.rb
62
+ - spec/spec_helper.rb
@@ -1,23 +0,0 @@
1
- == 1.0.2 2008-11-07
2
-
3
- * Documentation updates regarding Rails integration
4
- * Tiny typo fix in specs
5
-
6
- == 1.0.0 2008-01-25
7
-
8
- * New fancy syntax supporting extra member variables in enums
9
-
10
- == 0.1.0 ???
11
-
12
- * Add in a missing require
13
-
14
- == 0.0.3 2007-11-02
15
-
16
- * Support nesting enums inside other modules.
17
- * Add Comparable
18
-
19
- == 0.0.1 2007-11-01
20
-
21
- * 1 major enhancement:
22
- * Initial release
23
-