dblock / ruby-enum

A handy way to define enums in Ruby.
MIT License
178 stars 23 forks source link

Support subclassing an Enum #3

Closed afeld closed 7 years ago

afeld commented 11 years ago

if it's supported: add some tests for it; if not: raise an error when trying to subclass, or when trying to call .define from the subclass

dblock commented 11 years ago

I think it's not supported, but it should be a feature request. I believe subclassing should make it into two separate enums with their own values.

laertispappas commented 7 years ago

This is not supported. What should the default behavior be for sub classes? Based on your comment above I understand that a class of type S that inherits from T should have the same enums defined in T correct?

class T
  define :RED, 'red'
  define :GREEN, 'green'  
end

class S < T
  define :BLACK, 'black'
end

S::RED         # => 'red'
S::GREEN   # => 'green'
S::BLACK   # => 'black'
dblock commented 7 years ago

Yes @laertispappas. I think this is correct, and in your exaple T::BLACK should obviously not be a thing.

laertispappas commented 7 years ago

Great. Any propose method on how to implement this? I'm about to make a PR to support this if it's OK and no one is working on this.

I have faced the same issue on a gem we maintain with the same behavior. We were "losing" the functionality on sub classes because the behavior was defined on self.included method. In order to bypass this I'm thinking of implementing the self.inherited method and provide all the functionality to subclasses. What do you think about it?

dblock commented 7 years ago

I think anything that works that doesn't break existing functionality works! ;)

laertispappas commented 7 years ago

:)

dblock commented 7 years ago

Implemented via https://github.com/dblock/ruby-enum/pull/13.