Module: Metaractor::Spec::Helpers

Defined in:
lib/metaractor/spec.rb

Instance Method Summary collapse

Instance Method Details

#context_creator(error_message: nil, error_messages: [], errors: [], valid: nil, invalid: nil, success: nil, failure: nil, **attributes) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/metaractor/spec.rb', line 7

def context_creator(error_message: nil, error_messages: [], errors: [], valid: nil, invalid: nil, success: nil, failure: nil, **attributes)
  if error_message.present?
    error_messages << error_message
  end

  result = Interactor::Context.build(attributes)
  result.add_errors(messages: error_messages)
  result.add_errors(errors: errors)

  if (!valid.nil? && !valid) || (!invalid.nil? && invalid)
    result.invalidate!
  end

  if !result.errors.empty? ||
      result.invalid? ||
      (!success.nil? && !success) ||
      (!failure.nil? && failure)
    begin
      result.fail!
    rescue
      Interactor::Failure
    end
  end

  result
end