Skip to content

Instantly share code, notes, and snippets.

@nathan-appere nathan-appere/organizer.rb
Last active Jul 21, 2019

Embed
What would you like to do?
Bare logic for a functional organizer
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
You can’t perform that action at this time.