class FService::Result::Success

Represents a value of a successful operation. The value field can contain any information you want.

@!attribute [r] value

@return [Object] the provided value for the result

@!attribute [r] type

@return [Object] the provided type for the result. Defaults to nil.

@api public

Attributes

type[R]
value[R]

Public Class Methods

new(value, type = nil) click to toggle source

Creates a successful operation. You usually shouldn't call this directly. See {FService::Base#Success}.

@param value [Object] success value.

# File lib/f_service/result/success.rb, line 22
def initialize(value, type = nil)
  @value = value
  @type = type
  freeze
end

Public Instance Methods

and_then() { |*to_ary| ... } click to toggle source

Returns its value to the given block. Use this to chain multiple service calls (since all services return Results).

@example

class UsersController < BaseController
  def create
    result = User::Create.(user_params)
                         .and_then { |user| User::SendWelcomeEmail.(user: user) }
                         .and_then { |user| User::Login.(user: user) }

    if result.successful?
      json_success(result.value)
    else
      json_error(result.error)
    end
  end
end

@yieldparam value pass {#value} to a block @yieldparam type pass {#type} to a block

# File lib/f_service/result/success.rb, line 83
def and_then
  yield(*to_ary)
end
Also aliased as: then
catch() click to toggle source

Returns itself to the given block. Use this to chain multiple actions or service calls (only valid when they return a Result). It works just like the `.and_then` method, but only runs if service is a Failure.

@example

class UpdateUserOnExternalService
  attribute :user_params

  def run
    check_api_status
      .and_then { update_user }
      .or_else { create_update_worker }
  end

  private
  # some code
end

@return [self]

# File lib/f_service/result/success.rb, line 108
def catch
  self
end
Also aliased as: or_else
error() click to toggle source

Successful operations do not have error.

@return [nil]

# File lib/f_service/result/success.rb, line 58
def error
  nil
end
failed?() click to toggle source

Returns false.

@example

# Suppose that User::Update returns an FService::Result

log_errors(user) if User::Update.(user: user).failed?
# File lib/f_service/result/success.rb, line 46
def failed?
  false
end
or_else()
Alias for: catch
successful?() click to toggle source

Returns true.

@example

# Suppose that User::Update returns an FService::Result

log_errors(user) unless User::Update.(user: user).successful?
# File lib/f_service/result/success.rb, line 35
def successful?
  true
end
then()
Alias for: and_then
to_s() click to toggle source

Outputs a string representation of the object

@example

puts FService::Result::Success.new("Yay!")
# => Success("Yay!")

@return [String] the object's string representation

# File lib/f_service/result/success.rb, line 122
def to_s
  value.nil? ? 'Success()' : "Success(#{value.inspect})"
end
value!() click to toggle source

(see value)

# File lib/f_service/result/success.rb, line 51
def value!
  value
end