Skip to content

Commit

Permalink
Use public api to implement exhaustive pattern matching
Browse files Browse the repository at this point in the history
  • Loading branch information
bolshakov committed Feb 18, 2019
1 parent f0bd940 commit 7d56fe3
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Expand Up @@ -5,4 +5,4 @@ gemspec

# gem 'codeclimate-test-reporter', group: :test, require: nil

gem 'qo', github: 'bolshakov/qo', branch: 'feature/order-of-execution'
gem 'qo', github: 'baweaver/qo', branch: 'baweaver/exhaustive_match'
2 changes: 1 addition & 1 deletion fear.gemspec
Expand Up @@ -20,7 +20,7 @@ Gem::Specification.new do |spec|
spec.require_paths = ['lib']

spec.add_runtime_dependency 'dry-equalizer', '<= 0.2.1'
spec.add_runtime_dependency 'qo', '0.99.0'
spec.add_runtime_dependency 'qo', '>= 0.99.1'

spec.add_development_dependency 'appraisal'
spec.add_development_dependency 'bundler'
Expand Down
26 changes: 24 additions & 2 deletions lib/fear/exhaustive_pattern_match.rb
Expand Up @@ -5,6 +5,27 @@ module Fear
# @api private
# @see Qo::PatternMatchers::PatternMatch
module ExhaustivePatternMatch
class << self
def prepended(base)
base.extend(ClassMethods)
end
end

module ClassMethods
# FIXME: remove after merge https://github.com/baweaver/qo/pull/26
def mixin(destructure: false, as: :match)
create_self = ->(&function) { new(destructure: destructure, exhaustive: true, &function) }

Module.new do
define_method(as) do |&function|
create_self.call(&function).call(self)
end
end
end
end

private_constant :ClassMethods

EXHAUSTIVE_PATTERN_MATCH_ERROR = <<-ERROR.freeze
Pattern match is not exhaustive.
Expand All @@ -17,9 +38,10 @@ module ExhaustivePatternMatch
end
ERROR

def initialize(**)
def call(*)
super
@default ||= self.else { fail MatchError, EXHAUSTIVE_PATTERN_MATCH_ERROR }
rescue Qo::Exceptions::ExhaustiveMatchNotMet
raise MatchError, EXHAUSTIVE_PATTERN_MATCH_ERROR
end
end

Expand Down

0 comments on commit 7d56fe3

Please sign in to comment.