nice_enum 0.1.3 → 0.1.4

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 CHANGED
@@ -1,24 +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.
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 CHANGED
@@ -1,198 +1,199 @@
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.
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. Attempting to display the enum constants will result in their values
6
+ being displayed, which have no use to the end user of the application.
7
+
8
+ module Permissions
9
+ Read = 4
10
+ Write = 2
11
+ Execute = 1
12
+ end
13
+
14
+ permissions = Permissions::Read | Permissions::Write
15
+ puts permissions
16
+ # Will output "6" - Although correct, it won't make any sense for a
17
+ # computer-illiterate user.
18
+
19
+ nice_enum solves this problem by encapsulating the enum values inside an
20
+ instance of their enclosing class and making the to_s method return the
21
+ name of the constant. The above code translates to
22
+
23
+ class Permissions < Flags
24
+ enum :Read, 4
25
+ enum :Write, 2
26
+ enum :Execute, 1
27
+ end
28
+
29
+ permissions = Permissions::Read | Permissions::Write
30
+ puts permissions.join(" and ")
31
+ # Will output "Write and Read".
32
+
33
+ nice_enum contains two classes: Enum and Flags. Instances of Flag will
34
+ automatically figure out it's components when or'd together and provides a
35
+ different to_s method. Otherwise the classes are functionally identical.
36
+
37
+ Of course, an enum can use anything as it's value type, not only integers.
38
+ The value type must only provide a <=> operator, a +hash+ method, and a
39
+ <tt>eql?</tt> method.
40
+
41
+ The most recent documentation is available 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 inverse has to be done explicitly:
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 Integers 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.
199
+