-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #43 from peterfication/add-exhaustive-case-statement
Add exhaustive case matcher
- Loading branch information
Showing
12 changed files
with
315 additions
and
12 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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 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 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 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 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 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