nice-enum 0.1.1 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- data/extconf.rb +11 -0
- metadata +10 -24
- data/LICENSE +0 -24
- data/README.rdoc +0 -198
- data/lib/nice_enum.rb +0 -162
- data/samples/deck.rb +0 -59
- data/samples/filemodes.rb +0 -20
- data/samples/squaring_numbers.rb +0 -16
- data/test/attribute_test.rb +0 -30
- data/test/const_test.rb +0 -47
- data/test/default_attribute_test.rb +0 -24
- data/test/default_value_test.rb +0 -22
- data/test/flags_test.rb +0 -38
data/extconf.rb
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
require "mkmf"
|
2
|
+
|
3
|
+
puts
|
4
|
+
puts "========================================================================="
|
5
|
+
puts "Dear nice-enum user,"
|
6
|
+
puts "This project has been renamed to nice_enum."
|
7
|
+
puts "Please run gem install nice_enum"
|
8
|
+
puts "Thank you."
|
9
|
+
puts "========================================================================="
|
10
|
+
puts
|
11
|
+
exit
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 1
|
8
|
-
-
|
9
|
-
version: 0.1.
|
8
|
+
- 3
|
9
|
+
version: 0.1.3
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Raphael Robatsch
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2011-01-
|
17
|
+
date: 2011-01-11 00:00:00 +01:00
|
18
18
|
default_executable:
|
19
19
|
dependencies: []
|
20
20
|
|
@@ -22,24 +22,14 @@ description:
|
|
22
22
|
email: raphaelr@f-m.fm
|
23
23
|
executables: []
|
24
24
|
|
25
|
-
extensions:
|
25
|
+
extensions:
|
26
|
+
- extconf.rb
|
27
|
+
extra_rdoc_files: []
|
26
28
|
|
27
|
-
extra_rdoc_files:
|
28
|
-
- README.rdoc
|
29
29
|
files:
|
30
|
-
-
|
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
|
30
|
+
- extconf.rb
|
41
31
|
has_rdoc: true
|
42
|
-
homepage: http://raphaelr.github.com/
|
32
|
+
homepage: http://raphaelr.github.com/nice_enum
|
43
33
|
licenses: []
|
44
34
|
|
45
35
|
post_install_message:
|
@@ -70,9 +60,5 @@ rubygems_version: 1.3.7
|
|
70
60
|
signing_key:
|
71
61
|
specification_version: 3
|
72
62
|
summary: Nice Enumerations for Ruby
|
73
|
-
test_files:
|
74
|
-
|
75
|
-
- test/const_test.rb
|
76
|
-
- test/default_attribute_test.rb
|
77
|
-
- test/default_value_test.rb
|
78
|
-
- test/flags_test.rb
|
63
|
+
test_files: []
|
64
|
+
|
data/LICENSE
DELETED
@@ -1,24 +0,0 @@
|
|
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
DELETED
@@ -1,198 +0,0 @@
|
|
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
DELETED
@@ -1,162 +0,0 @@
|
|
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+.
|
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 name of the enumeration member.
|
49
|
-
def to_s
|
50
|
-
@name
|
51
|
-
end
|
52
|
-
|
53
|
-
alias old_respond_to? respond_to? # :nodoc:
|
54
|
-
def respond_to?(sym) # :nodoc:
|
55
|
-
old_respond_to?(sym) || @value.respond_to?(sym)
|
56
|
-
end
|
57
|
-
|
58
|
-
def method_missing(sym, *args, &block) # :nodoc:
|
59
|
-
@value.send(sym, *args, &block)
|
60
|
-
end
|
61
|
-
|
62
|
-
class << Enum
|
63
|
-
include Enumerable
|
64
|
-
|
65
|
-
# Returns the +Enum+ instance for the given value. If no enumeration
|
66
|
-
# member has been created for this value, a new instance is created,
|
67
|
-
# setting +name+ to +value.to_s+.
|
68
|
-
def new(value)
|
69
|
-
return @enumvalues[value] if @enumvalues.has_key? value
|
70
|
-
_setup_enumvalue(value, value, allocate)
|
71
|
-
end
|
72
|
-
|
73
|
-
# Returns the default value for the attribute +attr+. If no default
|
74
|
-
# value has been set, +nil+ is returned.
|
75
|
-
def default_value(attr)
|
76
|
-
@defaults ||= {}
|
77
|
-
@defaults[attr.to_sym]
|
78
|
-
end
|
79
|
-
|
80
|
-
# Enumerates all known enumeration members.
|
81
|
-
def each
|
82
|
-
@enumvalues ||= {}
|
83
|
-
@enumvalues.each_value { |enumvalue| yield enumvalue }
|
84
|
-
end
|
85
|
-
|
86
|
-
protected
|
87
|
-
# Adds a new enumeration member to the current enumeration. The
|
88
|
-
# generated member is accessible using the constant +name+. If
|
89
|
-
# +value+ is +nil+, it is set to the highest integer value in
|
90
|
-
# use \+ 1.
|
91
|
-
def enum(name, value = nil, attributes = {})
|
92
|
-
@enumvalues ||= {}
|
93
|
-
value ||= (@enumvalues.values.max || -1) + 1
|
94
|
-
|
95
|
-
enumvalue = allocate
|
96
|
-
const_set(name, enumvalue)
|
97
|
-
@enumvalues[value] = enumvalue
|
98
|
-
_setup_enumvalue(name, value, enumvalue, attributes)
|
99
|
-
end
|
100
|
-
|
101
|
-
def _setup_enumvalue(name, value, enumvalue, attributes = {}) # :nodoc:
|
102
|
-
enumvalue.instance_variable_set(:@name, name.to_s)
|
103
|
-
enumvalue.instance_variable_set(:@value, value)
|
104
|
-
attributes.each do |attribute, attr_value|
|
105
|
-
_create_accessor(attribute)
|
106
|
-
enumvalue.instance_variable_set("@#{attribute}", attr_value)
|
107
|
-
end
|
108
|
-
enumvalue
|
109
|
-
end
|
110
|
-
|
111
|
-
# Sets the default values for the attributes of the enumeration members.
|
112
|
-
# There can be more than one call to +defaults+.
|
113
|
-
def defaults(defs = {})
|
114
|
-
@defaults ||= {}
|
115
|
-
@defaults.merge!(defs)
|
116
|
-
end
|
117
|
-
alias default defaults
|
118
|
-
|
119
|
-
def _create_accessor(attribute) # :nodoc:
|
120
|
-
define_method(attribute) do
|
121
|
-
instance_variable_get("@#{attribute}") || self.class.default_value(attribute)
|
122
|
-
end
|
123
|
-
end
|
124
|
-
end
|
125
|
-
end
|
126
|
-
|
127
|
-
# Base class for enumerations in which members can be or'd together to create
|
128
|
-
# combinations of them. For example, Unix file permissions can be represented
|
129
|
-
# as a flag enumeration with the members <tt>Read = 4</tt>, <tt>Write = 2</tt>
|
130
|
-
# and <tt>Execute = 1</tt>. There are many samples in README.rdoc and samples/.
|
131
|
-
class Flags < Enum
|
132
|
-
# Returns the individual enumeration members that are contained in +self+.
|
133
|
-
# If the enumeration class contains a member with the value +0+, that
|
134
|
-
# member is only included in the result if there aren't any other members
|
135
|
-
# that are contained in +self+.
|
136
|
-
def flags
|
137
|
-
result = self.class.select { |flag| value & flag != 0 }.sort
|
138
|
-
result = [self.class.new(0)] if result.empty?
|
139
|
-
result
|
140
|
-
end
|
141
|
-
|
142
|
-
# Joins the result of +flags+ together.
|
143
|
-
def join(seperator = " | ")
|
144
|
-
flags.map { |flag| flag.name }.join(seperator)
|
145
|
-
end
|
146
|
-
alias to_s join
|
147
|
-
|
148
|
-
# Returns the combination of +self+ and +other+.
|
149
|
-
def |(other)
|
150
|
-
self.class.new(value | other)
|
151
|
-
end
|
152
|
-
|
153
|
-
# Returns the intersection of +self+ and +other+.
|
154
|
-
def &(other)
|
155
|
-
self.class.new(value & other)
|
156
|
-
end
|
157
|
-
|
158
|
-
# Bitwise-XORs +self+ with +other+.
|
159
|
-
def ^(other)
|
160
|
-
self.class.new(value ^ other)
|
161
|
-
end
|
162
|
-
end
|
data/samples/deck.rb
DELETED
@@ -1,59 +0,0 @@
|
|
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 }}"
|
data/samples/filemodes.rb
DELETED
@@ -1,20 +0,0 @@
|
|
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}"
|
data/samples/squaring_numbers.rb
DELETED
@@ -1,16 +0,0 @@
|
|
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}." }
|
data/test/attribute_test.rb
DELETED
@@ -1,30 +0,0 @@
|
|
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
|
data/test/const_test.rb
DELETED
@@ -1,47 +0,0 @@
|
|
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
|
@@ -1,24 +0,0 @@
|
|
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
|
data/test/default_value_test.rb
DELETED
@@ -1,22 +0,0 @@
|
|
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
|
data/test/flags_test.rb
DELETED
@@ -1,38 +0,0 @@
|
|
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
|