From da7f5565ed77c04fc094cc628af5cdb2f62842e8 Mon Sep 17 00:00:00 2001 From: dblock Date: Fri, 27 Mar 2020 09:33:00 -0400 Subject: [PATCH] Added each_key and each_value. --- .rubocop.yml | 10 +++++++++ .rubocop_todo.yml | 20 +++++++++-------- .travis.yml | 2 -- CHANGELOG.md | 11 ++++++---- Gemfile | 4 +++- README.md | 12 ++++++++++ Rakefile | 2 ++ lib/ruby-enum.rb | 2 ++ lib/ruby-enum/enum.rb | 22 +++++++++++++++++-- lib/ruby-enum/errors/base.rb | 4 +++- lib/ruby-enum/errors/duplicate_key_error.rb | 2 ++ lib/ruby-enum/errors/duplicate_value_error.rb | 2 ++ .../errors/uninitialized_constant_error.rb | 2 ++ lib/ruby-enum/version.rb | 4 +++- lib/ruby_enum.rb | 2 ++ ruby-enum.gemspec | 4 +++- spec/ruby-enum/enum_spec.rb | 22 ++++++++++++++----- spec/ruby-enum/version_spec.rb | 2 ++ spec/spec_helper.rb | 2 ++ 19 files changed, 105 insertions(+), 26 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index 04b2216..c9fe898 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -1,5 +1,15 @@ AllCops: + TargetRubyVersion: 2.3 Exclude: - vendor/**/* +Style/HashEachMethods: + Enabled: true + +Style/HashTransformKeys: + Enabled: true + +Style/HashTransformValues: + Enabled: true + inherit_from: .rubocop_todo.yml diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 119f27f..fb39a03 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -1,6 +1,6 @@ # This configuration was generated by # `rubocop --auto-gen-config` -# on 2018-02-15 19:02:43 -0500 using RuboCop version 0.52.1. +# on 2020-03-27 09:23:34 -0400 using RuboCop version 0.80.1. # The point is for the user to remove these configuration records # one by one as the offenses are removed from the code base. # Note that changes in the inspected code, or installation of new @@ -8,8 +8,9 @@ # Offense count: 1 # Configuration parameters: CountComments, ExcludedMethods. +# ExcludedMethods: refine Metrics/BlockLength: - Max: 170 + Max: 180 # Offense count: 1 # Configuration parameters: ExpectMatchingDefinition, Regex, IgnoreExecutableScripts, AllowedAcronyms. @@ -18,12 +19,12 @@ Naming/FileName: Exclude: - 'lib/ruby-enum.rb' -# Offense count: 3 -# Cop supports --auto-correct. -# Configuration parameters: AutoCorrect. -Performance/HashEachMethods: +# Offense count: 6 +# Configuration parameters: MinNameLength, AllowNamesEndingInNumbers, AllowedNames, ForbiddenNames. +# AllowedNames: io, id, to, by, on, in, at, ip, db, os, pp +Naming/MethodParameterName: Exclude: - - 'spec/ruby-enum/enum_spec.rb' + - 'lib/ruby-enum/enum.rb' # Offense count: 4 Style/Documentation: @@ -35,7 +36,8 @@ Style/Documentation: - 'lib/ruby-enum/errors/uninitialized_constant_error.rb' # Offense count: 10 -# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns. +# Cop supports --auto-correct. +# Configuration parameters: AutoCorrect, AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns. # URISchemes: http, https -Metrics/LineLength: +Layout/LineLength: Max: 148 diff --git a/.travis.yml b/.travis.yml index eddb63b..d355d4f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -9,8 +9,6 @@ rvm: - 2.6.5 - 2.4.0 - 2.3.1 - - 2.2 - - 2.1.1 - ruby-head - jruby-head diff --git a/CHANGELOG.md b/CHANGELOG.md index 4f5dd7b..1724e72 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,18 +1,21 @@ -### 0.7.3 (Next) +### 0.8.0 (Next) +* [#22](https://github.com/dblock/ruby-enum/pull/22): Added `Ruby::Enum#each_key` and `Ruby::Enum#each_value` - [@dblock](https://github.com/dblock). +* [#22](https://github.com/dblock/ruby-enum/pull/22): Dropped support for Ruby 2.2 - [@dblock](https://github.com/dblock). +* [#22](https://github.com/dblock/ruby-enum/pull/22): Upgraded RuboCop to 0.80.1 - [@dblock](https://github.com/dblock). * Your contribution here. ### 0.7.2 (15/3/2017) -* [#18](https://github.com/dblock/ruby-enum/pull/18): Adds support for non constant definition - [@laertispappas](https://github.com/laertispappas). +* [#18](https://github.com/dblock/ruby-enum/pull/18): Added support for non constant definition - [@laertispappas](https://github.com/laertispappas). ### 0.7.1 (23/2/2017) -* [#16](https://github.com/dblock/ruby-enum/pull/16): Replaces const_missing method in favor of const_set - [@laertispappas](https://github.com/laertispappas). +* [#16](https://github.com/dblock/ruby-enum/pull/16): Replaced `const_missing` with `const_set` - [@laertispappas](https://github.com/laertispappas). ### 0.7.0 (21/2/2017) -* [#3](https://github.com/dblock/ruby-enum/pull/13): Adds support for sub classing an Enum - [@laertispappas](https://github.com/laertispappas). +* [#3](https://github.com/dblock/ruby-enum/pull/13): Added support for subclassing an Enum - [@laertispappas](https://github.com/laertispappas). ### 0.6.0 (12/5/2016) diff --git a/Gemfile b/Gemfile index aa08fe5..7089426 100644 --- a/Gemfile +++ b/Gemfile @@ -1,3 +1,5 @@ +# frozen_string_literal: true + source 'http://rubygems.org' gemspec @@ -6,5 +8,5 @@ gem 'rake' group :development, :test do gem 'rspec', '~> 3.0' - gem 'rubocop', '0.52.1' + gem 'rubocop', '0.80.1' end diff --git a/README.md b/README.md index 9bfb9c8..ebe23d2 100644 --- a/README.md +++ b/README.md @@ -68,6 +68,18 @@ Colors.each do |key, enum| end ``` +``` ruby +Colors.each_key do |key| + # :RED, :GREEN +end +``` + +``` ruby +Colors.each_value do |value| + # "red", "green" +end +``` + #### Mapping ``` ruby diff --git a/Rakefile b/Rakefile index bc7afd6..dd0ed12 100644 --- a/Rakefile +++ b/Rakefile @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'rubygems' require 'bundler/gem_tasks' diff --git a/lib/ruby-enum.rb b/lib/ruby-enum.rb index 8dbd62f..855c8ee 100644 --- a/lib/ruby-enum.rb +++ b/lib/ruby-enum.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'i18n' require 'ruby-enum/version' diff --git a/lib/ruby-enum/enum.rb b/lib/ruby-enum/enum.rb index 7cf74f2..fccabf1 100644 --- a/lib/ruby-enum/enum.rb +++ b/lib/ruby-enum/enum.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module Ruby module Enum attr_reader :key, :value @@ -85,7 +87,7 @@ def key?(k) # Returns the corresponding enum instance or nil. def value(k) enum = @_enum_hash[k] - enum.value if enum + enum&.value end # Whether the specified value exists in this enum. @@ -106,7 +108,7 @@ def value?(v) # Returns the corresponding key symbol or nil. def key(v) enum = @_enums_by_value[v] - enum.key if enum + enum&.key end # Returns all enum keys. @@ -119,6 +121,22 @@ def values @_enum_hash.values.map(&:value) end + # Iterate over all enumerated values. + # Required for Enumerable mixin + def each_value(&_block) + @_enum_hash.each_value do |v| + yield v.value + end + end + + # Iterate over all enumerated keys. + # Required for Enumerable mixin + def each_key(&_block) + @_enum_hash.each_value do |v| + yield v.key + end + end + def to_h Hash[@_enum_hash.map do |key, enum| [key, enum.value] diff --git a/lib/ruby-enum/errors/base.rb b/lib/ruby-enum/errors/base.rb index 9a2357d..01daa10 100644 --- a/lib/ruby-enum/errors/base.rb +++ b/lib/ruby-enum/errors/base.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module Ruby module Enum module Errors @@ -26,7 +28,7 @@ def compose_message(key, attributes = {}) private - BASE_KEY = 'ruby.enum.errors.messages'.freeze #:nodoc: + BASE_KEY = 'ruby.enum.errors.messages' #:nodoc: # Given the key of the specific error and the options hash, translate the # message. diff --git a/lib/ruby-enum/errors/duplicate_key_error.rb b/lib/ruby-enum/errors/duplicate_key_error.rb index b87b899..64b0bd7 100644 --- a/lib/ruby-enum/errors/duplicate_key_error.rb +++ b/lib/ruby-enum/errors/duplicate_key_error.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module Ruby module Enum module Errors diff --git a/lib/ruby-enum/errors/duplicate_value_error.rb b/lib/ruby-enum/errors/duplicate_value_error.rb index 420407c..b3be267 100644 --- a/lib/ruby-enum/errors/duplicate_value_error.rb +++ b/lib/ruby-enum/errors/duplicate_value_error.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module Ruby module Enum module Errors diff --git a/lib/ruby-enum/errors/uninitialized_constant_error.rb b/lib/ruby-enum/errors/uninitialized_constant_error.rb index b5fff8e..42200c7 100644 --- a/lib/ruby-enum/errors/uninitialized_constant_error.rb +++ b/lib/ruby-enum/errors/uninitialized_constant_error.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module Ruby module Enum module Errors diff --git a/lib/ruby-enum/version.rb b/lib/ruby-enum/version.rb index 9e206bb..ef51c17 100644 --- a/lib/ruby-enum/version.rb +++ b/lib/ruby-enum/version.rb @@ -1,5 +1,7 @@ +# frozen_string_literal: true + module Ruby module Enum - VERSION = '0.7.2'.freeze + VERSION = '0.8.0' end end diff --git a/lib/ruby_enum.rb b/lib/ruby_enum.rb index abda069..c1af8f6 100644 --- a/lib/ruby_enum.rb +++ b/lib/ruby_enum.rb @@ -1 +1,3 @@ +# frozen_string_literal: true + require 'ruby-enum' diff --git a/ruby-enum.gemspec b/ruby-enum.gemspec index a244390..9eee0bd 100644 --- a/ruby-enum.gemspec +++ b/ruby-enum.gemspec @@ -1,4 +1,6 @@ -$LOAD_PATH.push File.expand_path('../lib', __FILE__) +# frozen_string_literal: true + +$LOAD_PATH.push File.expand_path('lib', __dir__) require 'ruby-enum/version' Gem::Specification.new do |s| diff --git a/spec/ruby-enum/enum_spec.rb b/spec/ruby-enum/enum_spec.rb index 370db61..2bca1d9 100644 --- a/spec/ruby-enum/enum_spec.rb +++ b/spec/ruby-enum/enum_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' class Colors @@ -63,8 +65,13 @@ class SecondSubclass < FirstSubclass end end context '#key?' do - it 'returns true for valid keys' do - Colors.keys.each do |key| + it 'returns true for valid keys accessed directly' do + Colors.keys.each do |key| # rubocop:disable Style/HashEachMethods + expect(Colors.key?(key)).to eq(true) + end + end + it 'returns true for valid keys accessed via each_keys' do + Colors.each_key do |key| expect(Colors.key?(key)).to eq(true) end end @@ -83,8 +90,13 @@ class SecondSubclass < FirstSubclass end end context '#value?' do - it 'returns true for valid values' do - Colors.values.each do |value| + it 'returns true for valid values accessed directly' do + Colors.values.each do |value| # rubocop:disable Style/HashEachMethods + expect(Colors.value?(value)).to eq(true) + end + end + it 'returns true for valid values accessed via each_value' do + Colors.each_value do |value| expect(Colors.value?(value)).to eq(true) end end @@ -149,7 +161,7 @@ class EmptyEnums context 'when a constant is redefined in a global namespace' do before do - RED = 'black'.freeze + RED = 'black' end it { expect(Colors::RED).to eq 'red' } diff --git a/spec/ruby-enum/version_spec.rb b/spec/ruby-enum/version_spec.rb index 7e86adc..e32d337 100644 --- a/spec/ruby-enum/version_spec.rb +++ b/spec/ruby-enum/version_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' describe Ruby::Enum do diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index ac980fe..1be2c76 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib')) require 'rubygems'