Last active
July 21, 2019 16:31
-
-
Save nathan-appere/e5607f504d1e9a170fdcfa66c056b87f to your computer and use it in GitHub Desktop.
Bare logic for a functional organizer
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
require 'contracts' | |
module Organizer | |
include Contracts | |
Contract KeywordArgs[list: ArrayOf[RespondTo[:call]], ctx: Optional[Hash]] => [Symbol, Hash] | |
def self.call(list:, ctx: {}) | |
result = :ok | |
begin | |
list.each do |calleable| | |
result, ctx_new = calleable.call(ctx) | |
ctx = ctx.merge(ctx_new) | |
if result == :error | |
break | |
end | |
end | |
rescue Exception => e | |
result = :error | |
# Todo: use event bus to notify error handlers ? | |
end | |
[result, ctx] | |
end | |
end | |
module ThroughModule | |
def self.call(a:, **) | |
[:ok, a_res: true] | |
end | |
end | |
throughLambda = ->(b:, **) do | |
[:ok, b_res: true] | |
end | |
module ThroughAnyMethod | |
def self.any_method(c:, **) | |
[:ok, a_res: true] | |
end | |
end | |
calleable_list = [ | |
ThroughModule, | |
throughLambda, | |
ThroughAnyMethod.method(:any_method), | |
] | |
ctx = { a: 1, b: 2, c: 3 } | |
result, ctx = Organizer.call(list: calleable_list, ctx: ctx) | |
puts ctx |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment