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_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.

Parameters:

  • value (Object)

    success value.



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

#typesObject (readonly)

Returns the provided types for the result. Defaults to nil.

Returns:

  • (Object)

    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

#valueObject (readonly)

Returns the provided value for the result.

Returns:

  • (Object)

    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).

Examples:

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

Yield Parameters:

  • value

    pass #value to a block

  • types

    pass #types to a block



82
83
84
# File 'lib/f_service/result/success.rb', line 82

def and_then
  yield(*to_ary)
end

#catchself 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.

Examples:

class UpdateUserOnExternalService
  attribute :user_params

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

  private
  # some code
end

Returns:

  • (self)


113
114
115
# File 'lib/f_service/result/success.rb', line 113

def catch
  self
end

#errornil

Successful operations do not have error.

Returns:

  • (nil)


57
58
59
# File 'lib/f_service/result/success.rb', line 57

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)


45
46
47
# File 'lib/f_service/result/success.rb', line 45

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)


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_sString

Outputs a string representation of the object

Examples:

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

Returns:

  • (String)

    the object’s string representation



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.

Returns:

  • (Object)

    the provided value for the result



50
51
52
# File 'lib/f_service/result/success.rb', line 50

def value!
  value
end