Created
November 7, 2016 15:25
-
-
Save alexandru-calinoiu/aa0d6bd48b25927e70200b798cae742e to your computer and use it in GitHub Desktop.
Play around with dry matchers
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
require 'dry-matcher' | |
success_case = Dry::Matcher::Case.new( | |
match: -> value { value.first == :ok }, | |
resolve: -> value { value.last } | |
) | |
failure_case = Dry::Matcher::Case.new( | |
match: -> value, *pattern { | |
value[0] == :err && (pattern.any? ? pattern.include?(value[1]) : true) | |
}, | |
resolve: -> value { value.last } | |
) | |
matcher = Dry::Matcher.new(success: success_case, failure: failure_case) | |
validate = -> value { | |
matcher.(value) do |m| | |
m.success do |v| | |
"Yay: #{v}" | |
end | |
m.failure :not_found do |v| | |
"Oops, not found: #{v}" | |
end | |
m.failure do |v| | |
"Boo: #{v}" | |
end | |
end | |
} | |
p validate.([:ok, 'success']) | |
p validate.([:err, :not_found, 'missing!']) | |
p validate.([:err, 'bad!']) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment