# collectiveidea/interactor architecture

> Architecture diagram of the GitHub repository collectiveidea/interactor, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-09-25). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/collectiveidea/interactor
- Repository: https://github.com/collectiveidea/interactor

## Overview

##### 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

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% User Application
    subgraph "User Application"
        Consumer["Consumer Application"]:::userCode
    end

    %% User-defined Interactors
    subgraph "User Interactors"
        UI1["BasicInteractor\n(includes Interactor)"]:::userCode
        UI2["OrganizerClass\n(includes Interactor::Organizer)"]:::userCode
        UI3["AnotherInteractor\n(includes Interactor)"]:::userCode
    end

    %% Core Library Components
    subgraph "Interactor Library"
        InteractorModule["Interactor Module\n(library entrypoint)"]:::coreLibrary
        Context["Context\n(data carrier + state)"]:::coreLibrary
        Error["Failure Exception"]:::errorPath
        Hooks["Hooks Subsystem\n(around/before/after)"]:::coreLibrary
        Organizer["Organizer Subsystem\n(sequence + rollback)"]:::coreLibrary
        GemSpec["Gem Specification & Metadata"]:::coreLibrary
    end

    %% Flow
    Consumer -->|"calls .call"| InteractorModule
    InteractorModule -->|"initializes"| Context
    InteractorModule -->|"handles exceptions"| Error
    InteractorModule -->|"runs hooks"| Hooks
    Hooks -->|"executes user call"| UI1
    Hooks -->|"executes user call"| UI2
    UI2 -->|"deferred to"| Organizer
    Organizer -->|"invokes sequence"| UI1
    Organizer --> UI3
    Organizer -.->|"rollback on failure"| UI1
    Context -->|"returned to"| Consumer

    %% Click Events
    click InteractorModule "https://github.com/collectiveidea/interactor/blob/master/lib/interactor.rb"
    click Context "https://github.com/collectiveidea/interactor/blob/master/lib/interactor/context.rb"
    click Error "https://github.com/collectiveidea/interactor/blob/master/lib/interactor/error.rb"
    click Hooks "https://github.com/collectiveidea/interactor/blob/master/lib/interactor/hooks.rb"
    click Organizer "https://github.com/collectiveidea/interactor/blob/master/lib/interactor/organizer.rb"
    click GemSpec "https://github.com/collectiveidea/interactor/blob/master/interactor.gemspec"

    %% Styles
    classDef coreLibrary fill:#D6EAF8,stroke:#1F618D,color:#1F618D
    classDef userCode fill:#D5F5E3,stroke:#229954,color:#145A32
    classDef errorPath fill:#FADBD8,stroke:#C0392B,color:#78281F
```
