Class: FService::Result::Success

Inherits:
Base
  • Object
show all
Defined in:
lib/f_service/result/success.rb

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#on

Constructor Details

#initialize(value) ⇒ Success

Creates a successful operation. You usually shouldn’t call this directly. See Base#success.

Parameters:

  • value (Object)

    success value.



19
20
21
22
# File 'lib/f_service/result/success.rb', line 19

def initialize(value)
  @value = value
  freeze
end

Instance Attribute Details

#valueObject (readonly)

Returns the provided value.



13
14
15
# File 'lib/f_service/result/success.rb', line 13

def value
  @value
end

Instance Method Details

#errornil

Successful operations do not have error.

Returns:

  • (nil)


54
55
56
# File 'lib/f_service/result/success.rb', line 54

def error
  nil
end

#failed?Boolean

Returns false.

Examples:

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

log_errors(user) if User::Update.(user: user).failed?

Returns:

  • (Boolean)


42
43
44
# File 'lib/f_service/result/success.rb', line 42

def failed?
  false
end

#successful?Boolean

Returns true.

Examples:

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

log_errors(user) unless User::Update.(user: user).successful?

Returns:

  • (Boolean)


31
32
33
# File 'lib/f_service/result/success.rb', line 31

def successful?
  true
end

#then {|value| ... } ⇒ Object

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

Examples:

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

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

Yield Parameters:

  • value (result)

    pass #value to a block



78
79
80
# File 'lib/f_service/result/success.rb', line 78

def then
  yield value
end

#to_sString

Outputs a string representation of the object

Examples:

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

Returns:

  • (String)

    the object’s string representation



90
91
92
# File 'lib/f_service/result/success.rb', line 90

def to_s
  value.nil? ? 'Success()' : "Success(#{value.inspect})"
end

#value!Object

Returns the provided value.



47
48
49
# File 'lib/f_service/result/success.rb', line 47

def value!
  value
end