# laserlemon/figaro architecture

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

- Interactive diagram: https://gitdiagram.com/laserlemon/figaro
- Repository: https://github.com/laserlemon/figaro

## Overview

##### Main Components
- Core Figaro Library:
- This includes the main module (lib/figaro.rb) alongside key components like Figaro::Env, Figaro::Error, and Figaro::Application.
- Its responsibility is to load a Git‑ignored YAML configuration file and inject its key/value pairs into the ENV for the Rails application.
- CLI Module:
- Found under lib/figaro/cli (with submodules for install and heroku_set), this component provides command-line tooling to install Figaro (creating the YAML file) and to easily configure remote environments (like Heroku) from that YAML file.
- The bin/figaro executable connects the CLI commands to these underlying functions.
- Rails Integration:
- This area, located under lib/figaro/rails, contains the Railtie (which hooks into Rails initialization) and Rake tasks (like lib/figaro/rails/tasks.rake).
- It ensures that when used in a Rails app, Figaro seamlessly loads configuration and integrates with the Rails configuration process.
- Tests/Specs (Verification Layer):
- Although not part of the runtime architecture, the spec directory (with RSpec tests) demonstrates how each component is tested, ensuring the behaviors of CLI commands, environment loading, and Rails integration are correct.

##### Relationships and Interactions
- The CLI Module is the entry point when Figaro is invoked from the command line (via the bin executable). This module calls into the Core Library to handle file reading, YAML parsing, and setting ENV variables.
- The Core Library accesses and processes the configuration file while managing errors and providing a convenient interface (including “bang” methods) to access ENV values.
- The Rails Integration component (via its Railtie and tasks) hooks into the Rails initialization process. It may also leverage the core functionality to load configuration data during application startup.

##### Architectural Patterns and Design Principles
- Separation of Concerns:
- The architecture clearly distinguishes between command-line operations (CLI), core logic, and Rails-specific functionality.
- Modularity:
- Each directory and module (core, CLI, rails, etc.) is encapsulated, meaning changes in one area (like CLI commands) have minimal impact on others.
- Convention over Configuration:
- Figaro advocates using a single YAML file and environment variables, following the Twelve-Factor App methodology.

##### Relevant Technologies and Frameworks
- Ruby as the implementation language, with the gem structure defined via the gemspec (figaro.gemspec).
- RSpec for testing (all files in the spec directory).
- Rails integration via Railtie and Rake tasks.
- Heroku integration via CLI commands (e.g., figaro heroku:set).

##### Final Touches and Details
- Every component has clear labels and a brief description if space allows.

## Diagram (Mermaid)

```mermaid
flowchart TD

    %% Core Figaro Library
    subgraph "Core Figaro Library"
        CORE1["Main (lib/figaro.rb)"]:::core
        CORE2["Env Handler (Figaro::Env - lib/figaro/env.rb)"]:::core
        CORE3["Error Handler (Figaro::Error - lib/figaro/error.rb)"]:::core
        CORE4["Application Interface (Figaro::Application - lib/figaro/application.rb)"]:::core
    end

    %% CLI Interface
    subgraph "CLI Interface"
        CLI_EXE["Executable (bin/figaro)"]:::cli
        CLI_MODULE["CLI Module (lib/figaro/cli.rb)"]:::cli
        CLI_HEROKU["Heroku Set Command (lib/figaro/cli/heroku_set.rb)"]:::cli
        CLI_INSTALL["Install Command (lib/figaro/cli/install.rb)"]:::cli
        CLI_INSTALL_YML["Install Configuration (YAML - lib/figaro/cli/install/application.yml)"]:::cli
        CLI_TASK["CLI Task Handler (lib/figaro/cli/task.rb)"]:::cli
    end

    %% Rails Integration
    subgraph "Rails Integration"
        RAILS_BASE["Rails Integration Base (lib/figaro/rails.rb)"]:::rails
        RAILS_APP["Rails Application Config (lib/figaro/rails/application.rb)"]:::rails
        RAILS_RAILTIE["Railtie (lib/figaro/rails/railtie.rb)"]:::rails
        RAILS_TASKS["Rails Rake Tasks (lib/figaro/rails/tasks.rake)"]:::rails
    end

    %% Verification Layer
    subgraph "Verification Layer"
        TESTS["Specs & Tests (spec/)"]:::test
    end

    %% ENV Injection Node
    ENV_NODE["ENV Variables"]:::env

    %% Connections
    %% CLI triggers core functionality
    CLI_EXE -->|"triggers"| CORE1
    CLI_MODULE -->|"calls"| CORE1
    CLI_HEROKU -->|"calls"| CORE1
    CLI_TASK -->|"invokes"| CORE1
    %% Install command yields YAML config that loads into Core
    CLI_INSTALL -->|"creates"| CLI_INSTALL_YML
    CLI_INSTALL_YML -->|"loads"| CORE1

    %% Rails integration hooks
    RAILS_RAILTIE -->|"hooks"| CORE1
    RAILS_BASE -->|"utilizes"| CORE1
    RAILS_APP -->|"utilizes"| CORE1
    RAILS_TASKS -->|"triggers"| CORE1

    %% Core loads environment variables
    CORE1 -->|"injects"| ENV_NODE

    %% Styling Classes
    classDef core fill:#ffcccc,stroke:#333,stroke-width:2px;
    classDef cli fill:#ccf2ff,stroke:#333,stroke-width:2px;
    classDef rails fill:#ccffcc,stroke:#333,stroke-width:2px;
    classDef env fill:#fff2cc,stroke:#333,stroke-width:2px;
    classDef test fill:#e6ccff,stroke:#333,stroke-width:2px;

    %% Click Events for Core Figaro Library Components
    click CORE1 "https://github.com/laserlemon/figaro/blob/main/lib/figaro.rb"
    click CORE2 "https://github.com/laserlemon/figaro/blob/main/lib/figaro/env.rb"
    click CORE3 "https://github.com/laserlemon/figaro/blob/main/lib/figaro/error.rb"
    click CORE4 "https://github.com/laserlemon/figaro/blob/main/lib/figaro/application.rb"

    %% Click Events for CLI Module Components
    click CLI_EXE "https://github.com/laserlemon/figaro/tree/main/bin/figaro"
    click CLI_MODULE "https://github.com/laserlemon/figaro/blob/main/lib/figaro/cli.rb"
    click CLI_HEROKU "https://github.com/laserlemon/figaro/blob/main/lib/figaro/cli/heroku_set.rb"
    click CLI_INSTALL "https://github.com/laserlemon/figaro/blob/main/lib/figaro/cli/install.rb"
    click CLI_INSTALL_YML "https://github.com/laserlemon/figaro/blob/main/lib/figaro/cli/install/application.yml"
    click CLI_TASK "https://github.com/laserlemon/figaro/blob/main/lib/figaro/cli/task.rb"

    %% Click Events for Rails Integration Components
    click RAILS_BASE "https://github.com/laserlemon/figaro/blob/main/lib/figaro/rails.rb"
    click RAILS_APP "https://github.com/laserlemon/figaro/blob/main/lib/figaro/rails/application.rb"
    click RAILS_RAILTIE "https://github.com/laserlemon/figaro/blob/main/lib/figaro/rails/railtie.rb"
    click RAILS_TASKS "https://github.com/laserlemon/figaro/blob/main/lib/figaro/rails/tasks.rake"

    %% Click Event for Tests/Specs
    click TESTS "https://github.com/laserlemon/figaro/tree/main/spec/"
```
