nice_enum 0.1.2

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/LICENSE ADDED
@@ -0,0 +1,24 @@
1
+ Copyright (c) 2010, Raphael Robatsch
2
+ All rights reserved.
3
+
4
+ Redistribution and use in source and binary forms, with or without
5
+ modification, are permitted provided that the following conditions are met:
6
+ * Redistributions of source code must retain the above copyright
7
+ notice, this list of conditions and the following disclaimer.
8
+ * Redistributions in binary form must reproduce the above copyright
9
+ notice, this list of conditions and the following disclaimer in the
10
+ documentation and/or other materials provided with the distribution.
11
+ * The names of the developers or contributors must not be used to
12
+ endorse or promote products derived from this software without
13
+ specific prior written permission.
14
+
15
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16
+ ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17
+ WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18
+ DISCLAIMED. IN NO EVENT SHALL THE DEVELOPERS OR CONTRIBUTORS BE LIABLE FOR ANY
19
+ DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20
+ (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21
+ LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22
+ ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23
+ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24
+ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
data/README.rdoc ADDED
@@ -0,0 +1,198 @@
1
+ = Nice Enumerations for Ruby
2
+
3
+ Ruby's built-in enumeration support, or the lack thereof, is somewhere in the
4
+ C/C++-Age. An enum in Ruby is usually a class or module with various
5
+ constants set to some values. Attempting to display the enum variables will
6
+ result in their values being displayed, which have no use to the end user of
7
+ the application.
8
+
9
+ module Permissions
10
+ Read = 4
11
+ Write = 2
12
+ Execute = 1
13
+ end
14
+
15
+ permissions = Permissions::Read | Permissions::Write
16
+ puts permissions
17
+ # Will output "6" - Altough correct, it won't make any sense for a
18
+ # computer-illiterate user.
19
+
20
+ Nice-Enum solves this problem by encapsulating the enum values inside an
21
+ instance of their enclosing class and making the to_s method return the
22
+ name of the constant. The above code translates to
23
+
24
+ class Permissions < Flags
25
+ enum :Read, 4
26
+ enum :Write, 2
27
+ enum :Execute, 1
28
+ end
29
+
30
+ permissions = Permissions::Read | Permissions::Write
31
+ puts permissions.join(" and ")
32
+ # Will output "Write and Read".
33
+
34
+ Nice-Enum contains two classes: Enum and Flags. Instances of Flag will
35
+ automatically figure out it's components when or'd together and provides a
36
+ different to_s method. Otherwise the classes are functionally identical.
37
+
38
+ Of course, an enum can use anything as it's value type, not only integers.
39
+ The value type must only provide a <=> operator.
40
+
41
+ Up-To-Date documentation is avaiable {at rubydoc.info}[http://rubydoc.info/github/raphaelr/nice-enum/master/frames].
42
+
43
+ = Installation
44
+
45
+ Using Rubygems:
46
+
47
+ gem install nice-enum
48
+
49
+ Manually:
50
+
51
+ git clone git://github.com/raphaelr/nice-enum
52
+ cd nice-enum
53
+ rake install
54
+
55
+ == Usage
56
+
57
+ require "nice_enum"
58
+
59
+ = Features
60
+
61
+ == From Enums to Fixnums and back
62
+
63
+ Getting the underlying value from an enum instance is usually not neccessary
64
+ because the Enumeration class will hand method calls automatically down to
65
+ the underlying value:
66
+
67
+ puts Permissions::Execute.next # => 2; Will call 1.next
68
+
69
+ You can explicitly ask for the underlying value:
70
+
71
+ puts Permissions::Execute.value # => 1
72
+
73
+ The reverse has to be done explicitly, but it's very easy:
74
+
75
+ puts Permissions.new(1) # => Execute
76
+ puts Permissions.new(3) # => Execute | Write
77
+ puts Permissions.new(80) # => 80
78
+
79
+ In the last example, the Permission instance is treated as a Permission with
80
+ the name "80" and the value 80.
81
+
82
+ == Auto-Values
83
+
84
+ Like in most languages with enum support, you don't have to explicitly provide
85
+ values for enumerations. Nice-Enum will create sequential Fixnums if you don't
86
+ specify them.
87
+
88
+ class Suit < Enum
89
+ enum :Spades # Will become 0
90
+ enum :Hearts # Will become 1
91
+ enum :Diamonds # Will become 2
92
+ enum :Clubs # Will become 3
93
+ end
94
+
95
+ You can also mix explicit and implicit values together:
96
+
97
+ class Numbers < Enum
98
+ enum :Zero
99
+ enum :One
100
+ enum :FourtyEight, 48
101
+ enum :FourtyNine
102
+ end
103
+
104
+ Note that eventual gaps aren't filled.
105
+
106
+ == Iterating over values
107
+
108
+ Suit.each { |suit| puts suit }
109
+ # Outputs: Spades
110
+ # Hearts
111
+ # Diamonds
112
+ # Clubs
113
+
114
+ The Enum class also includes the Enumerable module so you can do stuff like:
115
+
116
+ Suit.map { |suit| suit.name.chop }.each { |suit| puts suit }
117
+ # Outputs: Spade
118
+ # Heart
119
+ # Diamond
120
+ # Club
121
+
122
+ == Attributes
123
+
124
+ If that's not nice enough for you, maybe this is: You can attach any number of
125
+ attributes to enumerations. For example:
126
+
127
+ class Number < Enum
128
+ enum :Zero, 0, :squared => 0
129
+ enum :One, 1, :note => "Average number of eyes per eyehole", :squared => 1
130
+ enum :Five, 5, :note => "Number of digits per hand", :squared => 25
131
+ end
132
+
133
+ Number.each do |enum|
134
+ puts "#{enum} is squared #{enum.squared} and is special because it is the #{enum.note}."
135
+ end
136
+ # Outputs: Zero is squared 0 and is special because it is the nil.
137
+ # One is squared 1 and is special because it is the Average number of eyes per eyehole.
138
+ # Five is squared 25 and is special because it is the Number of digits per hand.
139
+
140
+ You can also provide default values for attributes:
141
+
142
+ class Number < Enum
143
+ default :note => "number which is the same if multiplied with one"
144
+ # ...
145
+ end
146
+ # Outputs: Zero is squared 0 and is special because it is the number which is the same if multiplied with one.
147
+ # One is squared 1 and is special because it is the Average number of eyes per eyehole.
148
+ # Five is squared 25 and is special because it is the Number of digits per hand.
149
+
150
+ == Instance methods
151
+
152
+ Since enum values are instances of their enclosing class, you can add methods to them:
153
+
154
+ class Number < Enum
155
+ enum :Zero, 0
156
+ enum :One, 1
157
+ enum :Two, 2
158
+
159
+ def square
160
+ value ** 2
161
+ end
162
+ end
163
+
164
+ numbers = Number.to_a
165
+ numbers << Number.new(3)
166
+
167
+ numbers.each { |number| puts "#{number} squared is #{number.square}." }
168
+ # Outputs: Zero squared is 0.
169
+ # One squared is 1.
170
+ # Two squared is 4.
171
+ # 3 squared is 9.
172
+
173
+ = License
174
+
175
+ Copyright (c) 2010, Raphael Robatsch
176
+ All rights reserved.
177
+
178
+ Redistribution and use in source and binary forms, with or without
179
+ modification, are permitted provided that the following conditions are met:
180
+ * Redistributions of source code must retain the above copyright
181
+ notice, this list of conditions and the following disclaimer.
182
+ * Redistributions in binary form must reproduce the above copyright
183
+ notice, this list of conditions and the following disclaimer in the
184
+ documentation and/or other materials provided with the distribution.
185
+ * The names of the developers or contributors must not be used to
186
+ endorse or promote products derived from this software without
187
+ specific prior written permission.
188
+
189
+ THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS AND CONTRIBUTORS "AS IS" AND
190
+ ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
191
+ WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
192
+ DISCLAIMED. IN NO EVENT SHALL THE DEVELOPERS OR CONTRIBUTORS BE LIABLE FOR ANY
193
+ DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
194
+ (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
195
+ LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
196
+ ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
197
+ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
198
+ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
data/lib/nice_enum.rb ADDED
@@ -0,0 +1,167 @@
1
+ #--
2
+ # Copyright (c) 2010, Raphael Robatsch
3
+ # All rights reserved.
4
+ #
5
+ # Redistribution and use in source and binary forms, with or without
6
+ # modification, are permitted provided that the following conditions are met:
7
+ # * Redistributions of source code must retain the above copyright
8
+ # notice, this list of conditions and the following disclaimer.
9
+ # * Redistributions in binary form must reproduce the above copyright
10
+ # notice, this list of conditions and the following disclaimer in the
11
+ # documentation and/or other materials provided with the distribution.
12
+ # * The names of the developers or contributors must not be used to
13
+ # endorse or promote products derived from this software without
14
+ # specific prior written permission.
15
+ #
16
+ # THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS AND CONTRIBUTORS "AS IS" AND
17
+ # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18
+ # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19
+ # DISCLAIMED. IN NO EVENT SHALL THE DEVELOPERS OR CONTRIBUTORS BE LIABLE FOR ANY
20
+ # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21
+ # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22
+ # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23
+ # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24
+ # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25
+ # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
+ #++
27
+
28
+ # Base class for all Enumerations. To create an Enumeration, subclass this
29
+ # class and add enum calls in the class body.
30
+ # There are many samples in README.rdoc and samples/ .
31
+ class Enum
32
+ include Comparable
33
+
34
+ # Returns the name of the enumeration member. This is the first parameter
35
+ # passed to +enum+, converted to a string.
36
+ attr_reader :name
37
+
38
+ # Returns the value of the enumeration member. This is the second parameter
39
+ # passed to +enum+.
40
+ attr_reader :value
41
+
42
+ # Compares the value of +self+ with the value of +other+.
43
+ def <=>(other)
44
+ other = other.value if other.is_a? self.class
45
+ return value <=> other
46
+ end
47
+
48
+ # Returns the hashcode of +value+.
49
+ def hash
50
+ value.hash
51
+ end
52
+
53
+ # Returns the name of the enumeration member.
54
+ def to_s
55
+ @name
56
+ end
57
+
58
+ alias old_respond_to? respond_to? # :nodoc:
59
+ def respond_to?(sym) # :nodoc:
60
+ old_respond_to?(sym) || @value.respond_to?(sym)
61
+ end
62
+
63
+ def method_missing(sym, *args, &block) # :nodoc:
64
+ @value.send(sym, *args, &block)
65
+ end
66
+
67
+ class << Enum
68
+ include Enumerable
69
+
70
+ # Returns the +Enum+ instance for the given value. If no enumeration
71
+ # member has been created for this value, a new instance is created,
72
+ # setting +name+ to +value.to_s+.
73
+ def new(value)
74
+ return @enumvalues[value] if @enumvalues.has_key? value
75
+ _setup_enumvalue(value, value, allocate)
76
+ end
77
+
78
+ # Returns the default value for the attribute +attr+. If no default
79
+ # value has been set, +nil+ is returned.
80
+ def default_value(attr)
81
+ @defaults ||= {}
82
+ @defaults[attr.to_sym]
83
+ end
84
+
85
+ # Enumerates all known enumeration members.
86
+ def each
87
+ @enumvalues ||= {}
88
+ @enumvalues.each_value { |enumvalue| yield enumvalue }
89
+ end
90
+
91
+ protected
92
+ # Adds a new enumeration member to the current enumeration. The
93
+ # generated member is accessible using the constant +name+. If
94
+ # +value+ is +nil+, it is set to the highest integer value in
95
+ # use \+ 1.
96
+ def enum(name, value = nil, attributes = {})
97
+ @enumvalues ||= {}
98
+ value ||= (@enumvalues.values.max || -1) + 1
99
+
100
+ enumvalue = allocate
101
+ const_set(name, enumvalue)
102
+ @enumvalues[value] = enumvalue
103
+ _setup_enumvalue(name, value, enumvalue, attributes)
104
+ end
105
+
106
+ def _setup_enumvalue(name, value, enumvalue, attributes = {}) # :nodoc:
107
+ enumvalue.instance_variable_set(:@name, name.to_s)
108
+ enumvalue.instance_variable_set(:@value, value)
109
+ attributes.each do |attribute, attr_value|
110
+ _create_accessor(attribute)
111
+ enumvalue.instance_variable_set("@#{attribute}", attr_value)
112
+ end
113
+ enumvalue
114
+ end
115
+
116
+ # Sets the default values for the attributes of the enumeration members.
117
+ # There can be more than one call to +defaults+.
118
+ def defaults(defs = {})
119
+ @defaults ||= {}
120
+ @defaults.merge!(defs)
121
+ end
122
+ alias default defaults
123
+
124
+ def _create_accessor(attribute) # :nodoc:
125
+ define_method(attribute) do
126
+ instance_variable_get("@#{attribute}") || self.class.default_value(attribute)
127
+ end
128
+ end
129
+ end
130
+ end
131
+
132
+ # Base class for enumerations in which members can be or'd together to create
133
+ # combinations of them. For example, Unix file permissions can be represented
134
+ # as a flag enumeration with the members <tt>Read = 4</tt>, <tt>Write = 2</tt>
135
+ # and <tt>Execute = 1</tt>. There are many samples in README.rdoc and samples/.
136
+ class Flags < Enum
137
+ # Returns the individual enumeration members that are contained in +self+.
138
+ # If the enumeration class contains a member with the value +0+, that
139
+ # member is only included in the result if there aren't any other members
140
+ # that are contained in +self+.
141
+ def flags
142
+ result = self.class.select { |flag| value & flag != 0 }.sort
143
+ result = [self.class.new(0)] if result.empty?
144
+ result
145
+ end
146
+
147
+ # Joins the result of +flags+ together.
148
+ def join(seperator = " | ")
149
+ flags.map { |flag| flag.name }.join(seperator)
150
+ end
151
+ alias to_s join
152
+
153
+ # Returns the combination of +self+ and +other+.
154
+ def |(other)
155
+ self.class.new(value | other)
156
+ end
157
+
158
+ # Returns the intersection of +self+ and +other+.
159
+ def &(other)
160
+ self.class.new(value & other)
161
+ end
162
+
163
+ # Bitwise-XORs +self+ with +other+.
164
+ def ^(other)
165
+ self.class.new(value ^ other)
166
+ end
167
+ end
data/samples/deck.rb ADDED
@@ -0,0 +1,59 @@
1
+ require "../lib/nice_enum"
2
+
3
+ module Cards
4
+ class Suit < Enum
5
+ enum :Spades
6
+ enum :Hearts
7
+ enum :Diamonds
8
+ enum :Clubs
9
+ end
10
+
11
+ class Value < Enum
12
+ enum :Ace, 1, :score => 11 # For the sake of simplicity
13
+ enum :Two, 2, :score => 2
14
+ enum :Three, 3, :score => 3
15
+ enum :Four, nil, :score => 4
16
+ enum :Five, nil, :score => 5
17
+ enum :Six, nil, :score => 6
18
+ enum :Jack, nil, :score => 10
19
+ enum :Queen, nil, :score => 10
20
+ enum :Knight, nil, :score => 10
21
+ end
22
+
23
+ class Card
24
+ attr_accessor :suit
25
+ attr_accessor :value
26
+
27
+ def initialize(suit, value)
28
+ @suit = suit
29
+ @value = value
30
+ end
31
+
32
+ def to_s
33
+ "#{value} of #{suit}"
34
+ end
35
+ end
36
+
37
+ class Deck
38
+ def initialize
39
+ @cards = Suit.map do |suit|
40
+ Value.map do |value|
41
+ Card.new(suit, value)
42
+ end
43
+ end.flatten.shuffle
44
+ end
45
+
46
+ def draw(n)
47
+ @cards.slice!(0, n)
48
+ end
49
+ end
50
+ end
51
+
52
+ deck = Cards::Deck.new
53
+ hand = deck.draw(5)
54
+
55
+ puts "Your hand:"
56
+ hand.each do |card|
57
+ puts " - #{card}"
58
+ end
59
+ puts "Blackjack score: #{hand.inject(0) { |score, card| score + card.value.score }}"
@@ -0,0 +1,20 @@
1
+ require "../lib/nice_enum"
2
+
3
+ class Permission < Flags
4
+ enum :None, 0
5
+ enum :Read, 4
6
+ enum :Write, 2
7
+ enum :Execute, 1
8
+ end
9
+
10
+ file = ARGV[0] || __FILE__
11
+ puts "Permissions on #{file}:"
12
+
13
+ mode = File.stat(file).mode
14
+ user = Permission.new((mode & 448) >> 6)
15
+ group = Permission.new((mode & 56) >> 3)
16
+ world = Permission.new(mode & 7)
17
+
18
+ puts "User: #{user}"
19
+ puts "Group: #{group}"
20
+ puts "World: #{world}"
@@ -0,0 +1,16 @@
1
+ require "../lib/nice_enum"
2
+
3
+ class Number < Enum
4
+ enum :Zero, 0
5
+ enum :One, 1
6
+ enum :Two, 2
7
+
8
+ def square
9
+ value ** 2
10
+ end
11
+ end
12
+
13
+ numbers = Number.to_a
14
+ numbers << Number.new(3)
15
+
16
+ numbers.each { |number| puts "#{number} squared is #{number.square}." }
@@ -0,0 +1,30 @@
1
+ require "nice_enum"
2
+ require "test/unit"
3
+
4
+ class AttributeTest < Test::Unit::TestCase
5
+ class Number < Enum
6
+ enum :Twenty, 20,
7
+ :note => "Exact value of e^pi - pi"
8
+
9
+ enum :Ninety, 90,
10
+ :note => "Square Root of -1"
11
+
12
+ enum :FourtyTwo, 42
13
+ end
14
+
15
+ def test_respond_to_known_attribute
16
+ assert Number::Twenty.respond_to?(:note)
17
+ end
18
+
19
+ def test_get_attribute_from_known_value
20
+ assert_equal "Exact value of e^pi - pi", Number::Twenty.note
21
+ end
22
+
23
+ def test_get_attribute_from_unknown_value
24
+ assert_equal nil, Number.new(8).note
25
+ end
26
+
27
+ def test_respond_to_unknown_attribute
28
+ assert !Number::Twenty.respond_to?(:color)
29
+ end
30
+ end
@@ -0,0 +1,47 @@
1
+ require "nice_enum"
2
+ require "test/unit"
3
+
4
+ class ConstTest < Test::Unit::TestCase
5
+ class Number < Enum
6
+ enum :Zero, 0
7
+ enum :One, 1
8
+ enum :Two, 2
9
+ end
10
+
11
+ def test_to_s
12
+ assert_equal "Zero", Number::Zero.to_s
13
+ end
14
+
15
+ def test_value_equality
16
+ assert_equal 2, Number::Two.value
17
+ end
18
+
19
+ def test_enum_equality
20
+ assert_equal Number::Zero, Number::Zero
21
+ end
22
+
23
+ def test_mixed_equality
24
+ assert_equal Number::Zero, 0
25
+ end
26
+
27
+ def test_new_stays_same
28
+ assert_same Number::Zero, Number.new(0)
29
+ end
30
+
31
+ def test_new_on_undefined_enum_value
32
+ assert_equal -13, Number.new(-13)
33
+ assert_equal "-13", Number.new(-13).to_s
34
+ end
35
+
36
+ def test_respond_to_mirroring
37
+ assert Number::One.respond_to? :abs
38
+ end
39
+
40
+ def test_send_mirroring
41
+ assert_equal -1, -Number::One
42
+ end
43
+
44
+ def test_enumerable
45
+ assert_equal [Number::Zero, Number::One, Number::Two], Number.to_a
46
+ end
47
+ end
@@ -0,0 +1,24 @@
1
+ require "nice_enum"
2
+ require "test/unit"
3
+
4
+ class DefaultAttributeTest < Test::Unit::TestCase
5
+ class Number < Enum
6
+ default :note => "A boring number"
7
+
8
+ enum :Twenty, 20,
9
+ :note => "Exact value of e^pi - pi"
10
+
11
+ enum :Ninety, 90,
12
+ :note => "Square Root of -1"
13
+
14
+ enum :FourtyTwo, 42
15
+ end
16
+
17
+ def test_get_set_attribute
18
+ assert_equal "Exact value of e^pi - pi", Number::Twenty.note
19
+ end
20
+
21
+ def test_get_unset_attribute
22
+ assert_equal "A boring number", Number::FourtyTwo.note
23
+ end
24
+ end
@@ -0,0 +1,22 @@
1
+ require "test/unit"
2
+ require "nice_enum"
3
+
4
+ class DefaultValueTest < Test::Unit::TestCase
5
+ class Suit < Enum
6
+ enum :Spades
7
+ enum :Hearts
8
+ enum :Green, 48
9
+ enum :Diamonds
10
+ enum :Clubs
11
+ end
12
+
13
+ def test_enum_order
14
+ assert_equal [Suit::Spades, Suit::Hearts, Suit::Green, Suit::Diamonds, Suit::Clubs], Suit.to_a.sort
15
+ end
16
+
17
+ def test_values
18
+ assert_equal 1, Suit::Hearts.value
19
+ assert_equal 48, Suit::Green.value
20
+ assert_equal 49, Suit::Diamonds.value
21
+ end
22
+ end
@@ -0,0 +1,38 @@
1
+ require "test/unit"
2
+ require "nice_enum"
3
+
4
+ class FlagsTest < Test::Unit::TestCase
5
+ class Permission < Flags
6
+ enum :Read, 4
7
+ enum :Write, 2
8
+ enum :Execute, 1
9
+ end
10
+
11
+ def test_binary_operators_return_class
12
+ assert (Permission::Read | Permission::Write).is_a? Permission
13
+ end
14
+
15
+ def test_or
16
+ assert_equal 6, Permission::Read | Permission::Write
17
+ end
18
+
19
+ def test_and
20
+ assert_equal Permission::Write, 7 & Permission::Write
21
+ end
22
+
23
+ def test_xor
24
+ assert_equal Permission::Read | Permission::Write, 7 ^ Permission::Execute
25
+ end
26
+
27
+ def test_single_flag
28
+ assert_equal [Permission::Read], Permission::Read.flags
29
+ end
30
+
31
+ def test_multi_flags
32
+ assert_equal [Permission::Write, Permission::Read], Permission.new(6).flags
33
+ end
34
+
35
+ def test_to_s
36
+ assert_equal "Execute|Write", Permission.new(3).to_s.gsub(' ', '')
37
+ end
38
+ end
@@ -0,0 +1,20 @@
1
+ require "nice_enum"
2
+ require "test/unit"
3
+
4
+ class HashcodeTest < Test::Unit::TestCase
5
+ VALUE = 18505
6
+ HASH = VALUE.hash
7
+
8
+ class Enumeration < Enum
9
+ enum :Value, VALUE
10
+ end
11
+
12
+ def test_hashcode_is_underlying_hash
13
+ assert_equal HASH, Enumeration::Value.hash
14
+ end
15
+
16
+ def test_hashcode_from_constructor
17
+ value = Enumeration.new(VALUE)
18
+ assert_equal HASH, value.hash
19
+ end
20
+ end
metadata ADDED
@@ -0,0 +1,80 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: nice_enum
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 2
9
+ version: 0.1.2
10
+ platform: ruby
11
+ authors:
12
+ - Raphael Robatsch
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2011-01-11 00:00:00 +01:00
18
+ default_executable:
19
+ dependencies: []
20
+
21
+ description:
22
+ email: raphaelr@f-m.fm
23
+ executables: []
24
+
25
+ extensions: []
26
+
27
+ extra_rdoc_files:
28
+ - README.rdoc
29
+ files:
30
+ - lib/nice_enum.rb
31
+ - samples/deck.rb
32
+ - samples/filemodes.rb
33
+ - samples/squaring_numbers.rb
34
+ - README.rdoc
35
+ - LICENSE
36
+ - test/attribute_test.rb
37
+ - test/const_test.rb
38
+ - test/default_attribute_test.rb
39
+ - test/default_value_test.rb
40
+ - test/flags_test.rb
41
+ - test/hashcode_test.rb
42
+ has_rdoc: true
43
+ homepage: http://raphaelr.github.com/nice_enum
44
+ licenses: []
45
+
46
+ post_install_message:
47
+ rdoc_options: []
48
+
49
+ require_paths:
50
+ - lib
51
+ required_ruby_version: !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ segments:
57
+ - 0
58
+ version: "0"
59
+ required_rubygems_version: !ruby/object:Gem::Requirement
60
+ none: false
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ segments:
65
+ - 0
66
+ version: "0"
67
+ requirements: []
68
+
69
+ rubyforge_project:
70
+ rubygems_version: 1.3.7
71
+ signing_key:
72
+ specification_version: 3
73
+ summary: Nice Enumerations for Ruby
74
+ test_files:
75
+ - test/attribute_test.rb
76
+ - test/const_test.rb
77
+ - test/default_attribute_test.rb
78
+ - test/default_value_test.rb
79
+ - test/flags_test.rb
80
+ - test/hashcode_test.rb