Skip to content

Commit cd5cea4

Browse files
authored
Class reloading (#56)
* Fix DuplicateKeyError when the whole class is reloaded * Add test for redundant module inclusion in a subclass * rubocop: disable RSpec/DescribedClass rule + nits * Add test for subclass lazy reloading * Update CHANGELOG.md
1 parent 7b3e255 commit cd5cea4

4 files changed

Lines changed: 62 additions & 1 deletion

File tree

.rubocop.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Metrics/BlockLength:
1111
RSpec/SpecFilePathFormat:
1212
Enabled: false
1313

14-
Style/LineLength:
14+
Layout/LineLength:
1515
Enabled: false
1616

1717
Style/HashEachMethods:
@@ -38,6 +38,9 @@ RSpec/NestedGroups:
3838
RSpec/ExampleLength:
3939
Enabled: false
4040

41+
RSpec/DescribedClass:
42+
Enabled: false
43+
4144
RSpec/MultipleExpectations:
4245
Enabled: false
4346

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
### 1.1.0 (Next)
22

3+
* [#56](https://github.com/dblock/ruby-enum/pull/56): Fix `DuplicateKeyError` when the enum class is reloaded - [@flvrone](https://github.com/flvrone).
34
* [#54](https://github.com/dblock/ruby-enum/pull/54): Add support for Ruby 4.0 - [@dblock](https://github.com/dblock).
45
* [#53](https://github.com/dblock/ruby-enum/pull/53): Replace code climate with coveralls - [@dblock](https://github.com/dblock).
56
* Your contribution here.

lib/ruby-enum/enum.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ def self.included(base)
1919
base.extend ClassMethods
2020

2121
base.private_class_method(:new)
22+
23+
base.instance_variable_set(:@_enum_hash, {})
24+
base.instance_variable_set(:@_enums_by_value, {})
2225
end
2326

2427
module ClassMethods

spec/ruby-enum/enum_spec.rb

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@ class FirstSubclass < Colors
1717
class SecondSubclass < FirstSubclass
1818
define :PINK, 'pink'
1919
end
20+
21+
class OtherSecondSubclass < FirstSubclass
22+
include Ruby::Enum
23+
24+
define :MAGENTA, 'magenta'
25+
end
26+
2027
it 'returns an enum value' do
2128
expect(Colors::RED).to eq 'red'
2229
expect(Colors::GREEN).to eq 'green'
@@ -169,6 +176,12 @@ class SecondSubclass < FirstSubclass
169176
expect(SecondSubclass.values).to eq(%w[red green orange pink])
170177
end
171178
end
179+
180+
context 'when a subclass of a subclass is defined with redundant module inclusion' do
181+
it 'returns all values' do
182+
expect(OtherSecondSubclass.values).to eq(%w[red green orange magenta])
183+
end
184+
end
172185
end
173186

174187
describe '#to_h' do
@@ -229,6 +242,47 @@ class SecondSubclass < FirstSubclass
229242
end
230243
end
231244

245+
describe 'Reloading enum definition' do
246+
it 'can be lazy reloaded' do
247+
class_body = proc do
248+
include Ruby::Enum
249+
250+
define :BUZZ_CUT, 'buzz_cut'
251+
end
252+
253+
hair_styles = Class.new(&class_body)
254+
255+
expect { hair_styles.class_eval(&class_body) }.not_to raise_error
256+
end
257+
258+
context 'when a subclass is defined' do
259+
it 'NEEDS to include Ruby::Enum explicitly to be lazy reloaded' do
260+
hair_styles = Class.new do
261+
include Ruby::Enum
262+
263+
define :BUZZ_CUT, 'buzz_cut'
264+
end
265+
266+
broken_subclass_body = proc do
267+
define :PONYTAIL, 'ponytail'
268+
end
269+
broken_subclass = Class.new(hair_styles, &broken_subclass_body)
270+
expect { broken_subclass.class_eval(&broken_subclass_body) }
271+
.to raise_error Ruby::Enum::Errors::DuplicateKeyError, /PONYTAIL/
272+
273+
subclass_body = proc do
274+
include Ruby::Enum
275+
276+
define :PONYTAIL, 'ponytail'
277+
end
278+
more_hair_styles = Class.new(hair_styles, &subclass_body)
279+
280+
expect { more_hair_styles.class_eval(&subclass_body) }.not_to raise_error
281+
expect(more_hair_styles.values).to eq(%w[buzz_cut ponytail])
282+
end
283+
end
284+
end
285+
232286
describe 'Given a class that has not defined any enums' do
233287
class EmptyEnums
234288
include Ruby::Enum

0 commit comments

Comments
 (0)