Skip to content

Output attributes

Add all return attributes via the output method. These are available through the Result class.

Usage

Assign and access output attributes via outputs=/outputs methods.

ruby
class UsersService::Create < ApplicationService::Base
  input :first_name, type: String
  input :middle_name, type: String
  input :last_name, type: String

  internal :full_name, type: String

  output :user, type: User

  make :assign_full_name
  make :create!

  def assign_full_name
    internals.full_name = [
      inputs.first_name,
      inputs.middle_name,
      inputs.last_name
    ].join(" ")
  end

  def create!
    outputs.user = User.create!(full_name: internals.full_name)
  end
end

Options

See using options for details.

Helpers

Servactory supports custom helpers for project purposes. Helpers are shorthand that expand into specific options.

Custom

Add custom helpers via output_option_helpers in configuration. Helpers can be based on existing options.

Configuration example

Example with must

ruby
class PaymentsService::Create < ApplicationService::Base
  # ...

  output :invoice_numbers,
         :must_be_6_characters,
         type: Array,
         consists_of: String

  # ...
end

Methods

Method only

Filter outputs with the only method. Returns a Hash with specified attributes.

ruby
outputs.full_name =
  outputs.only(:first_name, :middle_name, :last_name)
    .values
    .compact
    .join(" ")

Method except

Filter outputs with the except method. Returns a Hash without specified attributes.

ruby
outputs.full_name =
  outputs.except(:gender)
    .values
    .compact
    .join(" ")

Predicate methods

Access any output attribute as a predicate method.

ruby
# ...

output :full_name, type: String

# ...

def something
  return unless outputs.full_name? # instead of `outputs.full_name.present?`

  # ...
end