typesafe_enum 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGES.md +5 -0
- data/README.md +12 -10
- data/lib/typesafe_enum/base.rb +6 -2
- data/lib/typesafe_enum/module_info.rb +1 -1
- data/spec/.rubocop.yml +3 -0
- data/spec/unit/typesafe_enum/base_spec.rb +12 -12
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6dca44bfdc19636b439aea18d0a5bbb195150fcf
|
4
|
+
data.tar.gz: ae8b31bcd16f05536e1bb2ffd9346fbd395465e7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d8c40232f8fa100c7de8f49d830e20bce082126d951a69a4e53294a89134647c6cf2d0244034efe47bb88348a70352d4b3c061c5ff6c6a0d19f296e4a4de154d
|
7
|
+
data.tar.gz: 24543c5f5b1b72dbf554662275d47c918ef3807e723648aebe34552b217b52e82a91f9a20f35d9a7569ace4279dcdebefb00df0b136099a2f802fb66ac6c9812
|
data/CHANGES.md
CHANGED
@@ -1,3 +1,8 @@
|
|
1
|
+
## 0.1.3
|
2
|
+
|
3
|
+
- Fixed issue where invalid classes weren't properly removed after duplicate name declarations,
|
4
|
+
polluting the namespace and causing duplicate declration error messages to be lost.
|
5
|
+
|
1
6
|
## 0.1.2
|
2
7
|
|
3
8
|
- Fixed issue where `::find_by_value_str` failed to return `nil` for bad values
|
data/README.md
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
# typesafe_enum
|
2
2
|
|
3
|
-
[![Build Status](https://travis-ci.org/dmolesUC3/typesafe_enum.
|
4
|
-
[![Code Climate](https://codeclimate.com/github/dmolesUC3/typesafe_enum.
|
5
|
-
[![Inline docs](http://inch-ci.org/github/dmolesUC3/typesafe_enum.
|
3
|
+
[![Build Status](https://travis-ci.org/dmolesUC3/typesafe_enum.svg?branch=master)](https://travis-ci.org/dmolesUC3/typesafe_enum)
|
4
|
+
[![Code Climate](https://codeclimate.com/github/dmolesUC3/typesafe_enum.svg)](https://codeclimate.com/github/dmolesUC3/typesafe_enum)
|
5
|
+
[![Inline docs](http://inch-ci.org/github/dmolesUC3/typesafe_enum.svg)](http://inch-ci.org/github/dmolesUC3/typesafe_enum)
|
6
6
|
[![Gem Version](https://img.shields.io/gem/v/typesafe_enum.svg)](https://github.com/dmolesUC3/typesafe_enum/releases)
|
7
7
|
|
8
8
|
A Ruby implementation of Joshua Bloch's
|
@@ -24,8 +24,8 @@ class Suit < TypesafeEnum::Base
|
|
24
24
|
end
|
25
25
|
```
|
26
26
|
|
27
|
-
A constant is declared for each instance, with
|
28
|
-
value:
|
27
|
+
A constant is declared for each instance, with an instance of the new
|
28
|
+
class as the value of that constant:
|
29
29
|
|
30
30
|
```ruby
|
31
31
|
Suit::CLUBS
|
@@ -159,7 +159,9 @@ is linear in the number of enum values, so it's best suited for smaller enumerat
|
|
159
159
|
|
160
160
|
## Enum classes with methods
|
161
161
|
|
162
|
-
Enum classes can have methods, and other non-enum constants
|
162
|
+
Enum classes are just classes. They can have methods, and other non-enum constants.
|
163
|
+
(The `:initialize` method for each class, though, is declared programmatically by
|
164
|
+
the base class. If you need to redefine it, be sure to alias and call the original.)
|
163
165
|
|
164
166
|
```ruby
|
165
167
|
class Suit < TypesafeEnum::Base
|
@@ -220,11 +222,11 @@ Operation.map { |op| op.eval(39, 23) }
|
|
220
222
|
as seen in C, [C++](https://msdn.microsoft.com/en-us/library/2dzy4k6e.aspx),
|
221
223
|
[C#](https://msdn.microsoft.com/en-us/library/sbbt4032.aspx), and
|
222
224
|
[Objective-C](https://developer.apple.com/library/ios/releasenotes/ObjectiveC/ModernizationObjC/AdoptingModernObjective-C/AdoptingModernObjective-C.html#//apple_ref/doc/uid/TP40014150-CH1-SW6).
|
223
|
-
In C and most C-like languages, an `enum` is simply a
|
225
|
+
In C and most C-like languages, an `enum` is simply a named set of `int` values
|
224
226
|
(though C++ and others require an explicit cast to assign an `enum` value to
|
225
227
|
an `int` variable).
|
226
228
|
|
227
|
-
Similarly, a `Ruby::Enum` class is simply a
|
229
|
+
Similarly, a `Ruby::Enum` class is simply a named set of values of any type,
|
228
230
|
with convenience methods for iterating over the set. Usually the values are
|
229
231
|
strings, but they can be of any type.
|
230
232
|
|
@@ -260,9 +262,9 @@ Java introduced the concept of "typesafe enums", first as a
|
|
260
262
|
[design pattern]((http://www.oracle.com/technetwork/java/page1-139488.html#replaceenums))
|
261
263
|
and later as a
|
262
264
|
[first-class language construct](https://docs.oracle.com/javase/1.5.0/docs/guide/language/enums.html).
|
263
|
-
In Java, an `Enum` class
|
265
|
+
In Java, an `Enum` class defines a closed, valued set of _instances of that class,_ rather than
|
264
266
|
of a primitive type such as an `int`, and those instances have all the features of other objects,
|
265
|
-
such as methods, fields, and type membership. Likewise, a `TypesafeEnum` class
|
267
|
+
such as methods, fields, and type membership. Likewise, a `TypesafeEnum` class defines a valued set
|
266
268
|
of instances of that class, rather than of a set of some other type.
|
267
269
|
|
268
270
|
```ruby
|
data/lib/typesafe_enum/base.rb
CHANGED
@@ -63,10 +63,14 @@ module TypesafeEnum
|
|
63
63
|
attr_accessor :by_value
|
64
64
|
attr_accessor :as_array
|
65
65
|
|
66
|
+
def parent
|
67
|
+
parent_name = name =~ /::[^:]+\Z/ ? $`.freeze : nil
|
68
|
+
parent_name ? Object.const_get(parent_name) : Object
|
69
|
+
end
|
70
|
+
|
66
71
|
def undefine_class
|
67
|
-
enclosing_module = Module.nesting.last
|
68
72
|
class_value = name.split('::').last || ''
|
69
|
-
|
73
|
+
parent.send(:remove_const, class_value)
|
70
74
|
end
|
71
75
|
|
72
76
|
def register(instance)
|
data/spec/.rubocop.yml
CHANGED
@@ -1,27 +1,27 @@
|
|
1
1
|
# coding: UTF-8
|
2
2
|
require 'spec_helper'
|
3
3
|
|
4
|
-
class Suit < TypesafeEnum::Base
|
4
|
+
class ::Suit < TypesafeEnum::Base
|
5
5
|
new :CLUBS
|
6
6
|
new :DIAMONDS
|
7
7
|
new :HEARTS
|
8
8
|
new :SPADES
|
9
9
|
end
|
10
10
|
|
11
|
-
class Tarot < TypesafeEnum::Base
|
11
|
+
class ::Tarot < TypesafeEnum::Base
|
12
12
|
new :CUPS, 'Cups'
|
13
13
|
new :COINS, 'Coins'
|
14
14
|
new :WANDS, 'Wands'
|
15
15
|
new :SWORDS, 'Swords'
|
16
16
|
end
|
17
17
|
|
18
|
-
class RGBColor < TypesafeEnum::Base
|
18
|
+
class ::RGBColor < TypesafeEnum::Base
|
19
19
|
new :RED, :red
|
20
20
|
new :GREEN, :green
|
21
21
|
new :BLUE, :blue
|
22
22
|
end
|
23
23
|
|
24
|
-
class Scale < TypesafeEnum::Base
|
24
|
+
class ::Scale < TypesafeEnum::Base
|
25
25
|
new :DECA, 10
|
26
26
|
new :HECTO, 100
|
27
27
|
new :KILO, 1_000
|
@@ -31,7 +31,7 @@ end
|
|
31
31
|
module TypesafeEnum
|
32
32
|
describe Base do
|
33
33
|
|
34
|
-
describe '::
|
34
|
+
describe '::new' do
|
35
35
|
it ' news a constant enum value' do
|
36
36
|
enum = Suit::CLUBS
|
37
37
|
expect(enum).to be_a(Suit)
|
@@ -39,7 +39,7 @@ module TypesafeEnum
|
|
39
39
|
|
40
40
|
it 'insists symbols be symbols' do
|
41
41
|
expect do
|
42
|
-
class Cheat < Base
|
42
|
+
class ::Cheat < Base
|
43
43
|
new 'spades', 'spades'
|
44
44
|
end
|
45
45
|
end.to raise_error(TypeError)
|
@@ -47,7 +47,7 @@ module TypesafeEnum
|
|
47
47
|
|
48
48
|
it 'insists symbols be uppercase' do
|
49
49
|
expect do
|
50
|
-
class Cheat < Base
|
50
|
+
class ::Cheat < Base
|
51
51
|
new :spades, 'spades'
|
52
52
|
end
|
53
53
|
end.to raise_error(NameError)
|
@@ -55,24 +55,24 @@ module TypesafeEnum
|
|
55
55
|
|
56
56
|
it 'disallows duplicate symbols' do
|
57
57
|
expect do
|
58
|
-
class Cheat < Base
|
58
|
+
class ::Cheat < Base
|
59
59
|
new :SPADES, 'spades'
|
60
60
|
new :SPADES, 'more spades'
|
61
61
|
end
|
62
62
|
end.to raise_error(NameError)
|
63
63
|
|
64
|
-
expect { Cheat.class }.to raise_error(NameError)
|
64
|
+
expect { ::Cheat.class }.to raise_error(NameError)
|
65
65
|
end
|
66
66
|
|
67
67
|
it 'disallows duplicate values' do
|
68
68
|
expect do
|
69
|
-
class Cheat < Base
|
69
|
+
class ::Cheat < Base
|
70
70
|
new :SPADES, 'spades'
|
71
71
|
new :ALSO_SPADES, 'spades'
|
72
72
|
end
|
73
73
|
end.to raise_error(NameError)
|
74
74
|
|
75
|
-
expect { Cheat.class }.to raise_error(NameError)
|
75
|
+
expect { ::Cheat.class }.to raise_error(NameError)
|
76
76
|
end
|
77
77
|
|
78
78
|
it 'defaults the value to a lower-cased version of the symbol' do
|
@@ -377,7 +377,7 @@ module TypesafeEnum
|
|
377
377
|
end
|
378
378
|
|
379
379
|
it 'supports "inner class" methods via instance_eval' do
|
380
|
-
class Operation < Base
|
380
|
+
class ::Operation < Base
|
381
381
|
new(:PLUS, '+').instance_eval do
|
382
382
|
def eval(x, y)
|
383
383
|
x + y
|
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.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- David Moles
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-12-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|