Skip to content

Instantly share code, notes, and snippets.

@jodosha
Last active October 2, 2016 01:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jodosha/f8b11f22e431cffc43e4 to your computer and use it in GitHub Desktop.
Save jodosha/f8b11f22e431cffc43e4 to your computer and use it in GitHub Desktop.
Interactors proposal
class CreateEmailTest
include Lotus::Interactor
def initialize(params)
@params = params
@email_test = EmailTest.new(@params[:email_test])
end
def call
Operation.new(self, email_test: @email_test).
then(:validate).
then(:persist).
then(:capture_screenshot).
then(:deliver_emails).run
end
private
def validate
errors @params.errors unless @params.valid?
end
def persist
EmailTestRepository.create(@email_test)
end
def capture_screenshot
Screenshot.new(@email_test).capture
rescue
error "There was a problem while capturing the screenshot"
end
def deliver_emails
# ...
end
end
class CreateEmailTest
include Lotus::Interactor
def initialize(params)
@params = params
@email_test = EmailTest.new(@params[:email_test])
end
# List the operations to execute.
# Once error/errors is called, it uses `throw` to interrupt the flow.
#
# It has an implicit return value:
# Result
# email_test (how to declare its inclusion?)
# errors
# success?
def call
validate
persist
capture_screenshot
deliver_emails
end
private
def validate
errors @params.errors unless @params.valid?
end
def persist
@email_test = EmailTestRepository.create(@email_test)
error "Not persisted" if @email_test.id.nil?
end
def capture_screenshot
Screenshot.new(@email_test).capture
rescue
error "There was a problem while capturing the screenshot"
end
def deliver_emails
# ...
end
end
@apotonick
Copy link

So you're naming it Operation after Trailblazer? Or where does the Operation class come from?

@darwinrc
Copy link

The Interactors module will be included under apps or under lib? I mean, the use cases layer is going to be specific to each app or will it be an abstraction on top of domain model?

I'm asking this because in the clean architecture recommended by Uncle Bob, those interactors are part of the core of the business domain workflow while I think different apps will have different use cases.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment