typesafe_enum 0.1.7 → 0.1.8
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +4 -0
- data/.rubocop.yml +3 -3
- data/.ruby-version +1 -1
- data/CHANGES.md +7 -0
- data/Gemfile +2 -0
- data/Rakefile +5 -3
- data/lib/typesafe_enum.rb +2 -0
- data/lib/typesafe_enum/base.rb +8 -6
- data/lib/typesafe_enum/module_info.rb +4 -2
- data/spec/.rubocop.yml +6 -0
- data/spec/spec_helper.rb +2 -0
- data/spec/unit/typesafe_enum/base_spec.rb +13 -11
- data/typesafe_enum.gemspec +6 -4
- metadata +23 -18
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1f9b1132621b9e769433ef61e8fd2512a66197c4
|
4
|
+
data.tar.gz: f5394bf7f1a1e88c56c727352f8d2dd113934855
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ffd661decf927ce85719a8cac164e7105f674e4289d68d546102f500aeb8d3a70907a25fafee22960c07967104ec77b8ee4ac8310450ba2febeafdb75c31dfa0
|
7
|
+
data.tar.gz: 6f4770854fe78d823c0031f16338c081a4fbac9def1e519e4f7c579566c57ffbac0a13f4d597890b42fe97df614a52a3f6ff83b48fca5b505c96004bcf9833c0
|
data/.gitignore
CHANGED
data/.rubocop.yml
CHANGED
@@ -12,13 +12,13 @@ Style/Documentation:
|
|
12
12
|
Enabled: false
|
13
13
|
|
14
14
|
# Allow one line around class body (Style/EmptyLines will still disallow two or more)
|
15
|
-
|
15
|
+
Layout/EmptyLinesAroundClassBody:
|
16
16
|
Enabled: false
|
17
17
|
|
18
18
|
# Allow one line around module body (Style/EmptyLines will still disallow two or more)
|
19
|
-
|
19
|
+
Layout/EmptyLinesAroundModuleBody:
|
20
20
|
Enabled: false
|
21
21
|
|
22
22
|
# Allow one line around block body (Style/EmptyLines will still disallow two or more)
|
23
|
-
|
23
|
+
Layout/EmptyLinesAroundBlockBody:
|
24
24
|
Enabled: false
|
data/.ruby-version
CHANGED
@@ -1 +1 @@
|
|
1
|
-
2.
|
1
|
+
2.4.1
|
data/CHANGES.md
CHANGED
data/Gemfile
CHANGED
data/Rakefile
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
# ------------------------------------------------------------
|
2
4
|
# RSpec
|
3
5
|
|
@@ -8,7 +10,7 @@ namespace :spec do
|
|
8
10
|
|
9
11
|
desc 'Run all unit tests'
|
10
12
|
RSpec::Core::RakeTask.new(:unit) do |task|
|
11
|
-
task.rspec_opts = %w
|
13
|
+
task.rspec_opts = %w[--color --format documentation --order default]
|
12
14
|
task.pattern = 'unit/**/*_spec.rb'
|
13
15
|
end
|
14
16
|
|
@@ -38,7 +40,7 @@ RuboCop::RakeTask.new
|
|
38
40
|
|
39
41
|
desc 'List TODOs (from spec/todo.rb)'
|
40
42
|
RSpec::Core::RakeTask.new(:todo) do |task|
|
41
|
-
task.rspec_opts = %w
|
43
|
+
task.rspec_opts = %w[--color --format documentation --order default]
|
42
44
|
task.pattern = 'todo.rb'
|
43
45
|
end
|
44
46
|
|
@@ -46,4 +48,4 @@ end
|
|
46
48
|
# Defaults
|
47
49
|
|
48
50
|
desc 'Run unit tests, check test coverage, run acceptance tests, check code style'
|
49
|
-
task default: [
|
51
|
+
task default: %i[coverage rubocop]
|
data/lib/typesafe_enum.rb
CHANGED
data/lib/typesafe_enum/base.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
# A Ruby implementation of Joshua Bloch's
|
2
4
|
# [typesafe enum pattern](http://www.oracle.com/technetwork/java/page1-139488.html#replaceenums)
|
3
5
|
module TypesafeEnum
|
@@ -68,7 +70,7 @@ module TypesafeEnum
|
|
68
70
|
# @param ord [Integer] the ordinal to look up
|
69
71
|
# @return [self, nil] the corresponding enum instance, or nil
|
70
72
|
def find_by_ord(ord)
|
71
|
-
return nil if ord
|
73
|
+
return nil if ord > size || ord.negative?
|
72
74
|
as_array[ord]
|
73
75
|
end
|
74
76
|
|
@@ -94,11 +96,11 @@ module TypesafeEnum
|
|
94
96
|
key = instance.key
|
95
97
|
value = instance.value
|
96
98
|
if (found = find_by_key(key))
|
97
|
-
|
98
|
-
warn("ignoring redeclaration of #{name}::#{key} with value #{value} (source: #{caller
|
99
|
+
raise NameError, "#{name}::#{key} already exists" unless value == found.value
|
100
|
+
warn("ignoring redeclaration of #{name}::#{key} with value #{value} (source: #{caller(5..5).first})")
|
99
101
|
nil
|
100
102
|
else
|
101
|
-
|
103
|
+
raise NameError, "A #{name} instance with value '#{value}' already exists" if find_by_value(value)
|
102
104
|
[key, value]
|
103
105
|
end
|
104
106
|
end
|
@@ -143,7 +145,7 @@ module TypesafeEnum
|
|
143
145
|
result = 17
|
144
146
|
result = 31 * result + self.class.hash
|
145
147
|
result = 31 * result + ord
|
146
|
-
result.is_a?(
|
148
|
+
result.is_a?(Integer) ? result : result.hash
|
147
149
|
end
|
148
150
|
end
|
149
151
|
|
@@ -154,7 +156,7 @@ module TypesafeEnum
|
|
154
156
|
private
|
155
157
|
|
156
158
|
def initialize(key, value = nil, &block)
|
157
|
-
|
159
|
+
raise TypeError, "#{key} is not a symbol" unless key.is_a?(Symbol)
|
158
160
|
@key = key
|
159
161
|
@value = value || key.to_s.downcase
|
160
162
|
@ord = self.class.size
|
@@ -1,10 +1,12 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module TypesafeEnum
|
2
4
|
# The name of this gem
|
3
5
|
NAME = 'typesafe_enum'
|
4
6
|
|
5
7
|
# The version of this gem
|
6
|
-
VERSION = '0.1.
|
8
|
+
VERSION = '0.1.8'
|
7
9
|
|
8
10
|
# The copyright notice for this gem
|
9
|
-
COPYRIGHT = 'Copyright (c)
|
11
|
+
COPYRIGHT = 'Copyright (c) 2017 The Regents of the University of California'
|
10
12
|
end
|
data/spec/.rubocop.yml
CHANGED
@@ -1,5 +1,8 @@
|
|
1
1
|
inherit_from: ../.rubocop.yml
|
2
2
|
|
3
|
+
Metrics/BlockLength:
|
4
|
+
Enabled: false
|
5
|
+
|
3
6
|
Metrics/MethodLength:
|
4
7
|
Enabled: false
|
5
8
|
|
@@ -8,3 +11,6 @@ Metrics/ModuleLength:
|
|
8
11
|
|
9
12
|
Style/ClassAndModuleChildren:
|
10
13
|
Enabled: false
|
14
|
+
|
15
|
+
Security/MarshalLoad:
|
16
|
+
Enabled: false
|
data/spec/spec_helper.rb
CHANGED
@@ -1,4 +1,6 @@
|
|
1
|
-
|
1
|
+
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
2
4
|
require 'spec_helper'
|
3
5
|
|
4
6
|
class Suit < TypesafeEnum::Base
|
@@ -152,7 +154,7 @@ module TypesafeEnum
|
|
152
154
|
describe '::map' do
|
153
155
|
it 'maps enum values' do
|
154
156
|
all_keys = Suit.map(&:key)
|
155
|
-
expect(all_keys).to eq([
|
157
|
+
expect(all_keys).to eq(%i[CLUBS DIAMONDS HEARTS SPADES])
|
156
158
|
end
|
157
159
|
end
|
158
160
|
|
@@ -253,7 +255,7 @@ module TypesafeEnum
|
|
253
255
|
|
254
256
|
it 'always returns a Fixnum' do
|
255
257
|
Suit.each do |s1|
|
256
|
-
expect(s1.hash).to be_a(
|
258
|
+
expect(s1.hash).to be_a(Integer)
|
257
259
|
end
|
258
260
|
end
|
259
261
|
end
|
@@ -270,7 +272,7 @@ module TypesafeEnum
|
|
270
272
|
|
271
273
|
describe '#value' do
|
272
274
|
it 'returns the string value of the enum instance' do
|
273
|
-
expected = %w
|
275
|
+
expected = %w[clubs diamonds hearts spades]
|
274
276
|
Suit.each_with_index do |s, index|
|
275
277
|
expect(s.value).to eq(expected[index])
|
276
278
|
end
|
@@ -279,7 +281,7 @@ module TypesafeEnum
|
|
279
281
|
|
280
282
|
describe '#key' do
|
281
283
|
it 'returns the symbol key of the enum instance' do
|
282
|
-
expected = [
|
284
|
+
expected = %i[CLUBS DIAMONDS HEARTS SPADES]
|
283
285
|
Suit.each_with_index do |s, index|
|
284
286
|
expect(s.key).to eq(expected[index])
|
285
287
|
end
|
@@ -301,7 +303,7 @@ module TypesafeEnum
|
|
301
303
|
ec.each do |ev|
|
302
304
|
result = ev.to_s
|
303
305
|
[ec.to_s, ev.key, ev.ord, ev.value].each do |info|
|
304
|
-
expect(result).to include(
|
306
|
+
expect(result).to include(info.to_s)
|
305
307
|
end
|
306
308
|
end
|
307
309
|
end
|
@@ -311,7 +313,7 @@ module TypesafeEnum
|
|
311
313
|
|
312
314
|
describe '::find_by_key' do
|
313
315
|
it 'maps symbol keys to enum instances' do
|
314
|
-
keys = [
|
316
|
+
keys = %i[CLUBS DIAMONDS HEARTS SPADES]
|
315
317
|
expected = Suit.to_a
|
316
318
|
keys.each_with_index do |k, index|
|
317
319
|
expect(Suit.find_by_key(k)).to be(expected[index])
|
@@ -325,7 +327,7 @@ module TypesafeEnum
|
|
325
327
|
|
326
328
|
describe '::find_by_value' do
|
327
329
|
it 'maps values to enum instances' do
|
328
|
-
values = %w
|
330
|
+
values = %w[clubs diamonds hearts spades]
|
329
331
|
expected = Suit.to_a
|
330
332
|
values.each_with_index do |n, index|
|
331
333
|
expect(Suit.find_by_value(n)).to be(expected[index])
|
@@ -351,7 +353,7 @@ module TypesafeEnum
|
|
351
353
|
|
352
354
|
describe '::find_by_value_str' do
|
353
355
|
it 'maps string values to enum instances' do
|
354
|
-
values = %w
|
356
|
+
values = %w[clubs diamonds hearts spades]
|
355
357
|
expected = Suit.to_a
|
356
358
|
values.each_with_index do |n, index|
|
357
359
|
expect(Suit.find_by_value_str(n)).to be(expected[index])
|
@@ -407,11 +409,11 @@ module TypesafeEnum
|
|
407
409
|
when Suit::SPADES
|
408
410
|
'♠'
|
409
411
|
else
|
410
|
-
|
412
|
+
raise "unknown suit: #{self}"
|
411
413
|
end
|
412
414
|
end
|
413
415
|
|
414
|
-
expect(Suit.map { |s| pip(s) }).to eq(%w
|
416
|
+
expect(Suit.map { |s| pip(s) }).to eq(%w[♣ ♦ ♥ ♠])
|
415
417
|
end
|
416
418
|
|
417
419
|
it 'supports "inner class" methods' do
|
data/typesafe_enum.gemspec
CHANGED
@@ -1,4 +1,6 @@
|
|
1
|
-
|
1
|
+
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
2
4
|
lib = File.expand_path('../lib', __FILE__)
|
3
5
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
6
|
|
@@ -26,9 +28,9 @@ Gem::Specification.new do |spec|
|
|
26
28
|
|
27
29
|
spec.add_development_dependency 'bundler', '~> 1.7'
|
28
30
|
spec.add_development_dependency 'rake', '~> 10.4'
|
29
|
-
spec.add_development_dependency 'rspec', '~> 3.
|
31
|
+
spec.add_development_dependency 'rspec', '~> 3.7'
|
32
|
+
spec.add_development_dependency 'rubocop', '~> 0.52'
|
30
33
|
spec.add_development_dependency 'simplecov', '~> 0.9.2'
|
31
34
|
spec.add_development_dependency 'simplecov-console', '~> 0.2.0'
|
32
|
-
spec.add_development_dependency '
|
33
|
-
spec.add_development_dependency 'yard', '~> 0.8'
|
35
|
+
spec.add_development_dependency 'yard', '~> 0.9', '>= 0.9.12'
|
34
36
|
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.1.
|
4
|
+
version: 0.1.8
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- David Moles
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2017-12-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -44,70 +44,76 @@ dependencies:
|
|
44
44
|
requirements:
|
45
45
|
- - "~>"
|
46
46
|
- !ruby/object:Gem::Version
|
47
|
-
version: '3.
|
47
|
+
version: '3.7'
|
48
48
|
type: :development
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
52
|
- - "~>"
|
53
53
|
- !ruby/object:Gem::Version
|
54
|
-
version: '3.
|
54
|
+
version: '3.7'
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
|
-
name:
|
56
|
+
name: rubocop
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
58
58
|
requirements:
|
59
59
|
- - "~>"
|
60
60
|
- !ruby/object:Gem::Version
|
61
|
-
version: 0.
|
61
|
+
version: '0.52'
|
62
62
|
type: :development
|
63
63
|
prerelease: false
|
64
64
|
version_requirements: !ruby/object:Gem::Requirement
|
65
65
|
requirements:
|
66
66
|
- - "~>"
|
67
67
|
- !ruby/object:Gem::Version
|
68
|
-
version: 0.
|
68
|
+
version: '0.52'
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
|
-
name: simplecov
|
70
|
+
name: simplecov
|
71
71
|
requirement: !ruby/object:Gem::Requirement
|
72
72
|
requirements:
|
73
73
|
- - "~>"
|
74
74
|
- !ruby/object:Gem::Version
|
75
|
-
version: 0.2
|
75
|
+
version: 0.9.2
|
76
76
|
type: :development
|
77
77
|
prerelease: false
|
78
78
|
version_requirements: !ruby/object:Gem::Requirement
|
79
79
|
requirements:
|
80
80
|
- - "~>"
|
81
81
|
- !ruby/object:Gem::Version
|
82
|
-
version: 0.2
|
82
|
+
version: 0.9.2
|
83
83
|
- !ruby/object:Gem::Dependency
|
84
|
-
name:
|
84
|
+
name: simplecov-console
|
85
85
|
requirement: !ruby/object:Gem::Requirement
|
86
86
|
requirements:
|
87
87
|
- - "~>"
|
88
88
|
- !ruby/object:Gem::Version
|
89
|
-
version: 0.
|
89
|
+
version: 0.2.0
|
90
90
|
type: :development
|
91
91
|
prerelease: false
|
92
92
|
version_requirements: !ruby/object:Gem::Requirement
|
93
93
|
requirements:
|
94
94
|
- - "~>"
|
95
95
|
- !ruby/object:Gem::Version
|
96
|
-
version: 0.
|
96
|
+
version: 0.2.0
|
97
97
|
- !ruby/object:Gem::Dependency
|
98
98
|
name: yard
|
99
99
|
requirement: !ruby/object:Gem::Requirement
|
100
100
|
requirements:
|
101
101
|
- - "~>"
|
102
102
|
- !ruby/object:Gem::Version
|
103
|
-
version: '0.
|
103
|
+
version: '0.9'
|
104
|
+
- - ">="
|
105
|
+
- !ruby/object:Gem::Version
|
106
|
+
version: 0.9.12
|
104
107
|
type: :development
|
105
108
|
prerelease: false
|
106
109
|
version_requirements: !ruby/object:Gem::Requirement
|
107
110
|
requirements:
|
108
111
|
- - "~>"
|
109
112
|
- !ruby/object:Gem::Version
|
110
|
-
version: '0.
|
113
|
+
version: '0.9'
|
114
|
+
- - ">="
|
115
|
+
- !ruby/object:Gem::Version
|
116
|
+
version: 0.9.12
|
111
117
|
description: A gem that implements the typesafe enum pattern in Ruby
|
112
118
|
email:
|
113
119
|
- david.moles@ucop.edu
|
@@ -132,7 +138,7 @@ files:
|
|
132
138
|
- spec/spec_helper.rb
|
133
139
|
- spec/unit/typesafe_enum/base_spec.rb
|
134
140
|
- typesafe_enum.gemspec
|
135
|
-
homepage: http://github.com/
|
141
|
+
homepage: http://github.com/typesafe_enum
|
136
142
|
licenses:
|
137
143
|
- MIT
|
138
144
|
metadata: {}
|
@@ -152,7 +158,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
152
158
|
version: '0'
|
153
159
|
requirements: []
|
154
160
|
rubyforge_project:
|
155
|
-
rubygems_version: 2.
|
161
|
+
rubygems_version: 2.6.12
|
156
162
|
signing_key:
|
157
163
|
specification_version: 4
|
158
164
|
summary: Typesafe enum pattern for Ruby
|
@@ -160,4 +166,3 @@ test_files:
|
|
160
166
|
- spec/.rubocop.yml
|
161
167
|
- spec/spec_helper.rb
|
162
168
|
- spec/unit/typesafe_enum/base_spec.rb
|
163
|
-
has_rdoc:
|