Class: FService::Result::Success
- 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
-
#types ⇒ Object
readonly
The provided types for the result.
-
#value ⇒ Object
readonly
The provided value for the result.
Instance Method Summary collapse
-
#and_then {|value, types| ... } ⇒ Object
Returns its value to the given block.
-
#catch ⇒ self
(also: #or_else)
Returns itself to the given block.
-
#error ⇒ nil
Successful operations do not have error.
-
#failed? ⇒ Boolean
Returns false.
-
#initialize(value, types = []) ⇒ Success
constructor
Creates a successful operation.
-
#successful? ⇒ Boolean
Returns true.
-
#then(&block) ⇒ Object
See #and_then.
-
#to_s ⇒ String
Outputs a string representation of the object.
-
#value! ⇒ Object
The provided value for the result.
Methods inherited from Base
#on_failure, #on_success, #to_ary, #type
Constructor Details
#initialize(value, types = []) ⇒ Success
Creates a successful operation. You usually shouldn’t call this directly. See Base#Success.
22 23 24 25 |
# File 'lib/f_service/result/success.rb', line 22 def initialize(value, types = []) super(types) @value = value end |
Instance Attribute Details
#types ⇒ Object (readonly)
Returns the provided types for the result. Defaults to nil.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/f_service/result/success.rb', line 15 class Success < Result::Base attr_reader :value # Creates a successful operation. # You usually shouldn't call this directly. See {FService::Base#Success}. # # @param value [Object] success value. def initialize(value, types = []) super(types) @value = value end # Returns true. # # # @example # # Suppose that User::Update returns an FService::Result # # log_errors(user) unless User::Update.(user: user).successful? def successful? true end # Returns false. # # # @example # # Suppose that User::Update returns an FService::Result # # log_errors(user) if User::Update.(user: user).failed? def failed? false end # (see #value) def value! value end # Successful operations do not have error. # # @return [nil] def error nil end # 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 types pass {#types} to a block def and_then yield(*to_ary) end # See #and_then def then(&block) FService.deprecate!(name: "#{self.class}##{__method__}", alternative: '#and_then', from: caller[0]) and_then(&block) 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] def catch self end alias or_else catch # Outputs a string representation of the object # # # @example # puts FService::Result::Success.new("Yay!") # # => Success("Yay!") # # @return [String] the object's string representation def to_s value.nil? ? 'Success()' : "Success(#{value.inspect})" end end |
#value ⇒ Object (readonly)
Returns the provided value for the result.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/f_service/result/success.rb', line 15 class Success < Result::Base attr_reader :value # Creates a successful operation. # You usually shouldn't call this directly. See {FService::Base#Success}. # # @param value [Object] success value. def initialize(value, types = []) super(types) @value = value end # Returns true. # # # @example # # Suppose that User::Update returns an FService::Result # # log_errors(user) unless User::Update.(user: user).successful? def successful? true end # Returns false. # # # @example # # Suppose that User::Update returns an FService::Result # # log_errors(user) if User::Update.(user: user).failed? def failed? false end # (see #value) def value! value end # Successful operations do not have error. # # @return [nil] def error nil end # 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 types pass {#types} to a block def and_then yield(*to_ary) end # See #and_then def then(&block) FService.deprecate!(name: "#{self.class}##{__method__}", alternative: '#and_then', from: caller[0]) and_then(&block) 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] def catch self end alias or_else catch # Outputs a string representation of the object # # # @example # puts FService::Result::Success.new("Yay!") # # => Success("Yay!") # # @return [String] the object's string representation def to_s value.nil? ? 'Success()' : "Success(#{value.inspect})" end end |
Instance Method Details
#and_then {|value, types| ... } ⇒ Object
Returns its value to the given block. Use this to chain multiple service calls (since all services return Results).
82 83 84 |
# File 'lib/f_service/result/success.rb', line 82 def and_then yield(*to_ary) end |
#catch ⇒ self Also known as: or_else
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.
113 114 115 |
# File 'lib/f_service/result/success.rb', line 113 def catch self end |
#error ⇒ nil
Successful operations do not have error.
57 58 59 |
# File 'lib/f_service/result/success.rb', line 57 def error nil end |
#failed? ⇒ Boolean
Returns false.
45 46 47 |
# File 'lib/f_service/result/success.rb', line 45 def failed? false end |
#successful? ⇒ Boolean
Returns true.
34 35 36 |
# File 'lib/f_service/result/success.rb', line 34 def successful? true end |
#then(&block) ⇒ Object
See #and_then
87 88 89 90 91 |
# File 'lib/f_service/result/success.rb', line 87 def then(&block) FService.deprecate!(name: "#{self.class}##{__method__}", alternative: '#and_then', from: caller[0]) and_then(&block) end |
#to_s ⇒ String
Outputs a string representation of the object
127 128 129 |
# File 'lib/f_service/result/success.rb', line 127 def to_s value.nil? ? 'Success()' : "Success(#{value.inspect})" end |
#value! ⇒ Object
Returns the provided value for the result.
50 51 52 |
# File 'lib/f_service/result/success.rb', line 50 def value! value end |