Created
November 16, 2017 09:50
-
-
Save davydovanton/815a8f777ccfcc5d3204345b055ff4e8 to your computer and use it in GitHub Desktop.
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 'hanami/interactor' | |
require 'dry-matcher' | |
module Hanami::Interactor | |
module Matcher | |
SUCCESS_CASE = Dry::Matcher::Case.new( | |
match: -> result { result.successful? }, | |
resolve: -> result { result.value } | |
) | |
FAILURE_CASE = Dry::Matcher::Case.new( | |
match: -> result { result.failure? }, | |
resolve: -> result { result.value } | |
) | |
MatcherObject = Dry::Matcher.new(success: SUCCESS_CASE, failure: FAILURE_CASE) | |
def call(*args, **kwargs, &block) | |
@__result = ::Hanami::Interactor::Result.new | |
_call(*args, **kwargs) { super } | |
if block | |
MatcherObject.(@__result, &block) | |
else | |
@__result | |
end | |
end | |
end | |
end | |
module Hanami::Interactor::Interface | |
include Hanami::Interactor::Matcher | |
end | |
class Operation | |
include Hanami::Interactor | |
expose :value | |
def call(condition: true) | |
if condition | |
@value = 'hello' | |
else | |
@value = 'error' | |
fail! | |
end | |
end | |
end | |
Operation.new.call(condition: true) do |m| | |
m.success do |v| | |
puts "Yay: #{v}" | |
end | |
m.failure do |v| | |
puts "Boo: #{v}" | |
end | |
end | |
Operation.new.call(condition: false) do |m| | |
m.success do |v| | |
puts "Yay: #{v}" | |
end | |
m.failure do |v| | |
puts "Boo: #{v}" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment