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
Public Class Methods
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
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
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
Successful operations do not have error.
@return [nil]
# File lib/f_service/result/success.rb, line 58 def error nil end
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
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
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
(see value
)
# File lib/f_service/result/success.rb, line 51 def value! value end