#interactors

Stephann V.ste@ruby.social
2025-02-04

I was thinking about writing a Yet another interactor/service/use case/command gem for myself. But I ended up using pure Ruby. If I want, I can create different results, eg. `WaitingApproval = Data.define(:user)`.

#ruby #poro #services #interactors

Screenshot of this code:

class UserCreator
  Success = Data.define(:user)
  Failure = Data.define(:reason, :user)

  def initialize(attributes:)
    @attributes = attributes
  end

  def create
    user = # do stuff

    return Success[user:] if user.save

    Failure[reason: :invalid_record, user:]
  end
end


def create
  case UserCreator.new(attributes:).create
  in UserCreator::Success(user)
    redirect_to user_path(user)
  in UserCreator::Failure(user)
    render Users::NewPage.new(user:)
  end
end

Client Info

Server: https://mastodon.social
Version: 2025.04
Repository: https://github.com/cyevgeniy/lmst