Class: ActiveInteractor::Result

Inherits:
Object
  • Object
show all
Extended by:
ActiveModel::Naming
Defined in:
lib/active_interactor/result.rb

Overview

An interactor result

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#dataActiveInteractor::Context::Result, ... (readonly)

Returns the data returned by the interactor.

Examples:

result = CreateUser.perform(email: 'hello@aaronmallen.me', password: 'password')
result.data.user
#=> <#User @email="hello@aaronmallen.me">

Returns:



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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/active_interactor/result.rb', line 25

class Result
  extend ActiveModel::Naming

  attr_reader :data, :errors

  # Return a result as a JSON string
  delegate :to_json, to: :to_hash

  # Status codes for interactor results
  #
  # @visibility private
  STATUS = {
    success: 0,
    failed_at_input: 1,
    failed_at_runtime: 2,
    failed_at_output: 3
  }.freeze

  class << self
    # Return a failed result
    #
    # @visibility private
    #
    # @param data [ActiveInteractor::Context::Result, Object, nil] the data returned by the interactor
    # @param errors [ActiveModel::Errors, Hash, String] the errors from the interactor
    # @param status [Integer] the status code for the result
    #
    # @return [ActiveInteractor::Result] the failed result
    def failure(data: {}, errors: {}, status: STATUS[:failed_at_runtime])
      result = new(status: status, data: data)
      parse_errors(errors).each_pair do |attribute, messages|
        Array.wrap(messages).each { |message| result.errors.add(attribute, message) }
      end
      result
    end

    # Needed for ActiveModel::Errors
    #
    # @visibility private
    def human_attribute_name(attribute, _options = {})
      attribute.respond_to?(:to_s) ? attribute.to_s.humanize : attribute
    end

    # Needed for ActiveModel::Errors
    #
    # @visibility private
    def lookup_ancestors
      [self]
    end

    # Return a successful result
    #
    # @visibility private
    #
    # @param data [ActiveInteractor::Context::Result, Object, nil] the data returned by the interactor
    #
    # @return [ActiveInteractor::Result] the successful result
    def success(data: {})
      new(status: STATUS[:success], data: data)
    end

    private

    def parse_errors(errors)
      case errors
      when String
        { generic: [errors] }
      when ActiveModel::Errors
        errors.to_hash
      else
        errors
      end
    end
  end

  private_class_method :new

  # Create a new instance of {ActiveInteractor::Result}
  #
  # @visibility private
  #
  # @param status [Integer] the status code for the result
  # @param data [ActiveInteractor::Context::Result, Object, nil] the data returned by the interactor
  #
  # @return [ActiveInteractor::Result] the result
  def initialize(status:, data: {})
    @status = status
    @data = data
    @errors = ActiveModel::Errors.new(self)
  end

  # Whether or not the result is a failure
  #
  # @example When the result is a success
  #  result = CreateUser.perform(
  #    email: 'hello@aaronmallen',
  #    password: 'password',
  #    password_confirmation: 'password'
  #  )
  #  result.failure?
  #  #=> false
  #
  # @example When the result is a failure
  #  result = CreateUser.perform(
  #    email: 'hello@aaronmallen',
  #    password: 'password',
  #    password_confirmation: 'not password'
  #  )
  #  result.failure?
  #  #=> true
  #
  # @return [Boolean] whether or not the result is a failure
  def failure?
    !success?
  end
  alias failed? failure?

  # Needed for ActiveModel::Errors
  #
  # @visibility private
  def read_attribute_for_validation(attribute_name)
    data&.send(attribute_name.to_sym)
  end

  # Whether or not the result is a success
  #
  # @example When the result is a success
  #  result = CreateUser.perform(
  #    email: 'hello@aaronmallen',
  #    password: 'password',
  #    password_confirmation: 'password'
  #  )
  #  result.success?
  #  #=> true
  #
  # @example When the result is a failure
  #  result = CreateUser.perform(
  #    email: 'hello@aaronmallen',
  #    password: 'password',
  #    password_confirmation: 'not password'
  #  )
  #  result.success?
  #  #=> false
  #
  # @return [Boolean] whether or not the result is a failure
  def success?
    @status == STATUS[:success]
  end
  alias successful? success?

  # Return the result as a hash
  #
  # @example
  #  result = CreateUser.perform(email: 'hello@aaronmallen', password: 'password')
  #  result.to_hash
  #  #=> { success: true, status: 0, errors: {}, data: { user: <#User> } }
  def to_hash
    {
      success: success?,
      status: @status,
      errors: errors.to_hash,
      data: data.to_json
    }
  end
  alias to_h to_hash
end

#errorsActiveModel::Errors (readonly)

Returns the errors from the interactor.

Examples:

result = CreateUser.perform(
  email: 'hello@aaronmallen',
  password: 'password',
  password_confirmation: 'not password'
)
result.errors.full_messages
#=> ["Password confirmation doesn't match Password"]

Returns:

  • (ActiveModel::Errors)

    the errors from the interactor



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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/active_interactor/result.rb', line 25

