attr_enum 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,6 @@
1
+ Manifest
2
+ README.rdoc
3
+ Rakefile
4
+ attr_enum.gemspec
5
+ lib/attr_enum.rb
6
+ test/attr_enum_test.rb
@@ -0,0 +1,24 @@
1
+ = attr_enum
2
+
3
+ A class macro to define an enumeration variable. See http://en.wikipedia.org/wiki/Enumerated_type for more information.
4
+
5
+ == Usage
6
+
7
+ class Card
8
+ attr_enum :suit, %w( clubs hearts diamonds spades )
9
+ end
10
+
11
+ c = Card.new
12
+ c.suit = 'clubs'
13
+ c.suit
14
+ => 'clubs'
15
+ c.suit = 'jack'
16
+ => EnumeratedTypeError, Card#suit must be one of clubs, hearts, diamonds, spades.
17
+
18
+ == Installation
19
+
20
+ From rubyforge:
21
+ gem install attr_enum
22
+
23
+ From github:
24
+ gem install kmcd-attr_enum --source http://gems.github.com
@@ -0,0 +1,14 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'echoe'
4
+
5
+ task :default => [:test]
6
+
7
+ Echoe.new('attr_enum', '0.1.0') do |gem|
8
+ gem.description = 'Enumerated type macro for Ruby'
9
+ gem.url = 'http://github.com/kmcd/attr_enum'
10
+ gem.author = 'Keith McDonnell'
11
+ gem.email = 'keith@dancingtext.com'
12
+ gem.ignore_pattern = ['tmp/*', 'script/*']
13
+ gem.development_dependencies = []
14
+ end
@@ -0,0 +1,31 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{attr_enum}
5
+ s.version = "0.1.0"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Keith McDonnell"]
9
+ s.date = %q{2010-10-08}
10
+ s.description = %q{Enumerated type macro for Ruby}
11
+ s.email = %q{keith@dancingtext.com}
12
+ s.extra_rdoc_files = ["README.rdoc", "lib/attr_enum.rb"]
13
+ s.files = ["Manifest", "README.rdoc", "Rakefile", "attr_enum.gemspec", "lib/attr_enum.rb", "test/attr_enum_test.rb"]
14
+ s.homepage = %q{http://github.com/kmcd/attr_enum}
15
+ s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Attr_enum", "--main", "README.rdoc"]
16
+ s.require_paths = ["lib"]
17
+ s.rubyforge_project = %q{attr_enum}
18
+ s.rubygems_version = %q{1.3.7}
19
+ s.summary = %q{Enumerated type macro for Ruby}
20
+ s.test_files = ["test/attr_enum_test.rb"]
21
+
22
+ if s.respond_to? :specification_version then
23
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
24
+ s.specification_version = 3
25
+
26
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
27
+ else
28
+ end
29
+ else
30
+ end
31
+ end
@@ -0,0 +1,63 @@
1
+ # = attr_enum
2
+ # A macro to define an enumeration variable
3
+ # See http://en.wikipedia.org/wiki/Enumerated_type for more info
4
+ # = Usage
5
+ # require 'rubygems'
6
+ # require 'attr_enum'
7
+ #
8
+ # class Card
9
+ # attr_enum :suit, %w( clubs hearts diamonds spades )
10
+ # end
11
+ #
12
+ # c = Card.new
13
+ # c.suit = 'clubs'
14
+ # c.suit
15
+ # => 'clubs'
16
+ # m.suit = 'jack'
17
+ # => EnumeratedTypeError, Card.suit must be one of clubs, hearts, diamonds, spades.
18
+
19
+ class Module
20
+
21
+ # Declare an enumerated type which creates:
22
+ # * a class constant
23
+ # * a reader attribute
24
+ # * a writer with type constraint
25
+ # === Arguments
26
+ # +name+:: the name of the enumerated type, e.g :suit
27
+ # +types+:: an array of valid types, e.g. %w( clubs hearts diamonds spades )
28
+ # === Example
29
+ # class Card
30
+ # attr_enum :suit, %w( clubs hearts diamonds spades )
31
+ # end
32
+ # ==== Class constant
33
+ # Card::SUITS
34
+ # => ["clubs", "hearts", "diamonds", "spades"]
35
+ # ==== reader/writer attributes
36
+ # c = Card.new
37
+ # c.suit = 'clubs'
38
+ # c.suit
39
+ # => 'clubs'
40
+ # ==== Error handling:
41
+ # Card.new.suit = 'spades'
42
+ # => EnumeratedTypeError, Card.suit must be one of clubs, hearts, diamonds, spades.
43
+ def attr_enum(name, types)
44
+ attr_reader name
45
+
46
+ class_eval do
47
+ # Create a class constant
48
+ const_set :"#{name.to_s.upcase}_TYPES", types
49
+
50
+ define_method(:"#{name}=") do |type|
51
+ # Add type constraint to setter method
52
+ if self.class.const_get(:"#{name.to_s.upcase}_TYPES").include?(type)
53
+ instance_variable_set :"@#{name}", type
54
+ else
55
+ raise EnumeratedTypeError, "Invalid type; must be one of #{types.join(', ')}."
56
+ end
57
+ end
58
+ end
59
+ end
60
+ end
61
+
62
+ class EnumeratedTypeError < StandardError
63
+ end
@@ -0,0 +1,39 @@
1
+ require 'attr_enum'
2
+ require 'test/unit'
3
+
4
+ begin
5
+ require 'rubygems'
6
+ require 'redgreen'
7
+ rescue
8
+ end
9
+
10
+ class EnumeratedTypeTest < Test::Unit::TestCase
11
+ SUITS = %w( clubs hearts diamonds spades )
12
+ class Card
13
+ attr_enum :suit, SUITS
14
+ end
15
+
16
+ def setup
17
+ @card = Card.new
18
+ end
19
+
20
+ def test_should_have_getter
21
+ assert @card.suit.nil?
22
+ end
23
+
24
+ def test_should_have_class_constant
25
+ assert_equal SUITS, Card::SUIT_TYPES
26
+ end
27
+
28
+ def test_should_have_setter
29
+ Card::SUIT_TYPES.each do |suit|
30
+ @card.suit = suit
31
+ assert_equal suit, @card.suit
32
+ end
33
+ end
34
+
35
+ def test_should_have_type_constraint_on_setter
36
+ error = assert_raise(EnumeratedTypeError) { @card.suit = 'spadez' }
37
+ assert_match /Invalid type; must be one of #{SUITS.join ', ' }\./, error.message
38
+ end
39
+ end
metadata ADDED
@@ -0,0 +1,79 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: attr_enum
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Keith McDonnell
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-10-08 00:00:00 +01:00
19
+ default_executable:
20
+ dependencies: []
21
+
22
+ description: Enumerated type macro for Ruby
23
+ email: keith@dancingtext.com
24
+ executables: []
25
+
26
+ extensions: []
27
+
28
+ extra_rdoc_files:
29
+ - README.rdoc
30
+ - lib/attr_enum.rb
31
+ files:
32
+ - Manifest
33
+ - README.rdoc
34
+ - Rakefile
35
+ - attr_enum.gemspec
36
+ - lib/attr_enum.rb
37
+ - test/attr_enum_test.rb
38
+ has_rdoc: true
39
+ homepage: http://github.com/kmcd/attr_enum
40
+ licenses: []
41
+
42
+ post_install_message:
43
+ rdoc_options:
44
+ - --line-numbers
45
+ - --inline-source
46
+ - --title
47
+ - Attr_enum
48
+ - --main
49
+ - README.rdoc
50
+ require_paths:
51
+ - lib
52
+ required_ruby_version: !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ hash: 3
58
+ segments:
59
+ - 0
60
+ version: "0"
61
+ required_rubygems_version: !ruby/object:Gem::Requirement
62
+ none: false
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ hash: 11
67
+ segments:
68
+ - 1
69
+ - 2
70
+ version: "1.2"
71
+ requirements: []
72
+
73
+ rubyforge_project: attr_enum
74
+ rubygems_version: 1.3.7
75
+ signing_key:
76
+ specification_version: 3
77
+ summary: Enumerated type macro for Ruby
78
+ test_files:
79
+ - test/attr_enum_test.rb