How collectiveidea/interactor works
Project type and purpose
• This is an open-source Ruby library (a Ruby gem) named “Interactor” that encapsulates business logic in self-contained objects.
• It provides two main abstractions:
• Basic Interactors (single-purpose units of work)
• Organizers (to sequence multiple interactors with optional rollback)
File structure
• Top‐level files: gemspec, Gemfile, Rakefile, standard config, CI workflow, README, CONTRIBUTING, tests, license, changelog.
• lib/interactor.rb: the entry point that defines Interactor module, includes code and autoloads subcomponents.
• lib/interactor/{context.rb, error.rb, hooks.rb, organizer.rb}: core classes/modules.
• spec/: RSpec tests exercising integration and unit specs for Context, Hooks, Organizer.
Key architecture elements from README
• A call flow: Consumer → Interactor.call(hash) → Context initialization → Hook execution (around/before/call/after) → return Context
• Organizer flow: sequence of interactors with shared Context and rollback on failure.
• Extensibility via mix-in concerns (ActiveSupport::Concern).
A. Consumer Application (e.g., Rails controller or any Ruby code)
B. Interactor Library
1. Interactor (module entrypoint)
2. Context (data carrier + success/failure state)
3. Hooks subsystem (before/after/around)
4. Organizer subsystem (sequence + rollback)
C. External Dependencies (ActiveSupport::Concern, exceptions, user code)
D. Your Interactor Classes (basic or organizers implemented by the user)
Relationships & interactions
• Consumer invokes SomeInteractor.call(input_hash)
• Interactor.call does: build Context → wrap in error handler → run Hooks (around → before → call → after) → return Context
• For organizers: after Context init, Interactor::Organizer invokes each child interactor in order, passing same Context; on failure triggers rollback on previously run interactors.
• Context flows through all interactors, accumulates data, flags success/failure.
Architectural patterns & design principles
• Command-pattern style: each interactor is a command object with a single call method.
• Chain of Responsibility for organizers: sequential execution, short-circuit on failure.
• Hook/Interceptor pattern: around/before/after hooks loosely couple cross-cutting concerns.
• Single Responsibility: each interactor does one thing; organizers orchestrate many.
Technologies & frameworks
• Ruby ≥2.3
• RSpec for tests
• ActiveSupport::Concern for hook concerns
• Standard Ruby exception (Interactor::Failure) for control flow