class Result
  extend ActiveModel::Naming

  attr_reader :data, :errors

  # Return a result as a JSON string
  delegate :to_json, to: :to_hash

  # Status codes for interactor results
  #
  # @visibility private
  STATUS = {
    success: 0,
    failed_at_input: 1,
    failed_at_runtime: 2,
    failed_at_output: 3
  }.freeze

  class << self
    # Return a failed result
    #
    # @visibility private
    #
    # @param data [ActiveInteractor::Context::Result, Object, nil] the data returned by the interactor
    # @param errors [ActiveModel::Errors, Hash, String] the errors from the interactor
    # @param status [Integer] the status code for the result
    #
    # @return [ActiveInteractor::Result] the failed result
    def failure(data: {}, errors: {}, status: STATUS[:failed_at_runtime])
      result = new(status: status, data: data)
      parse_errors(errors).each_pair do |attribute, messages|
        Array.wrap(messages).each { |message| result.errors.add(attribute, message) }
      end
      result
    end

    # Needed for ActiveModel::Errors
    #
    # @visibility private
    def human_attribute_name(attribute, _options = {})
      attribute.respond_to?(:to_s) ? attribute.to_s.humanize : attribute
    end

    # Needed for ActiveModel::Errors
    #
    # @visibility private
    def lookup_ancestors
      [self]
    end

    # Return a successful result
    #
    # @visibility private
    #
    # @param data [ActiveInteractor::Context::Result, Object, nil] the data returned by the interactor
    #
    # @return [ActiveInteractor::Result] the successful result
    def success(data: {})
      new(status: STATUS[:success], data: data)
    end

    private

    def parse_errors(errors)
      case errors
      when String
        { generic: [errors] }
      when ActiveModel::Errors
        errors.to_hash
      else
        errors
      end
    end
  end

  private_class_method :new

  # Create a new instance of {ActiveInteractor::Result}
  #
  # @visibility private
  #
  # @param status [Integer] the status code for the result
  # @param data [ActiveInteractor::Context::Result, Object, nil] the data returned by the interactor
  #
  # @return [ActiveInteractor::Result] the result
  def initialize(status:, data: {})
    @status = status
    @data = data
    @errors = ActiveModel::Errors.new(self)
  end

  # Whether or not the result is a failure
  #
  # @example When the result is a success
  #  result = CreateUser.perform(
  #    email: 'hello@aaronmallen',
  #    password: 'password',
  #    password_confirmation: 'password'
  #  )
  #  result.failure?
  #  #=> false
  #
  # @example When the result is a failure
  #  result = CreateUser.perform(
  #    email: 'hello@aaronmallen',
  #    password: 'password',
  #    password_confirmation: 'not password'
  #  )
  #  result.failure?
  #  #=> true
  #
  # @return [Boolean] whether or not the result is a failure
  def failure?
    !success?
  end
  alias failed? failure?

  # Needed for ActiveModel::Errors
  #
  # @visibility private
  def read_attribute_for_validation(attribute_name)
    data&.send(attribute_name.to_sym)
  end

  # Whether or not the result is a success
  #
  # @example When the result is a success
  #  result = CreateUser.perform(
  #    email: 'hello@aaronmallen',
  #    password: 'password',
  #    password_confirmation: 'password'
  #  )
  #  result.success?
  #  #=> true
  #
  # @example When the result is a failure
  #  result = CreateUser.perform(
  #    email: 'hello@aaronmallen',
  #    password: 'password',
  #    password_confirmation: 'not password'
  #  )
  #  result.success?
  #  #=> false
  #
  # @return [Boolean] whether or not the result is a failure
  def success?
    @status == STATUS[:success]
  end
  alias successful? success?

  # Return the result as a hash
  #
  # @example
  #  result = CreateUser.perform(email: 'hello@aaronmallen', password: 'password')
  #  result.to_hash
  #  #=> { success: true, status: 0, errors: {}, data: { user: <#User> } }
  def to_hash
    {
      success: success?,
      status: @status,
      errors: errors.to_hash,
      data: data.to_json
    }
  end
  alias to_h to_hash
end

Instance Method Details

#failure?Boolean Also known as: failed?

Whether or not the result is a failure

Examples:

When the result is a success

result = CreateUser.perform(
  email: 'hello@aaronmallen',
  password: 'password',
  password_confirmation: 'password'
)
result.failure?
#=> false

When the result is a failure

result = CreateUser.perform(
  email: 'hello@aaronmallen',
  password: 'password',
  password_confirmation: 'not password'
)
result.failure?
#=> true

Returns:

  • (Boolean)

    whether or not the result is a failure



137
138
139
# File 'lib/active_interactor/result.rb', line 137

def failure?
  !success?
end

#success?Boolean Also known as: successful?

Whether or not the result is a success

Examples:

When the result is a success

result = CreateUser.perform(
  email: 'hello@aaronmallen',
  password: 'password',
  password_confirmation: 'password'
)
result.success?
#=> true

When the result is a failure

result = CreateUser.perform(
  email: 'hello@aaronmallen',
  password: 'password',
  password_confirmation: 'not password'
)
result.success?
#=> false

Returns:

  • (Boolean)

    whether or not the result is a failure



170
171
172
# File 'lib/active_interactor/result.rb', line 170

def success?
  @status == STATUS[:success]
end

#to_hashObject Also known as: to_h

Return the result as a hash

Examples:

result = CreateUser.perform(email: 'hello@aaronmallen', password: 'password')
result.to_hash
#=> { success: true, status: 0, errors: {}, data: { user: <#User> } }


181
182
183
184
185
186
187
188
# File 'lib/active_interactor/result.rb', line 181

def to_hash
  {
    success: success?,
    status: @status,
    errors: errors.to_hash,
    data: data.to_json
  }
end