typesafe_enum 0.2.1 → 0.2.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0e1176c5141f142b7273ea78e428250bdf6d1035a7d396e4eff850200c450f0e
4
- data.tar.gz: 8348631d2f84b3a56f8008be1638b667544fa9a557cbbf8e75a0ea1a421bbaf0
3
+ metadata.gz: bb07ad036d64d6c64c976b86db50a4f94a33c85fb18410077551282ad2d32f51
4
+ data.tar.gz: 0c9a2342fa1163a2475fe0c7bb7656c333e678a66ae5be47b6beed9934cebd17
5
5
  SHA512:
6
- metadata.gz: '0590bab7859bd8d61a2846814179553dab741911a0919f8c90446642af3991a75fc2d6ef3be8979eb2713e4b1860379b8acd4e978f708459ba006b8540a613fb'
7
- data.tar.gz: a7135f565f5b37526e159acce7e4fdeee98f556d7acb02dce9b52fb38e7036fb38d2891954a3bb29d8cabfeb5dc72e4f8fad7e4486a6029ccb4dc424da0854f6
6
+ metadata.gz: 8a39483acf17f3a706dd61caa5cebd6c269789a23baf8b36ce0c7f3e4a870d3e0dc15b47753e268505417e00d769dea6d1e1719a9e5570fba5af9f8329da37ea
7
+ data.tar.gz: b02640de3559f49920ae311c6cd2b68411a78bd38763289cb87726c3c4989d0ed49c98a1f2d0450d9a8ef79b0782cca1f62968d61ea83ce61deff1978b24e267
data/.gitignore CHANGED
@@ -1,6 +1,7 @@
1
1
  # IDEA
2
2
 
3
3
  .idea
4
+ /out/
4
5
 
5
6
  # Ruby defaults
6
7
 
