From 0a9bed382f6eb92fd78e85e9c48315802c44528d Mon Sep 17 00:00:00 2001 From: Zach Gianos Date: Tue, 26 Jan 2021 20:39:01 -0800 Subject: [PATCH 01/10] values: prepend superclass values When enumerating values for a subclass, the superclass values are prepended. Fixes #27. --- .rubocop_todo.yml | 2 +- lib/ruby-enum/enum.rb | 5 ++++- spec/ruby-enum/enum_spec.rb | 8 ++++++++ 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index fb39a03..6a1d4fe 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -10,7 +10,7 @@ # Configuration parameters: CountComments, ExcludedMethods. # ExcludedMethods: refine Metrics/BlockLength: - Max: 180 + Max: 200 # Offense count: 1 # Configuration parameters: ExpectMatchingDefinition, Regex, IgnoreExecutableScripts, AllowedAcronyms. diff --git a/lib/ruby-enum/enum.rb b/lib/ruby-enum/enum.rb index fccabf1..86d805d 100644 --- a/lib/ruby-enum/enum.rb +++ b/lib/ruby-enum/enum.rb @@ -118,7 +118,10 @@ def keys # Returns all enum values. def values - @_enum_hash.values.map(&:value) + values = @_enum_hash.values.map(&:value) + superclass.values + values + rescue NoMethodError + values end # Iterate over all enumerated values. diff --git a/spec/ruby-enum/enum_spec.rb b/spec/ruby-enum/enum_spec.rb index 2bca1d9..8417003 100644 --- a/spec/ruby-enum/enum_spec.rb +++ b/spec/ruby-enum/enum_spec.rb @@ -188,6 +188,14 @@ class EmptyEnums expect(subject::PINK).to eq 'pink' end end + + describe '#values' do + subject { FirstSubclass.values } + + it 'contains the enums in the parent class' do + expect(subject).to eq(%w[red green orange]) + end + end end describe 'non constant definitions' do From aa8415eefcb7459719d60da1823524c15d611b36 Mon Sep 17 00:00:00 2001 From: Zach Gianos Date: Wed, 27 Jan 2021 10:23:16 -0800 Subject: [PATCH 02/10] values: add specs to test more superclass levels --- spec/ruby-enum/enum_spec.rb | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/spec/ruby-enum/enum_spec.rb b/spec/ruby-enum/enum_spec.rb index 8417003..12e556c 100644 --- a/spec/ruby-enum/enum_spec.rb +++ b/spec/ruby-enum/enum_spec.rb @@ -187,12 +187,20 @@ class EmptyEnums expect(subject::ORANGE).to eq 'orange' expect(subject::PINK).to eq 'pink' end + + 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 enums in the parent class' do + it 'contains the values from the parent class' do expect(subject).to eq(%w[red green orange]) end end From cb8f9de8b3635e3556c1e375e36d36bd693ca928 Mon Sep 17 00:00:00 2001 From: Zach Gianos Date: Wed, 27 Jan 2021 09:56:10 -0800 Subject: [PATCH 03/10] rubocop: exclude spec from Metrics/BlockLength The spec file violates the Metrics/BlockLength cop and always needs to be updated. This excludes the file from being evaluated against that rule so that other blocks may still adhere to a normal block length configuration. --- .rubocop.yml | 4 ++++ .rubocop_todo.yml | 6 ------ 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index c9fe898..08c82d3 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -3,6 +3,10 @@ AllCops: Exclude: - vendor/**/* +Metrics/BlockLength: + Exclude: + - 'spec/**/*_spec.rb' + Style/HashEachMethods: Enabled: true diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 6a1d4fe..81fac83 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -6,12 +6,6 @@ # Note that changes in the inspected code, or installation of new # versions of RuboCop, may require this file to be generated again. -# Offense count: 1 -# Configuration parameters: CountComments, ExcludedMethods. -# ExcludedMethods: refine -Metrics/BlockLength: - Max: 200 - # Offense count: 1 # Configuration parameters: ExpectMatchingDefinition, Regex, IgnoreExecutableScripts, AllowedAcronyms. # AllowedAcronyms: CLI, DSL, ACL, API, ASCII, CPU, CSS, DNS, EOF, GUID, HTML, HTTP, HTTPS, ID, IP, JSON, LHS, QPS, RAM, RHS, RPC, SLA, SMTP, SQL, SSH, TCP, TLS, TTL, UDP, UI, UID, UUID, URI, URL, UTF8, VM, XML, XMPP, XSRF, XSS From 35a2d7b12e8e1bf68a861981ccf6105dc6eedcb3 Mon Sep 17 00:00:00 2001 From: Zach Gianos Date: Wed, 27 Jan 2021 10:34:39 -0800 Subject: [PATCH 04/10] values: replace rescue with type check on superclass call --- lib/ruby-enum/enum.rb | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/lib/ruby-enum/enum.rb b/lib/ruby-enum/enum.rb index 86d805d..e4552a3 100644 --- a/lib/ruby-enum/enum.rb +++ b/lib/ruby-enum/enum.rb @@ -119,9 +119,12 @@ def keys # Returns all enum values. def values values = @_enum_hash.values.map(&:value) - superclass.values + values - rescue NoMethodError - values + + if superclass < Ruby::Enum + superclass.values + values + else + values + end end # Iterate over all enumerated values. From 15442541543ed31aa47b340cc3ef787229013dcc Mon Sep 17 00:00:00 2001 From: Zach Gianos Date: Wed, 27 Jan 2021 10:56:50 -0800 Subject: [PATCH 05/10] readme/changlog: updated with `values` superclass behavior --- CHANGELOG.md | 4 ++++ README.md | 25 +++++++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 20573ad..0795796 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +### 0.9.0 + +* [#29](https://github.com/dblock/ruby-enum/pull/29): Added superclass values when enumerating with `Ruby::Enum#values` - [@gi](https://github.com/gi). + ### 0.8.1 (Next) * Your contribution here. diff --git a/README.md b/README.md index ebe23d2..a905a15 100644 --- a/README.md +++ b/README.md @@ -192,6 +192,31 @@ 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 A + include Ruby::Enum + + define :T, 'T' +end + +class B < A + define :U, 'U' +end +``` + +``` ruby +A::T # 'T' +B::T # 'T' +B::U # 'U' +``` + +The `values` class method will enumerate the values from all base classes. + +``` ruby +A.values # ['T'] +B.values # ['T', 'U'] +``` + ## Contributing You're encouraged to contribute to this gem. See [CONTRIBUTING](CONTRIBUTING.md) for details. From da79681eb9f421029e77368c18f7671fb3f9700b Mon Sep 17 00:00:00 2001 From: Zach Gianos Date: Wed, 27 Jan 2021 13:34:34 -0800 Subject: [PATCH 06/10] changelog: subsume 0.8.1 into 0.9.0 --- CHANGELOG.md | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 31abb46..ee3ac9c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,10 +1,7 @@ -### 0.9.0 - -* [#29](https://github.com/dblock/ruby-enum/pull/29): Added superclass values when enumerating with `Ruby::Enum#values` - [@gi](https://github.com/gi). - -### 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) From 7bac3c1f60c15723caba8272c3f4fe1a97855bda Mon Sep 17 00:00:00 2001 From: Zach Gianos Date: Wed, 27 Jan 2021 13:47:55 -0800 Subject: [PATCH 07/10] readme: update inheritance behavior examples --- README.md | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index db490df..1388992 100644 --- a/README.md +++ b/README.md @@ -211,28 +211,37 @@ Inheriting from a `Ruby::Enum` class, all defined enums in the parent class will Sub classes can also provide extra enums as usual. ``` ruby -class A +class PrimaryColors include Ruby::Enum - define :T, 'T' + define :RED, 'RED' + define :GREEN, 'GREEN' + define :BLUE, 'BLUE' end -class B < A - define :U, 'U' +class RainbowColors < PrimaryColors + define :ORANGE, 'ORANGE' + define :YELLOW, 'YELLOW' + define :INIDGO, 'INIDGO' + define :VIOLET, 'VIOLET' end ``` ``` ruby -A::T # 'T' -B::T # 'T' -B::U # 'U' +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 -A.values # ['T'] -B.values # ['T', 'U'] +PrimaryColors.values # ['RED', 'GREEN', 'BLUE'] +RainbowColors.values # ['RED', 'ORANGE', 'YELLOW', 'GREEN', 'BLUE', 'INIDGO', 'VIOLET'] ``` ## Contributing From 66738357d00415b2c4bd4bfaa3262bed747b67d9 Mon Sep 17 00:00:00 2001 From: Zach Gianos Date: Wed, 27 Jan 2021 13:49:59 -0800 Subject: [PATCH 08/10] values: rename result variable to not shadow method name --- lib/ruby-enum/enum.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/ruby-enum/enum.rb b/lib/ruby-enum/enum.rb index 3b9a0df..f7abb4c 100644 --- a/lib/ruby-enum/enum.rb +++ b/lib/ruby-enum/enum.rb @@ -118,12 +118,12 @@ def keys # Returns all enum values. def values - values = @_enum_hash.values.map(&:value) + result = @_enum_hash.values.map(&:value) if superclass < Ruby::Enum - superclass.values + values + superclass.values + result else - values + result end end From 5031e6f0474747588aff757bec9a374f09f2157b Mon Sep 17 00:00:00 2001 From: Zach Gianos Date: Wed, 27 Jan 2021 14:00:17 -0800 Subject: [PATCH 09/10] version: bump to 0.9.0 --- lib/ruby-enum/version.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/ruby-enum/version.rb b/lib/ruby-enum/version.rb index 3d1fc4c..1a3c94b 100644 --- a/lib/ruby-enum/version.rb +++ b/lib/ruby-enum/version.rb @@ -2,6 +2,6 @@ module Ruby module Enum - VERSION = '0.8.1' + VERSION = '0.9.0' end end From 0c0e4a077f4ec8f9cde66963715d10900b6850ff Mon Sep 17 00:00:00 2001 From: Zach Gianos Date: Thu, 28 Jan 2021 10:37:48 -0800 Subject: [PATCH 10/10] upgrading: add new file with upgrading implementation details --- UPGRADING.md | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 UPGRADING.md diff --git a/UPGRADING.md b/UPGRADING.md new file mode 100644 index 0000000..1a25311 --- /dev/null +++ b/UPGRADING.md @@ -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.