Skip to content

Instantly share code, notes, and snippets.

@nathan-appere
Last active July 21, 2019 16:31
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 nathan-appere/e5607f504d1e9a170fdcfa66c056b87f to your computer and use it in GitHub Desktop.
Save nathan-appere/e5607f504d1e9a170fdcfa66c056b87f to your computer and use it in GitHub Desktop.
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