Last active
August 19, 2019 15:17
-
-
Save dreikanter/34cc736ff568fc84065a5da964b56442 to your computer and use it in GitHub Desktop.
Trying dry-monads
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
# #fmap - bypass Failure object, or return Success(result) | |
# #bind - bypass Failure object, or return result | |
require 'dry/monads' | |
require 'dry/monads/do' | |
class SequenceExecutor | |
include Dry::Monads[:result] | |
# This will prepend the class with a module, bypassinf a block to #call | |
include Dry::Monads::Do.for(:call) | |
def call | |
# Each yield will short-circuits the execution unless result is Success | |
content = yield load_content('FEED') | |
puts "content == #{content}" | |
entities = yield process(content) | |
puts "entities == #{entities}" | |
normalize(entities) | |
end | |
def load_content(feed) | |
puts "-----> load #{feed}" | |
Success('CONTENT') | |
# Failure('loading error') | |
end | |
def process(content) | |
puts "-----> process #{content}" | |
Success([ | |
%i[uid1 entity1], | |
%i[uid2 entity2], | |
%i[uid3 entity3] | |
]) | |
# Failure('processing error') | |
end | |
def normalize(entities) | |
puts "-----> normalize #{entities}" | |
result = entities.map { |entity| Success(entity) } | |
Success(result) | |
# Failure('normalization error') | |
end | |
end | |
result = SequenceExecutor.new.call | |
puts "result = #{result}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment