Skip to content

Instantly share code, notes, and snippets.

@adamstrickland
Last active September 2, 2022 16:15
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 adamstrickland/cb5ca5b8b9e44a79bbeb1626c109a3fc to your computer and use it in GitHub Desktop.
Save adamstrickland/cb5ca5b8b9e44a79bbeb1626c109a3fc to your computer and use it in GitHub Desktop.
require_relative "./interactor"
ctx = SomeInteractor::Context.new(var_a: true)
res = SomeInteractor.new(ctx).call
assert res.success?
res = SomeInteractor.new(var_a: false).call
assert res.failure?
begin
SomeInteractor.new(var_b: :foo).call
rescue => e
assert e.present?
end
class SomeInteractor
Context = ImmutableStruct.new(:var_a)
attr_reader :context
def initialize(ctx, **opts)
ctx ||= Context.new(**opts)
raise unless ctx.is_a?(Context)
@context = ctx
end
def call
return Success(context) if context.var_a
Failure("context#var_a is falsy")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment