Class: ActiveInteractor::Result

Inherits:
Object
  • Object
show all
Includes:
ActiveModelErrorMethods
Defined in:
lib/active_interactor/result.rb

Overview

The object returned by an interactor

Constant Summary collapse

STATUS =
{
  success: 0,
  failed_at_input: 1,
  failed_at_runtime: 2,
  failed_at_output: 3
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(status:, data: {}) ⇒ Result

Returns a new instance of Result.



102
103
104
105
106
# File 'lib/active_interactor/result.rb', line 102

def initialize(status:, data: {})
  @status = status
  @data = data
  @errors = ActiveModel::Errors.new(self)
end

Instance Attribute Details

#dataActiveInteractor::Context::Result (readonly)

Returns the data returned by the interactor.

Examples:

Given an interactor that creates an ActiveRecord User

class CreateUser < ActiveInteractor::Interactor::Base
  argument :login, String, 'The login for the User', required: true
  argument :password, String, 'The password for the User', required: true
  argument :password_confirmation, String, 'The password confirmation for the user', required: true

  returns :user, User, 'The created User', required: true

  def interact
    context.user = User.new(context)
    fail!(context.user.errors) unless context.user.save
  end
end

result = CreateUser.perform(login: 'johndoe', password: 'password', password_confirmation: 'password')
result.data.user

#=> <# User @login='johndoe'>

Returns:



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
# File 'lib/active_interactor/result.rb', line 34

class Result
  include ActiveModelErrorMethods

  # @!method to_json
  # The {ActiveInteractor::Interactor::Base result} as a Hash
  #
  # @example When an {ActiveInteractor::Interactor::Base interactor} succeeds
  #  result = CreateUser.perform(login: 'johndoe', password: 'password', password_confirmation: 'password')
  #  result.to_hash
  #
  #  #=> { :success => true, :errors => {}, :data => { :login => 'johndoe' } }
  #
  # @example When an {ActiveInteractor::Interactor::Base interactor} fails
  #  result = CreateUser.perform(login: 'johndoe', password: 'password', password_confirmation: 'notpassword')
  #  result.to_hash
  #
  #  #=> {
  #  #=>   :success => false,
  #  #=>   :errors => { :password_confirmation => ["doesn't match Password"] },
  #  #=>   :data => { :login => 'johndoe' }
  #  #=> }
  #
  # @deprecated will be removed in version 2.0.0-alpha.3.0.0
  #  use {#to_hash} instead
  # @return [Hash {Symbol => Boolean, Hash}]
  delegate :to_json, to: :to_hash

  # @private
  STATUS = {
    success: 0,
    failed_at_input: 1,
    failed_at_runtime: 2,
    failed_at_output: 3
  }.freeze

  attr_reader :data

  class << self
    # @private
    def success(data: {})
      new(status: STATUS[:success], data: data)
    end

    # @private
    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

    private

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

  private_class_method :new
  # @private
  def initialize(status:, data: {})
    @status = status
    @data = data
    @errors = ActiveModel::Errors.new(self)
  end

  # Whether or not the {ActiveInteractor::Interactor::Base result} is a failure
  #
  # @example When an {ActiveInteractor::Interactor::Base interactor} fails
  #  result = CreateUser.perform(login: 'johndoe', password: 'password', password_confirmation: 'notpassword')
  #  result.failure?
  #
  #  #=> true
  #
  # @example When an {ActiveInteractor::Interactor::Base interactor} succeeds
  #  result = CreateUser.perform(login: 'johndoe', password: 'password', password_confirmation: 'password')
  #  result.failure?
  #
  #  #=> false
  #
  # @return [Boolean]
  def failure?
    !success?
  end
  alias failed? failure?

  # @private
  def read_attribute_for_validation(attribute_name)
    data.send(attribute_name.to_sym)
  end

  # Whether or not the {ActiveInteractor::Interactor::Base result} is a success
  #
  # @return [Boolean]
  def success?
    @status == STATUS[:success]
  end
  alias successful? success?

  # The {ActiveInteractor::Interactor::Base result} as a Hash
  #
  # @example When an {ActiveInteractor::Interactor::Base interactor} succeeds
  #  result = CreateUser.perform(login: 'johndoe', password: 'password', password_confirmation: 'password')
  #  result.to_hash
  #
  #  #=> { :success => true, :errors => {}, :data => { :login => 'johndoe' } }
  #
  # @example When an {ActiveInteractor::Interactor::Base interactor} fails
  #  result = CreateUser.perform(login: 'johndoe', password: 'password', password_confirmation: 'notpassword')
  #  result.to_hash
  #
  #  #=> {
  #  #=>   :success => false,
  #  #=>   :errors => { :password_confirmation => ["doesn't match Password"] },
  #  #=>   :data => { :login => 'johndoe' }
  #  #=> }
  #
  # @return [Hash {Symbol => Boolean, Hash}]
  def to_hash
    {
      success: success?,
      errors: errors.to_hash,
      data: data.to_json
    }
  end
  alias to_h to_hash
end

#errorsActiveModel::Errors (readonly)

Returns the errors returned by the interactor.

Returns:

  • (ActiveModel::Errors)

    the errors returned by the interactor

See Also:



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
# File 'lib/active_interactor/result.rb', line 34

class Result
  include ActiveModelErrorMethods

  # @!method to_json
  # The {ActiveInteractor::Interactor::Base result} as a Hash
  #
  # @example When an {ActiveInteractor::Interactor::Base interactor} succeeds
  #  result = CreateUser.perform(login: 'johndoe', password: 'password', password_confirmation: 'password')
  #  result.to_hash
  #
  #  #=> { :success => true, :errors => {}, :data => { :login => 'johndoe' } }
  #
  # @example When an {ActiveInteractor::Interactor::Base interactor} fails
  #  result = CreateUser.perform(login: 'johndoe', password: 'password', password_confirmation: 'notpassword')
  #  result.to_hash
  #
  #  #=> {
  #  #=>   :success => false,
  #  #=>   :errors => { :password_confirmation => ["doesn't match Password"] },
  #  #=>   :data => { :login => 'johndoe' }
  #  #=> }
  #
  # @deprecated will be removed in version 2.0.0-alpha.3.0.0
  #  use {#to_hash} instead
  # @return [Hash {Symbol => Boolean, Hash}]
  delegate :to_json, to: :to_hash

  # @private
  STATUS = {
    success: 0,
    failed_at_input: 1,
    failed_at_runtime: 2,
    failed_at_output: 3
  }.freeze

  attr_reader :data

  class << self
    # @private
    def success(data: {})
      new(status: STATUS[:success], data: data)
    end

    # @private
    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

    private

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

  private_class_method :new
  # @private
  def initialize(status:, data: {})
    @status = status
    @data = data
    @errors = ActiveModel::Errors.new(self)
  end

  # Whether or not the {ActiveInteractor::Interactor::Base result} is a failure
  #
  # @example When an {ActiveInteractor::Interactor::Base interactor} fails
  #  result = CreateUser.perform(login: 'johndoe', password: 'password', password_confirmation: 'notpassword')
  #  result.failure?
  #
  #  #=> true
  #
  # @example When an {ActiveInteractor::Interactor::Base interactor} succeeds
  #  result = CreateUser.perform(login: 'johndoe', password: 'password', password_confirmation: 'password')
  #  result.failure?
  #
  #  #=> false
  #
  # @return [Boolean]
  def failure?
    !success?
  end
  alias failed? failure?

  # @private
  def read_attribute_for_validation(attribute_name)
    data.send(attribute_name.to_sym)
  end

  # Whether or not the {ActiveInteractor::Interactor::Base result} is a success
  #
  # @return [Boolean]
  def success?
    @status == STATUS[:success]
  end
  alias successful? success?

  # The {ActiveInteractor::Interactor::Base result} as a Hash
  #
  # @example When an {ActiveInteractor::Interactor::Base interactor} succeeds
  #  result = CreateUser.perform(login: 'johndoe', password: 'password', password_confirmation: 'password')
  #  result.to_hash
  #
  #  #=> { :success => true, :errors => {}, :data => { :login => 'johndoe' } }
  #
  # @example When an {ActiveInteractor::Interactor::Base interactor} fails
  #  result = CreateUser.perform(login: 'johndoe', password: 'password', password_confirmation: 'notpassword')
  #  result.to_hash
  #
  #  #=> {
  #  #=>   :success => false,
  #  #=>   :errors => { :password_confirmation => ["doesn't match Password"] },
  #  #=>   :data => { :login => 'johndoe' }
  #  #=> }
  #
  # @return [Hash {Symbol => Boolean, Hash}]
  def to_hash
    {
      success: success?,
      errors: errors.to_hash,
      data: data.to_json
    }
  end
  alias to_h to_hash
end

Class Method Details

.failure(data: {}, errors: {}, status: STATUS[:failed_at_runtime]) ⇒ Object



78
79
80
81
82
83
84
# File 'lib/active_interactor/result.rb', line 78

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

.success(data: {}) ⇒ Object



73
74
75
# File 'lib/active_interactor/result.rb', line 73

def success(data: {})
  new(status: STATUS[:success], data: data)
end

Instance Method Details

#failure?Boolean Also known as: failed?

Whether or not the result is a failure

Examples:

When an interactor fails

result = CreateUser.perform(login: 'johndoe', password: 'password', password_confirmation: 'notpassword')
result.failure?

#=> true

When an interactor succeeds

result = CreateUser.perform(login: 'johndoe', password: 'password', password_confirmation: 'password')
result.failure?

#=> false

Returns:

  • (Boolean)


123
124
125
# File 'lib/active_interactor/result.rb', line 123

def failure?
  !success?
end

#read_attribute_for_validation(attribute_name) ⇒ Object



129
130
131
# File 'lib/active_interactor/result.rb', line 129

def read_attribute_for_validation(attribute_name)
  data.send(attribute_name.to_sym)
end

#success?Boolean Also known as: successful?

Whether or not the result is a success

Returns:

  • (Boolean)


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

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

#to_hashHash {Symbol => Boolean, Hash} Also known as: to_h

The result as a Hash

Examples:

When an interactor succeeds

result = CreateUser.perform(login: 'johndoe', password: 'password', password_confirmation: 'password')
result.to_hash

#=> { :success => true, :errors => {}, :data => { :login => 'johndoe' } }

When an interactor fails

result = CreateUser.perform(login: 'johndoe', password: 'password', password_confirmation: 'notpassword')
result.to_hash

#=> {
#=>   :success => false,
#=>   :errors => { :password_confirmation => ["doesn't match Password"] },
#=>   :data => { :login => 'johndoe' }
#=> }

Returns:

  • (Hash {Symbol => Boolean, Hash})


160
161
162
163
164
165
166
# File 'lib/active_interactor/result.rb', line 160

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

#to_jsonHash {Symbol => Boolean, Hash}

Deprecated.

will be removed in version 2.0.0-alpha.3.0.0 use #to_hash instead

The result as a Hash

Examples:

When an interactor succeeds

result = CreateUser.perform(login: 'johndoe', password: 'password', password_confirmation: 'password')
result.to_hash

#=> { :success => true, :errors => {}, :data => { :login => 'johndoe' } }

When an interactor fails

result = CreateUser.perform(login: 'johndoe', password: 'password', password_confirmation: 'notpassword')
result.to_hash

#=> {
#=>   :success => false,
#=>   :errors => { :password_confirmation => ["doesn't match Password"] },
#=>   :data => { :login => 'johndoe' }
#=> }

Returns:

  • (Hash {Symbol => Boolean, Hash})


59
# File 'lib/active_interactor/result.rb', line 59

delegate :to_json, to: :to_hash