Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lib/ruby-enum/enum.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ def self.included(base)
base.extend ClassMethods

base.private_class_method(:new)

base.instance_variable_set(:@_enum_hash, {})
base.instance_variable_set(:@_enums_by_value, {})
end

module ClassMethods
Expand Down
29 changes: 29 additions & 0 deletions spec/ruby-enum/enum_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ class FirstSubclass < Colors
class SecondSubclass < FirstSubclass
define :PINK, 'pink'
end

class OtherSecondSubclass < FirstSubclass
include Ruby::Enum

define :MAGENTA, 'magenta'
end

it 'returns an enum value' do
expect(Colors::RED).to eq 'red'
expect(Colors::GREEN).to eq 'green'
Expand Down Expand Up @@ -169,6 +176,12 @@ class SecondSubclass < FirstSubclass
expect(SecondSubclass.values).to eq(%w[red green orange pink])
end
end

context 'when a subclass of a subclass is defined with redundant module inclusion' do
it 'returns all values' do
expect(OtherSecondSubclass.values).to eq(%w[red green orange magenta])
end
end
end

describe '#to_h' do
Expand Down Expand Up @@ -229,6 +242,22 @@ class SecondSubclass < FirstSubclass
end
end

# rubocop:disable Rspec/DescribedClass

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's just rubocop -a; rubocop --auto-gen-config these or disable the rule?

describe 'Reloading enum definition' do
it 'plays nice with lazy loading in Ruby on Rails' do

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: maybe just say "can be lazy reloaded" ... also bald is not a hairstyle :)

class_body = proc do
include Ruby::Enum

define :BALD, 'bald'
end

HairStyles = Class.new(&class_body)

expect { HairStyles.class_eval(&class_body) }.not_to raise_error
end
end
# rubocop:enable Rspec/DescribedClass

describe 'Given a class that has not defined any enums' do
class EmptyEnums
include Ruby::Enum
Expand Down
Loading