data/CHANGES.md CHANGED
@@ -1,3 +1,7 @@
1
+ ## 0.2.2 (23 April 2020)
2
+
3
+ - Implement [`Enumerable`](https://ruby-doc.org/core-2.6.5/Enumerable.html)
4
+
1
5
  ## 0.2.1 (12 March 2020)
2
6
 
3
7
  - Update to Rake 12.3.3
data/README.md CHANGED
@@ -14,23 +14,25 @@ with syntax loosely inspired by [Ruby::Enum](https://github.com/dblock/ruby-enum
14
14
  - [Basic usage](#basic-usage)
15
15
  - [Ordering](#ordering)
16
16
  - [String representations](#string-representations)
17
+ - [Enumerable](#enumerable)
17
18
  - [Convenience methods on enum classes](#convenience-methods-on-enum-classes)
18
- - [::to\_a](#to_a)
19
- - [::size](#size)
20
- - [::each, ::each\_with\_index, and ::map](#each-each_with_index-and-map)
21
- - [::find\_by\_key, ::find\_by\_value, ::find\_by\_ord](#find_by_key-find_by_value-find_by_ord)
22
- - [::find\_by\_value\_str](#find_by_value_str)
19
+ - [#to_a](#to_a)
20
+ - [#size](#size)
21
+ - [#each, <code>#each_with_index</code>, <code>#map</code> and <code>#flat_map</code>](#each-each_with_index-map-and-flat_map)
22
+ - [#find_by_key, <code>#find_by_value</code>, <code>#find_by_ord</code>](#find_by_key-find_by_value-find_by_ord)
23
+ - [#find_by_value_str](#find_by_value_str)
23
24
  - [Enum classes with methods](#enum-classes-with-methods)
24
25
  - [Enum instances with methods](#enum-instances-with-methods)
25
- - [How is this different from Ruby::Enum?](#how-is-this-different-from-rubyenum)
26
+ - [How is this different from <a href="https://github.com/dblock/ruby-enum">Ruby::Enum</a>?](#how-is-this-different-from-rubyenum)
26
27
  - [How is this different from java.lang.Enum?](#how-is-this-different-from-javalangenum)
27
- - [Clunkier syntax](#clunkier-syntax)
28
- - [No special switch/case support](#no-special-switchcase-support)
29
- - [No serialization support](#no-serialization-support)
30
- - [No support classes](#no-support-classes)
31
- - [Enum classes are not closed](#enum-classes-are-not-closed)
28
+ - [Clunkier syntax](#clunkier-syntax)
29
+ - [No special switch/<code>case</code> support](#no-special-switchcase-support)
30
+ - [No serialization support](#no-serialization-support)
31
+ - [No support classes](#no-support-classes)
32
+ - [Enum classes are not closed](#enum-classes-are-not-closed)
32
33
  - [Contributing](#contributing)
33
34
 
35
+
34
36
  ## Basic usage
35
37
 
36
38
  Create a new enum class and a set of instances:
@@ -199,9 +201,19 @@ Suit::DIAMONDS.to_s
199
201
 
200
202
  It can of course be overridden.
201
203
 
204
+ ## `Enumerable`
205
+
206
+ As of version 0.2.2, `TypesafeEnum` classes implement
207
+ [`Enumerable`](https://ruby-doc.org/core-2.6.5/Enumerable.html),
208
+ so they support methods such as
209
+ [`#find`](https://ruby-doc.org/core-2.6.5/Enumerable.html#method-i-find),
210
+ [`#select`](https://ruby-doc.org/core-2.6.5/Enumerable.html#method-i-select),
211
+ and [`#reduce`](https://ruby-doc.org/core-2.6.5/Enumerable.html#method-i-reduce),
212
+ in addition to the convenience methods called out specifically below.
213
+
202
214
  ## Convenience methods on enum classes
203
215
 
204
- ### `::to_a`
216
+ ### `#to_a`
205
217
 
206
218
  Returns an array of the enum instances in declaration order:
207
219
 
@@ -210,7 +222,7 @@ Tarot.to_a
210
222
  # => [#<Tarot:0x007fd4db30eca8 @key=:CUPS, @value="Cups", @ord=0>, #<Tarot:0x007fd4db30ebe0 @key=:COINS, @value="Coins", @ord=1>, #<Tarot:0x007fd4db30eaf0 @key=:WANDS, @value="Wands", @ord=2>, #<Tarot:0x007fd4db30e9b0 @key=:SWORDS, @value="Swords", @ord=3>]
211
223
  ```
212
224
 
213
- ### `::size`
225
+ ### `#size`
214
226
 
215
227
  Returns the number of enum instances:
216
228
 
@@ -219,7 +231,7 @@ Suit.size
219
231
  # => 4
220
232
  ```
221
233
 
222
- ### `::each`, `::each_with_index`, and `::map`
234
+ ### `#each`, `#each_with_index`, `#map` and `#flat_map`
223
235
 
224
236
  Iterate over the set of enum instances:
225
237
 
@@ -238,9 +250,12 @@ Suit.each_with_index { |s, i| puts "#{i}: #{s.key}" }
238
250
 
239
251
  Suit.map(&:value)
240
252
  # => ["clubs", "diamonds", "hearts", "spades"]
253
+
254
+ Suit.flat_map { |s| [s.key, s.value] }
255
+ # => [:CLUBS, "clubs", :DIAMONDS, "diamonds", :HEARTS, "hearts", :SPADES, "spades"]
241
256
  ```
242
257
 
243
- ### `::find_by_key`, `::find_by_value`, `::find_by_ord`
258
+ ### `#find_by_key`, `#find_by_value`, `#find_by_ord`
244
259
 
245
260
  Look up an enum instance based on its key, value, or ordinal:
246
261
 
@@ -253,7 +268,7 @@ Tarot.find_by_ord(3)
253
268
  # => #<Tarot:0x007faab19fd810 @key=:SWORDS, @value="Swords", @ord=3>
254
269
  ```
255
270
 
256
- ### `::find_by_value_str`
271
+ ### `#find_by_value_str`
257
272
 
258
273
  Look up an enum instance based on the string form of its value (as returned by `to_s`) --
259
274
  useful for, e.g., XML or JSON mapping of enums with non-string values:
@@ -8,6 +8,7 @@ module TypesafeEnum
8
8
  include Comparable
9
9
 
10
10
  class << self
11
+ include Enumerable
11
12
 
12
13
  # Returns an array of the enum instances in declaration order
13
14
  # @return [Array<self>] All instances of this enum, in declaration order
@@ -18,32 +19,16 @@ module TypesafeEnum
18
19
  # Returns the number of enum instances
19
20
  # @return [Integer] the number of instances
20
21
  def size
21
- as_array ? as_array.length : 0
22
+ as_array.size
22
23
  end
23
24
 
24
25
  # Iterates over the set of enum instances
25
26
  # @yield [self] Each instance of this enum, in declaration order
26
- # @return [Array<self>] All instances of this enum, in declaration order
27
+ # @return [Enumerator<self>] All instances of this enum, in declaration order
27
28
  def each(&block)
28
29
  to_a.each(&block)
29
30
  end
30
31
 
31
- # Iterates over the set of enum instances
32
- # @yield [self, Integer] Each instance of this enum, in declaration order,
33
- # with its ordinal index
34
- # @return [Array<self>] All instances of this enum, in declaration order
35
- def each_with_index(&block)
36
- to_a.each_with_index(&block)
37
- end
38
-
39
- # Iterates over the set of enum instances
40
- # @yield [self] Each instance of this enum, in declaration order
41
- # @return [Array] An array containing the result of applying `&block`
42
- # to each instance of this enum, in instance declaration order
43
- def map(&block)
44
- to_a.map(&block)
45
- end
46
-
47
32
  # Looks up an enum instance based on its key
48
33
  # @param key [Symbol] the key to look up
49
34
  # @return [self, nil] the corresponding enum instance, or nil
@@ -5,7 +5,7 @@ module TypesafeEnum
5
5
  NAME = 'typesafe_enum'
6
6
 
7
7
  # The version of this gem
8
- VERSION = '0.2.1'
8
+ VERSION = '0.2.2'
9
9
 
10
10
  # The copyright notice for this gem
11
11
  COPYRIGHT = 'Copyright (c) 2020 The Regents of the University of California'
@@ -32,8 +32,8 @@ end
32
32
  module TypesafeEnum
33
33
  describe Base do
34
34
 
35
- describe '::new' do
36
- it ' news a constant enum value' do
35
+ describe :new do
36
+ it 'news a constant enum value' do
37
37
  enum = Suit::CLUBS
38
38
  expect(enum).to be_a(Suit)
39
39
  end
@@ -113,7 +113,7 @@ module TypesafeEnum
113
113
  end
114
114
  end
115
115
 
116
- describe '::to_a' do
116
+ describe :to_a do
117
117
  it 'returns the values as an array' do
118
118
  expect(Suit.to_a).to eq([Suit::CLUBS, Suit::DIAMONDS, Suit::HEARTS, Suit::SPADES])
119
119
  end
@@ -125,13 +125,27 @@ module TypesafeEnum
125
125
  end
126
126
  end
127
127
 
128
- describe '::size' do
128
+ describe :size do
129
129
  it 'returns the number of enum instnaces' do
130
130
  expect(Suit.size).to eq(4)
131
131
  end
132
132
  end
133
133
 
134
- describe '::each' do
134
+ describe :count do
135
+ it 'returns the number of enum instnaces' do
136
+ expect(Suit.count).to eq(4)
137
+ end
138
+
139
+ it 'counts items' do
140
+ expect(Suit.count(Suit::SPADES)).to eq(1)
141
+ end
142
+
143
+ it 'counts items that match a predicate' do
144
+ expect(Suit.count { |s| s.value.length == 6 }).to eq(2)
145
+ end
146
+ end
147
+
148
+ describe :each do
135
149
  it 'iterates the enum values' do
136
150
  expected = [Suit::CLUBS, Suit::DIAMONDS, Suit::HEARTS, Suit::SPADES]
137
151
  index = 0
@@ -142,7 +156,7 @@ module TypesafeEnum
142
156
  end
143
157
  end
144
158
 
145
- describe '::each_with_index' do
159
+ describe :each_with_index do
146
160
  it 'iterates the enum values with indices' do
147
161
  expected = [Suit::CLUBS, Suit::DIAMONDS, Suit::HEARTS, Suit::SPADES]
148
162
  Suit.each_with_index do |s, index|
@@ -151,14 +165,21 @@ module TypesafeEnum
151
165
  end
152
166
  end
153
167
 
154
- describe '::map' do
168
+ describe :map do
155
169
  it 'maps enum values' do
156
170
  all_keys = Suit.map(&:key)
157
171
  expect(all_keys).to eq(%i[CLUBS DIAMONDS HEARTS SPADES])
158
172
  end
159
173
  end
160
174
 
161
- describe '#<=>' do
175
+ describe :flat_map do
176
+ it 'flatmaps enum values' do
177
+ result = Tarot.flat_map { |t| [t.key, t.value] }
178
+ expect(result).to eq([:CUPS, 'Cups', :COINS, 'Coins', :WANDS, 'Wands', :SWORDS, 'Swords'])
179
+ end
180
+ end
181
+
182
+ describe :<=> do
162
183
  it 'orders enum instances' do
163
184
  Suit.each_with_index do |s1, i1|
164
185
  Suit.each_with_index do |s2, i2|
@@ -168,7 +189,7 @@ module TypesafeEnum
168
189
  end
169
190
  end
170
191
 
171
- describe '#==' do
192
+ describe :== do
172
193
  it 'returns true for identical instances, false otherwise' do
173
194
  Suit.each do |s1|
174
195
  Suit.each do |s2|
@@ -202,7 +223,7 @@ module TypesafeEnum
202
223
  # rubocop:enable Security/MarshalLoad
203
224
  end
204
225
 
205
- describe '#!=' do
226
+ describe :!= do
206
227
  it 'returns false for identical instances, true otherwise' do
207
228
  Suit.each do |s1|
208
229
  Suit.each do |s2|
@@ -224,7 +245,7 @@ module TypesafeEnum
224
245
  end
225
246
  end
226
247
 
227
- describe '#hash' do
248
+ describe :hash do
228
249
  it 'gives consistent values' do
229
250
  Suit.each do |s1|
230
251
  Suit.each do |s2|
@@ -264,7 +285,7 @@ module TypesafeEnum
264
285
  end
265
286
  end
266
287
 
267
- describe '#eql?' do
288
+ describe :eql? do
268
289
  it 'is consistent with #hash' do
269
290
  Suit.each do |s1|
270
291
  Suit.each do |s2|
@@ -274,7 +295,7 @@ module TypesafeEnum
274
295
  end
275
296
  end
276
297
 
277
- describe '#value' do
298
+ describe :value do
278
299
  it 'returns the string value of the enum instance' do
279
300
  expected = %w[clubs diamonds hearts spades]
280
301
  Suit.each_with_index do |s, index|
@@ -283,7 +304,7 @@ module TypesafeEnum
283
304
  end
284
305
  end
285
306
 
286
- describe '#key' do
307
+ describe :key do
287
308
  it 'returns the symbol key of the enum instance' do
288
309
  expected = %i[CLUBS DIAMONDS HEARTS SPADES]
289
310
  Suit.each_with_index do |s, index|
@@ -292,7 +313,7 @@ module TypesafeEnum
292
313
  end
293
314
  end
294
315
 
295
- describe '#ord' do
316
+ describe :ord do
296
317
  it 'returns the ord value of the enum instance' do
297
318
  Suit.each_with_index do |s, index|
298
319
  expect(s.ord).to eq(index)
@@ -300,7 +321,7 @@ module TypesafeEnum
300
321
  end
301
322
  end
302
323
 
303
- describe '#to_s' do
324
+ describe :to_s do
304
325
  it 'provides an informative string' do
305
326
  aggregate_failures 'informative string' do
306
327
  [Suit, Tarot, RGBColor, Scale].each do |ec|
@@ -315,7 +336,7 @@ module TypesafeEnum
315
336
  end
316
337
  end
317
338
 
318
- describe '::find_by_key' do
339
+ describe :find_by_key do
319
340
  it 'maps symbol keys to enum instances' do
320
341
  keys = %i[CLUBS DIAMONDS HEARTS SPADES]
321
342
  expected = Suit.to_a
@@ -329,7 +350,7 @@ module TypesafeEnum
329
350
  end
330
351
  end
331
352
 
332
- describe '::find_by_value' do
353
+ describe :find_by_value do
333
354
  it 'maps values to enum instances' do
334
355
  values = %w[clubs diamonds hearts spades]
335
356
  expected = Suit.to_a
@@ -355,7 +376,7 @@ module TypesafeEnum
355
376
  end
356
377
  end
357
378
 
358
- describe '::find_by_value_str' do
379
+ describe :find_by_value_str do
359
380
  it 'maps string values to enum instances' do
360
381
  values = %w[clubs diamonds hearts spades]
361
382
  expected = Suit.to_a
@@ -381,7 +402,7 @@ module TypesafeEnum
381
402
  end
382
403
  end
383
404
 
384
- describe '::find_by_ord' do
405
+ describe :find_by_ord do
385
406
  it 'maps ordinal indices to enum instances' do
386
407
  Suit.each do |s|
387
408
  expect(Suit.find_by_ord(s.ord)).to be(s)
@@ -436,5 +457,9 @@ module TypesafeEnum
436
457
 
437
458
  expect(Operation.map { |op| op.eval(39, 23) }).to eq([39 + 23, 39 - 23])
438
459
  end
460
+
461
+ it 'is an Enumerable' do
462
+ expect(Suit).to be_an(Enumerable)
463
+ end
439
464
  end
440
465
  end
@@ -26,7 +26,7 @@ Gem::Specification.new do |spec|
26
26
  spec.add_development_dependency 'rake', '~> 12.3', '>= 12.3.3'
27
27
  spec.add_development_dependency 'rspec', '~> 3.9'
28
28
  spec.add_development_dependency 'rubocop', '~> 0.80'
29
- spec.add_development_dependency 'simplecov', '~> 0.9.2'
30
- spec.add_development_dependency 'simplecov-console', '~> 0.2.0'
29
+ spec.add_development_dependency 'simplecov', '~> 0.18'
30
+ spec.add_development_dependency 'simplecov-console', '~> 0.7'
31
31
  spec.add_development_dependency 'yard', '~> 0.9', '>= 0.9.12'
32
32
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: typesafe_enum
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Moles
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-03-12 00:00:00.000000000 Z
11
+ date: 2020-04-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -64,28 +64,28 @@ dependencies:
64
64
  requirements:
65
65
  - - "~>"
66
66
  - !ruby/object:Gem::Version
67
- version: 0.9.2
67
+ version: '0.18'
68
68
  type: :development
69
69
  prerelease: false
70
70
  version_requirements: !ruby/object:Gem::Requirement
71
71
  requirements:
72
72
  - - "~>"
73
73
  - !ruby/object:Gem::Version
74
- version: 0.9.2
74
+ version: '0.18'
75
75
  - !ruby/object:Gem::Dependency
76
76
  name: simplecov-console
77
77
  requirement: !ruby/object:Gem::Requirement
78
78
  requirements:
79
79
  - - "~>"
80
80
  - !ruby/object:Gem::Version
81
- version: 0.2.0
81
+ version: '0.7'
82
82
  type: :development
83
83
  prerelease: false
84
84
  version_requirements: !ruby/object:Gem::Requirement
85
85
  requirements:
86
86
  - - "~>"
87
87
  - !ruby/object:Gem::Version
88
- version: 0.2.0
88
+ version: '0.7'
89
89
  - !ruby/object:Gem::Dependency
90
90
  name: yard
91
91
  requirement: !ruby/object:Gem::Requirement
@@ -149,7 +149,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
149
149
  - !ruby/object:Gem::Version
150
150
  version: '0'
151
151
  requirements: []
152
- rubygems_version: 3.0.6
152
+ rubygems_version: 3.0.8
153
153
  signing_key:
154
154
  specification_version: 4
155
155
  summary: Typesafe enum pattern for Ruby