Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

values: prepend superclass values #29

Merged
merged 11 commits into from
Jan 29, 2021
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
### 0.8.1 (Next)
### 0.9.0 (Next)

* Your contribution here.
* [#29](https://github.com/dblock/ruby-enum/pull/29): Added superclass values when enumerating with `Ruby::Enum#values` - [@gi](https://github.com/gi).
* [#30](https://github.com/dblock/ruby-enum/pull/30): Default value to key - [@gi](https://github.com/gi).

### 0.8.0 (2020/3/27)
Expand Down
34 changes: 34 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,40 @@ Since keys are unique there is no way to map values to keys using `Colors.value(
Inheriting from a `Ruby::Enum` class, all defined enums in the parent class will be accessible in sub classes as well.
Sub classes can also provide extra enums as usual.

``` ruby
class PrimaryColors
include Ruby::Enum

define :RED, 'RED'
define :GREEN, 'GREEN'
define :BLUE, 'BLUE'
end

class RainbowColors < PrimaryColors
Copy link
Owner

Choose a reason for hiding this comment

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

The example is super clear, this works.

I do have a question. In the scenario with colors inheritance is the wrong object oriented pattern to use. Rainbow colors don't "inherit" primary colors, they are a combination of primary colors and other colors. In this case we would want a module and mixins. Should we allow class PrimaryColors be module PrimaryColors, extend Ruby::Enum, then include PrimaryColors into RainbowColors or something like that? ... for another PR.

Maybe there's a better OOO example for README that we can use in a separate update? WDYT?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yeah. Semantically, it makes more sense to include the PrimaryColors in the RainbowColors class instead of inheriting them.

Though, I see this is a minor implementation detail. Defining an enum doesn't actually create an instance of that class: Colors::RED isn't an instance of Ruby::Enum or Colors. It's a String or Symbol.

There's little difference between inheriting from a class and including a module other than the Ruby semantics. Both options include the base/included class in the class hierarchy.

Regardless, I think this could be an update for another PR.

Copy link
Owner

Choose a reason for hiding this comment

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

100%

define :ORANGE, 'ORANGE'
define :YELLOW, 'YELLOW'
define :INIDGO, 'INIDGO'
define :VIOLET, 'VIOLET'
end
```

``` ruby
RainbowColors::RED # 'RED'
RainbowColors::ORANGE # 'ORANGE'
RainbowColors::YELLOW # 'YELLOW'
RainbowColors::GREEN # 'GREEN'
RainbowColors::BLUE # 'BLUE'
RainbowColors::INIDGO # 'INIDGO'
RainbowColors::VIOLET # 'VIOLET'
```

The `values` class method will enumerate the values from all base classes.

``` ruby
PrimaryColors.values # ['RED', 'GREEN', 'BLUE']
RainbowColors.values # ['RED', 'ORANGE', 'YELLOW', 'GREEN', 'BLUE', 'INIDGO', 'VIOLET']
```

## Contributing

You're encouraged to contribute to this gem. See [CONTRIBUTING](CONTRIBUTING.md) for details.
Expand Down
44 changes: 44 additions & 0 deletions UPGRADING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Upgrading Ruby::Enum

## Upgrading to >= 0.9.0

### Inheritance & `Ruby::Enum.values`

This only applies to classes that inherit from another which is a `Ruby::Enum`.

Prior to version `0.9.0`, the `values` class method would enumerate only the
values defined in the class.

As of version `0.9.0`, the `values` class method enumerates values defined in
the entire class heirarchy, ancestors first.

``` ruby
class PrimaryColors
include Ruby::Enum

define :RED, 'RED'
define :GREEN, 'GREEN'
define :BLUE, 'BLUE'
end

class RainbowColors < PrimaryColors
define :ORANGE, 'ORANGE'
define :YELLOW, 'YELLOW'
define :INIDGO, 'INIDGO'
define :VIOLET, 'VIOLET'
end
```

`gem 'ruby-enum', '< 0.9.0'`

``` ruby
RainbowColors.values # ['ORANGE', 'YELLOW', 'INIDGO', 'VIOLET']
```

`gem 'ruby-enum', '>= 0.9.0'`

``` ruby
RainbowColors.values # ['RED', 'ORANGE', 'YELLOW', 'GREEN', 'BLUE', 'INIDGO', 'VIOLET']
```

See [#29](https://github.com/dblock/ruby-enum/pull/29) for more information.
8 changes: 7 additions & 1 deletion lib/ruby-enum/enum.rb
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,13 @@ def keys

# Returns all enum values.
def values
@_enum_hash.values.map(&:value)
result = @_enum_hash.values.map(&:value)

if superclass < Ruby::Enum
superclass.values + result
else
result
end
end

# Iterate over all enumerated values.
Expand Down
2 changes: 1 addition & 1 deletion lib/ruby-enum/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

module Ruby
module Enum
VERSION = '0.8.1'
VERSION = '0.9.0'
end
end
16 changes: 16 additions & 0 deletions spec/ruby-enum/enum_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,22 @@ class EmptyEnums
expect(subject::ORANGE).to eq 'orange'
expect(subject::PINK).to eq 'pink'
end
gi marked this conversation as resolved.
Show resolved Hide resolved

describe '#values' do
subject { SecondSubclass.values }

it 'contains the values from all of the parent classes' do
expect(subject).to eq(%w[red green orange pink])
end
end
end

describe '#values' do
subject { FirstSubclass.values }

it 'contains the values from the parent class' do
expect(subject).to eq(%w[red green orange])
end
end
end

Expand Down