Class: RailsAiBridge::Service

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_ai_bridge/service.rb,
lib/rails_ai_bridge/service/result.rb

Overview

Base service class providing a standardized interface for all services.

Services follow the command pattern with a single public method (.call) that returns a Service::Result object. This provides consistent error handling and result structure across all services.

Examples:

Basic Usage

class MyService < RailsAiBridge::Service
  def call
    # business logic
    Service::Result.new(true, data: "result")
  end
end

result = MyService.call(arg1, kwarg1: "value")
if result.success?
  puts result.data
else
  puts "Error: #{result.errors.first}"
end

Defined Under Namespace

Classes: Result

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args, **kwargs) ⇒ Service

Initialize the service with provided arguments.

Parameters:

  • args (Array)

    positional arguments

  • kwargs (Hash)

    keyword arguments



40
41
42
43
44
# File 'lib/rails_ai_bridge/service.rb', line 40

def initialize(*args, **kwargs)
  # Standard initialization
  @args = args
  @kwargs = kwargs
end

Class Method Details

.callService::Result

Class-level entry point that creates an instance and calls it.

Parameters:

  • args (Array)

    positional arguments passed to initialize

  • kwargs (Hash)

    keyword arguments passed to initialize

Returns:



31
32
33
# File 'lib/rails_ai_bridge/service.rb', line 31

def call(*, **)
  new(*, **).call
end

Instance Method Details

#callService::Result

Execute the service and return a result.

Subclasses must override this method to implement specific business logic.

Returns:

Raises:

  • (NotImplementedError)

    if not overridden by subclass



52
53
54
# File 'lib/rails_ai_bridge/service.rb', line 52

def call
  raise NotImplementedError, "#{self.class} must implement #call"
end