Skip to content

Commit

Permalink
Added each_key and each_value.
Browse files Browse the repository at this point in the history
  • Loading branch information
dblock committed Mar 27, 2020
1 parent 6e3ef16 commit 36c31c6
Show file tree
Hide file tree
Showing 19 changed files with 105 additions and 26 deletions.
10 changes: 10 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -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
20 changes: 11 additions & 9 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
# 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
# versions of RuboCop, may require this file to be generated again.

# Offense count: 1
# Configuration parameters: CountComments, ExcludedMethods.
# ExcludedMethods: refine
Metrics/BlockLength:
Max: 170
Max: 180

# Offense count: 1
# Configuration parameters: ExpectMatchingDefinition, Regex, IgnoreExecutableScripts, AllowedAcronyms.
Expand All @@ -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:
Expand All @@ -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
2 changes: 0 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ rvm:
- 2.6.5
- 2.4.0
- 2.3.1
- 2.2
- 2.1.1
- ruby-head
- jruby-head

Expand Down
11 changes: 7 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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)

Expand Down
4 changes: 3 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

source 'http://rubygems.org'

gemspec
Expand All @@ -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
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'rubygems'
require 'bundler/gem_tasks'

Expand Down
2 changes: 2 additions & 0 deletions lib/ruby-enum.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'i18n'

require 'ruby-enum/version'
Expand Down
22 changes: 20 additions & 2 deletions lib/ruby-enum/enum.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Ruby
module Enum
attr_reader :key, :value
Expand Down Expand Up @@ -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.
Expand All @@ -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.
Expand All @@ -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]
Expand Down
4 changes: 3 additions & 1 deletion lib/ruby-enum/errors/base.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Ruby
module Enum
module Errors
Expand Down Expand Up @@ -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.
Expand Down
2 changes: 2 additions & 0 deletions lib/ruby-enum/errors/duplicate_key_error.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Ruby
module Enum
module Errors
Expand Down
2 changes: 2 additions & 0 deletions lib/ruby-enum/errors/duplicate_value_error.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Ruby
module Enum
module Errors
Expand Down
2 changes: 2 additions & 0 deletions lib/ruby-enum/errors/uninitialized_constant_error.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Ruby
module Enum
module Errors
Expand Down
4 changes: 3 additions & 1 deletion lib/ruby-enum/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true

module Ruby
module Enum
VERSION = '0.7.2'.freeze
VERSION = '0.8.0'
end
end
2 changes: 2 additions & 0 deletions lib/ruby_enum.rb
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
# frozen_string_literal: true

require 'ruby-enum'
4 changes: 3 additions & 1 deletion ruby-enum.gemspec
Original file line number Diff line number Diff line change
@@ -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|
Expand Down
22 changes: 17 additions & 5 deletions spec/ruby-enum/enum_spec.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'spec_helper'

class Colors
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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' }
Expand Down
2 changes: 2 additions & 0 deletions spec/ruby-enum/version_spec.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'spec_helper'

describe Ruby::Enum do
Expand Down
2 changes: 2 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))

require 'rubygems'
Expand Down

0 comments on commit 36c31c6

Please sign in to comment.