-
Notifications
You must be signed in to change notification settings - Fork 23
Add exhaustive case matcher #43
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
Merged
dblock
merged 4 commits into
dblock:master
from
peterfication:add-exhaustive-case-statement
Jan 7, 2024
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib')) | ||
|
|
||
| require 'benchmark' | ||
| require 'ruby-enum' | ||
|
|
||
| ## | ||
| # Test enum | ||
| class Color | ||
| include Ruby::Enum | ||
| include Ruby::Enum::Case | ||
|
|
||
| define :RED, :red | ||
| define :GREEN, :green | ||
| define :BLUE, :blue | ||
| end | ||
|
|
||
| puts 'Running 1.000.000 normal case statements' | ||
| case_statement_time = Benchmark.realtime do | ||
| 1_000_000.times do | ||
| case Color::RED | ||
| when Color::RED, Color::GREEN | ||
| 'red or green' | ||
| when Color::BLUE | ||
| 'blue' | ||
| end | ||
| end | ||
| end | ||
|
|
||
| puts 'Running 1.000.000 ruby-enum case statements' | ||
| ruby_enum_time = Benchmark.realtime do | ||
| 1_000_000.times do | ||
| Color.case(Color::RED, | ||
| { | ||
| [Color::RED, Color::GREEN] => -> { 'red or green' }, | ||
| Color::BLUE => -> { 'blue' } | ||
| }) | ||
| end | ||
| end | ||
|
|
||
| puts "ruby-enum case: #{ruby_enum_time.round(4)}" | ||
| puts "case statement: #{case_statement_time.round(4)}" | ||
|
|
||
| puts "ruby-enum case is #{(ruby_enum_time / case_statement_time).round(2)} times slower" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module Ruby | ||
| module Enum | ||
| ## | ||
| # Adds a method to an enum class that allows for exhaustive matching on a value. | ||
| # | ||
| # @example | ||
| # class Color | ||
| # include Ruby::Enum | ||
| # include Ruby::Enum::Case | ||
| # | ||
| # define :RED, :red | ||
| # define :GREEN, :green | ||
| # define :BLUE, :blue | ||
| # define :YELLOW, :yellow | ||
| # end | ||
| # | ||
| # Color.case(Color::RED, { | ||
| # [Color::RED, Color::GREEN] => -> { "red or green" }, | ||
| # Color::BLUE => -> { "blue" }, | ||
| # Color::YELLOW => -> { "yellow" }, | ||
| # }) | ||
| # | ||
| # Reserves the :else key for a default case: | ||
| # Color.case(Color::RED, { | ||
| # [Color::RED, Color::GREEN] => -> { "red or green" }, | ||
| # else: -> { "blue or yellow" }, | ||
| # }) | ||
| module Case | ||
| def self.included(klass) | ||
| klass.extend(ClassMethods) | ||
| end | ||
|
|
||
| ## | ||
| # @see Ruby::Enum::Case | ||
| module ClassMethods | ||
| class ValuesNotDefinedError < StandardError | ||
| end | ||
|
|
||
| class NotAllCasesHandledError < StandardError | ||
| end | ||
|
|
||
| def case(value, cases) | ||
| validate_cases(cases) | ||
|
|
||
| filtered_cases = cases.select do |values, _proc| | ||
| values = [values] unless values.is_a?(Array) | ||
| values.include?(value) | ||
| end | ||
|
|
||
| return call_proc(cases[:else], value) if filtered_cases.none? | ||
|
|
||
| results = filtered_cases.map { |_values, proc| call_proc(proc, value) } | ||
|
|
||
| # Return the first result if there is only one result | ||
| results.size == 1 ? results.first : results | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def call_proc(proc, value) | ||
| return if proc.nil? | ||
|
|
||
| if proc.arity == 1 | ||
| proc.call(value) | ||
| else | ||
| proc.call | ||
| end | ||
| end | ||
|
|
||
| def validate_cases(cases) | ||
| all_values = cases.keys.flatten - [:else] | ||
| else_defined = cases.key?(:else) | ||
| superfluous_values = all_values - values | ||
| missing_values = values - all_values | ||
|
|
||
| raise ValuesNotDefinedError, "Value(s) not defined: #{superfluous_values.join(', ')}" if superfluous_values.any? | ||
| raise NotAllCasesHandledError, "Not all cases handled: #{missing_values.join(', ')}" if missing_values.any? && !else_defined | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,6 @@ | |
|
|
||
| module Ruby | ||
| module Enum | ||
| VERSION = '0.9.1' | ||
| VERSION = '1.0.0' | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require 'spec_helper' | ||
|
|
||
| RSpec.describe Ruby::Enum::Case do | ||
| test_enum = | ||
| Class.new do | ||
| include Ruby::Enum | ||
| include Ruby::Enum::Case | ||
|
|
||
| define :RED, :red | ||
| define :GREEN, :green | ||
| define :BLUE, :blue | ||
| end | ||
|
|
||
| describe '.case' do | ||
| context 'when all cases are defined' do | ||
| subject { test_enum.case(test_enum::RED, cases) } | ||
|
|
||
| let(:cases) do | ||
| { | ||
| [test_enum::RED, test_enum::GREEN] => -> { 'red or green' }, | ||
| test_enum::BLUE => -> { 'blue' } | ||
| } | ||
| end | ||
|
|
||
| it { is_expected.to eq('red or green') } | ||
|
|
||
| context 'when the value is nil' do | ||
| subject { test_enum.case(nil, cases) } | ||
|
|
||
| it { is_expected.to be_nil } | ||
| end | ||
|
|
||
| context 'when the value is empty' do | ||
| subject { test_enum.case('', cases) } | ||
|
|
||
| it { is_expected.to be_nil } | ||
| end | ||
|
|
||
| context 'when the value is the value of the enum' do | ||
| subject { test_enum.case(:red, cases) } | ||
|
|
||
| it { is_expected.to eq('red or green') } | ||
| end | ||
|
|
||
| context 'when the value is used inside the lambda' do | ||
| subject { test_enum.case(test_enum::RED, cases) } | ||
|
|
||
| let(:cases) do | ||
| { | ||
| [test_enum::RED, test_enum::GREEN] => ->(color) { "is #{color}" }, | ||
| test_enum::BLUE => -> { 'blue' } | ||
| } | ||
| end | ||
|
|
||
| it { is_expected.to eq('is red') } | ||
| end | ||
| end | ||
|
|
||
| context 'when there are mutliple matches' do | ||
| subject do | ||
| test_enum.case( | ||
| test_enum::RED, | ||
| { | ||
| [test_enum::RED, test_enum::GREEN] => -> { 'red or green' }, | ||
| test_enum::RED => -> { 'red' }, | ||
| test_enum::BLUE => -> { 'blue' } | ||
| } | ||
| ) | ||
| end | ||
|
|
||
| it { is_expected.to eq(['red or green', 'red']) } | ||
| end | ||
|
|
||
| context 'when not all cases are defined' do | ||
| it 'raises an error' do | ||
| expect do | ||
| test_enum.case( | ||
| test_enum::RED, | ||
| { [test_enum::RED, test_enum::GREEN] => -> { 'red or green' } } | ||
| ) | ||
| end.to raise_error(Ruby::Enum::Case::ClassMethods::NotAllCasesHandledError) | ||
| end | ||
| end | ||
|
|
||
| context 'when not all cases are defined but :else is specified (default case)' do | ||
| it 'does not raise an error' do | ||
| expect do | ||
| result = test_enum.case( | ||
| test_enum::BLUE, | ||
| { | ||
| [test_enum::RED, test_enum::GREEN] => -> { 'red or green' }, | ||
| else: -> { 'blue' } | ||
| } | ||
| ) | ||
|
|
||
| expect(result).to eq('blue') | ||
| end.not_to raise_error | ||
| end | ||
| end | ||
|
|
||
| context 'when a superfluous case is defined' do | ||
| it 'raises an error' do | ||
| expect do | ||
| test_enum.case( | ||
| test_enum::RED, | ||
| { | ||
| [test_enum::RED, test_enum::GREEN] => -> { 'red or green' }, | ||
| test_enum::BLUE => -> { 'blue' }, | ||
| :something => -> { 'green' } | ||
| } | ||
| ) | ||
| end.to raise_error(Ruby::Enum::Case::ClassMethods::ValuesNotDefinedError) | ||
| end | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.