Lesli::Courier

The Lesli::Courier module is a dynamic service caller designed to streamline interactions with service classes within a Ruby on Rails application. By enabling dynamic resolution of modules, services, and methods, Lesli::Courier simplifies complex service calls into a clean and fluent interface.

Why Use Lesli::Courier?

  1. Dynamic Module and Service Resolution: Avoid hardcoding module and service names, making the codebase more flexible and easier to maintain.
  2. Fluent Interface: Create service calls with a readable, chainable syntax.
  3. Error Handling: Optional support for handling undefined modules, services, or methods gracefully.
  4. Improved Readability: Reduce boilerplate code and make service interactions more concise.


How It Works

The Lesli::Courier module dynamically:

  1. Resolves the module name (e.g., :lesli_test becomes LesliTest).
  2. Identifies the service class within the module (e.g., :ticket_service becomes TicketService).
  3. Calls the specified method on the service class, optionally passing arguments.


Key Methods

initialize(module_name, on_error = nil)

    Lesli::Courier.new(:lesli_test)

from(service_name)

    Lesli::Courier.new(:lesli_test).from(:ticket_service)

with(*args)

    Lesli::Courier.new(:lesli_test).from(:ticket_service).with(current_user, query)

call(method_name)

    Lesli::Courier.new(:lesli_test).from(:ticket_service).call(:index)

Error Handling

If any part of the process fails (e.g., the module, service class, or method is not found), Lesli::Courier will return the value specified in the on_error parameter (if provided). Otherwise, it will raise an error.

Usage Examples

Basic Usage

# Call the `index_with_deadline` method on the `TicketService` class within the `LesliTest` module
Lesli::Courier.new(:lesli_test)
              .from(:ticket_service)
              .call(:index_with_deadline)

Passing Arguments

# Call the `index` method on the `TicketService` class, passing arguments
Lesli::Courier.new(:lesli_test)
              .from(:ticket_service)
              .with(current_user, query)
              .call(:index)

Custom Error Handling

# Return a custom error object if the service or method is not found
Lesli::Courier.new(:lesli_test, []).from(:ticket_service).call(:non_existent_method)

Note: In the event of an error, Lesli::Courier will always return false instead of raising an exception.

 Edit this page

Last Update: 2024/12/26