ruby-enum 0.5.0 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9c0c38cc0b3edc3052b171b5dc3aacdee8a3e2f0
4
- data.tar.gz: e561a122f1dcdc6b5657d1d004d37d37a00e8108
3
+ metadata.gz: 4fc128cc7839208ced2d3cf393b21c82f3ebdfe6
4
+ data.tar.gz: ce5e04b5b1d5e9756a4b4f761bb1d8f127d9e24c
5
5
  SHA512:
6
- metadata.gz: 474384b04e35b3ab7e0fb5145cb07c545c12f8dd37df44c1c0c75d465439df575f4aeeb82e0ca2cc71b55dea25af610c08b2b57fcb8760abb5b9cfc40e9ca6ab
7
- data.tar.gz: e98254d96a8f3f1af22f1ee3c34ca5d2de116724a2cae4f4ab8ba337a05cd2bf78397a6ae3bbf0a2771f3b76a915865b1cbf8ccdd3b8cfe97ffe84bbae7abe92
6
+ metadata.gz: 56ad33d14d0cf9e59f2237387e79422ce7b4cfbaf65fa47162bf1d60e6e3efeb843e321b0fa56f69059bfcef0506f4b6f3faab5d0168cdc756f8ef6ca760f291
7
+ data.tar.gz: 4a5a8d5bf5b64eeb155f9d3f8b20d271e8d752dc52287e9623efff4db795ed23424cf084545946220b89724c3ac7882205c38bb604b98f79e0bf160f0e82455a
@@ -1,3 +1,7 @@
1
+ ### 0.6.0 (12/5/2016)
2
+
3
+ * [#12](https://github.com/dblock/ruby-enum/pull/12): A `Ruby::Enum::Errors::DuplicateKeyError` or a `Ruby::Enum::Errors::DuplciateKeyValyeError` will now be raised when duplicate keys / values are defined - [@laertispappas](https://github.com/laertispappas).
4
+
1
5
  ### 0.5.0 (11/20/2015)
2
6
 
3
7
  * [#8](https://github.com/dblock/ruby-enum/pull/8): Added `Ruby::Enum#key`, `Ruby::Enum#value`, `Ruby::Enum#key?`, and `Ruby::Enum#value?` - [@dmolesUC3](https://github.com/dmolesUC3).
@@ -1,35 +1,35 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- ruby-enum (0.5.0)
4
+ ruby-enum (0.6.0)
5
5
  i18n
6
6
 
7
7
  GEM
8
8
  remote: http://rubygems.org/
9
9
  specs:
10
- ast (2.1.0)
10
+ ast (2.3.0)
11
11
  astrolabe (1.3.1)
12
12
  parser (~> 2.2)
13
13
  diff-lcs (1.2.5)
14
14
  i18n (0.7.0)
15
- parser (2.2.3.0)
16
- ast (>= 1.1, < 3.0)
15
+ parser (2.3.3.1)
16
+ ast (~> 2.2)
17
17
  powerpack (0.1.1)
18
- rainbow (2.0.0)
19
- rake (10.3.1)
18
+ rainbow (2.1.0)
19
+ rake (11.3.0)
20
20
  rspec (3.4.0)
21
21
  rspec-core (~> 3.4.0)
22
22
  rspec-expectations (~> 3.4.0)
23
23
  rspec-mocks (~> 3.4.0)
24
- rspec-core (3.4.1)
24
+ rspec-core (3.4.4)
25
25
  rspec-support (~> 3.4.0)
26
26
  rspec-expectations (3.4.0)
27
27
  diff-lcs (>= 1.2.0, < 2.0)
28
28
  rspec-support (~> 3.4.0)
29
- rspec-mocks (3.4.0)
29
+ rspec-mocks (3.4.1)
30
30
  diff-lcs (>= 1.2.0, < 2.0)
31
31
  rspec-support (~> 3.4.0)
32
- rspec-support (3.4.0)
32
+ rspec-support (3.4.1)
33
33
  rubocop (0.35.1)
34
34
  astrolabe (~> 1.3)
35
35
  parser (>= 2.2.3.0, < 3.0)
@@ -37,7 +37,7 @@ GEM
37
37
  rainbow (>= 1.99.1, < 3.0)
38
38
  ruby-progressbar (~> 1.7)
39
39
  tins (<= 1.6.0)
40
- ruby-progressbar (1.7.5)
40
+ ruby-progressbar (1.8.1)
41
41
  tins (1.6.0)
42
42
 
43
43
  PLATFORMS
@@ -50,4 +50,4 @@ DEPENDENCIES
50
50
  ruby-enum!
51
51
 
52
52
  BUNDLED WITH
53
- 1.10.6
53
+ 1.13.6
data/README.md CHANGED
@@ -120,6 +120,33 @@ Colors.key('yellow')
120
120
  # => nil
121
121
  ```
122
122
 
123
+
124
+ ## Duplicate enumerator keys or duplicate values
125
+
126
+ Defining duplicate enums will raise a `Ruby::Enum::Errors::DuplicateKeyError`. Moreover a duplicate
127
+ value is not allowed. Defining a duplicate value will raise a `Ruby::Enum::Errors::DuplicateKeyError`.
128
+ The following declarations will both raise an exception:
129
+
130
+ ```ruby
131
+ class Colors
132
+ include Ruby::Enum
133
+
134
+ define :RED, "red"
135
+ define :RED, "my red" # will raise a DuplicateKeyError exception
136
+ end
137
+
138
+ # The following will raise a DuplicateValueError
139
+ class Colots
140
+ include Ruby::Enum
141
+
142
+ define :RED, 'red'
143
+ define :SOME, 'red' # Boom
144
+ end
145
+ ```
146
+
147
+ The `DuplicateValueError` exception is thrown to be consistent with the unique key constraint.
148
+ Since keys are unique there is no way to map values to keys using `Colors.key('red')`
149
+
123
150
  ## Contributing
124
151
 
125
152
  You're encouraged to contribute to this gem. See [CONTRIBUTING](CONTRIBUTING.md) for details.
@@ -12,3 +12,11 @@ en:
12
12
  \_\_\_include Ruby::Enum\n
13
13
  \_\_\_define %{key}, 'value'\n
14
14
  \_\_end"
15
+ duplicate_key:
16
+ message: 'Duplicate key.'
17
+ summary: "The constant %{name}::%{key} has already been defined."
18
+ resolution: "The enumerated key is already defined in class %{name}%. Please use a different name to declare it."
19
+ duplicate_value:
20
+ message: 'Duplicate value found.'
21
+ summary: "The value %{value} has already been defined in %{name}%."
22
+ resolution: "The enumerated value %{value} is already defined in class %{name}%. Please use a different value."
@@ -7,3 +7,5 @@ I18n.load_path << File.join(File.dirname(__FILE__), 'config', 'locales', 'en.yml
7
7
 
8
8
  require 'ruby-enum/errors/base'
9
9
  require 'ruby-enum/errors/uninitialized_constant_error'
10
+ require 'ruby-enum/errors/duplicate_key_error'
11
+ require 'ruby-enum/errors/duplicate_value_error'
@@ -21,11 +21,27 @@ module Ruby
21
21
  def define(key, value)
22
22
  @_enum_hash ||= {}
23
23
  @_enums_by_value ||= {}
24
+
25
+ validate_key!(key)
26
+ validate_value!(value)
27
+
24
28
  new_instance = new(key, value)
25
29
  @_enum_hash[key] = new_instance
26
30
  @_enums_by_value[value] = new_instance
27
31
  end
28
32
 
33
+ def validate_key!(key)
34
+ return unless @_enum_hash.key?(key)
35
+
36
+ fail Ruby::Enum::Errors::DuplicateKeyError, name: name, key: key
37
+ end
38
+
39
+ def validate_value!(value)
40
+ return unless @_enums_by_value.key?(value)
41
+
42
+ fail Ruby::Enum::Errors::DuplicateValueError, name: name, value: value
43
+ end
44
+
29
45
  def const_missing(key)
30
46
  if @_enum_hash[key]
31
47
  @_enum_hash[key].value
@@ -0,0 +1,12 @@
1
+ module Ruby
2
+ module Enum
3
+ module Errors
4
+ # Error raised when a duplicate enum key is found
5
+ class DuplicateKeyError < Base
6
+ def initialize(attrs)
7
+ super(compose_message('duplicate_key', attrs))
8
+ end
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,12 @@
1
+ module Ruby
2
+ module Enum
3
+ module Errors
4
+ # Error raised when a duplicate enum value is found
5
+ class DuplicateValueError < Base
6
+ def initialize(attrs)
7
+ super(compose_message('duplicate_value', attrs))
8
+ end
9
+ end
10
+ end
11
+ end
12
+ end
@@ -1,5 +1,5 @@
1
1
  module Ruby
2
2
  module Enum
3
- VERSION = '0.5.0'
3
+ VERSION = '0.6.0'
4
4
  end
5
5
  end
@@ -109,4 +109,24 @@ describe Ruby::Enum do
109
109
  expect(Colors.to_h).to eq(RED: 'red', GREEN: 'green')
110
110
  end
111
111
  end
112
+
113
+ context 'on duplicate keys' do
114
+ it 'raises DuplicateKeyError' do
115
+ expect do
116
+ Colors.class_eval do
117
+ define :RED, 'some'
118
+ end
119
+ end.to raise_error Ruby::Enum::Errors::DuplicateKeyError, /The constant Colors::RED has already been defined./
120
+ end
121
+ end
122
+
123
+ context 'on duplicate values' do
124
+ it 'raises a DuplicateValueError' do
125
+ expect do
126
+ Colors.class_eval do
127
+ define :Other, 'red'
128
+ end
129
+ end.to raise_error Ruby::Enum::Errors::DuplicateValueError, /The value red has already been defined./
130
+ end
131
+ end
112
132
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby-enum
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Doubrovkine
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-11-20 00:00:00.000000000 Z
11
+ date: 2016-12-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: i18n
@@ -42,11 +42,11 @@ files:
42
42
  - lib/ruby-enum.rb
43
43
  - lib/ruby-enum/enum.rb
44
44
  - lib/ruby-enum/errors/base.rb
45
+ - lib/ruby-enum/errors/duplicate_key_error.rb
46
+ - lib/ruby-enum/errors/duplicate_value_error.rb
45
47
  - lib/ruby-enum/errors/uninitialized_constant_error.rb
46
48
  - lib/ruby-enum/version.rb
47
49
  - lib/ruby_enum.rb
48
- - pkg/ruby-enum-0.3.0.gem
49
- - pkg/ruby-enum-0.4.0.gem
50
50
  - ruby-enum.gemspec
51
51
  - spec/ruby-enum/enum_spec.rb
52
52
  - spec/ruby-enum/version_spec.rb
@@ -71,7 +71,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
71
71
  version: 1.3.6
72
72
  requirements: []
73
73
  rubyforge_project:
74
- rubygems_version: 2.4.8
74
+ rubygems_version: 2.5.1
75
75
  signing_key:
76
76
  specification_version: 4
77
77
  summary: Enum-like behavior for Ruby.
Binary file
Binary file