Skip to content

Instantly share code, notes, and snippets.

@alexandru-calinoiu
Created November 7, 2016 15:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexandru-calinoiu/aa0d6bd48b25927e70200b798cae742e to your computer and use it in GitHub Desktop.
Save alexandru-calinoiu/aa0d6bd48b25927e70200b798cae742e to your computer and use it in GitHub Desktop.
Play around with dry matchers
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