Module: Metaractor::ContextErrors

Defined in:
lib/metaractor/context_errors.rb

Instance Method Summary collapse

Instance Method Details

#add_error(message: nil, errors: nil, **args) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/metaractor/context_errors.rb', line 21

def add_error(message: nil, errors: nil, **args)
  if message.nil?
    add_errors(errors: errors, **args)
  else
    add_errors(messages: Array(message), **args)
  end
end

#add_errors(messages: [], errors: {}, **args) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/metaractor/context_errors.rb', line 29

def add_errors(messages: [], errors: {}, **args)
  if !messages.empty?
    self.errors.add(errors: {base: messages}, **args)
  else
    self.errors.add(errors: errors, **args)
  end
end

#error_messagesObject



37
38
39
# File 'lib/metaractor/context_errors.rb', line 37

def error_messages
  errors.full_messages
end

#errorsObject



3
4
5
6
7
8
9
# File 'lib/metaractor/context_errors.rb', line 3

def errors
  if super.nil?
    self.errors = Metaractor::Errors.new
  end

  super
end

#fail_with_error!(message: nil, errors: nil, **args) ⇒ Object



11
12
13
14
# File 'lib/metaractor/context_errors.rb', line 11

def fail_with_error!(message: nil, errors: nil, **args)
  add_error(message: message, errors: errors, **args)
  fail!
end

#fail_with_errors!(messages: [], errors: {}, **args) ⇒ Object



16
17
18
19
# File 'lib/metaractor/context_errors.rb', line 16

def fail_with_errors!(messages: [], errors: {}, **args)
  add_errors(messages: messages, errors: errors, **args)
  fail!
end