Module: WithAccounts::ErrorsHelpers

Instance Method Summary collapse

Instance Method Details

#error_keyObject



3
4
5
# File 'lib/with_accounts/errors_helpers.rb', line 3

def error_key
  self.class.name.demodulize.to_sym
end

#format_errors(errors_hash) ⇒ Object

Raises:

  • (StandardError)


7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/with_accounts/errors_helpers.rb', line 7

def format_errors(errors_hash)
  raise StandardError unless errors_hash.present? && errors_hash.is_a?(Hash) && errors_hash.keys.size == 1

  errors_key = errors_hash.keys.first
  errors = errors_hash.values.flatten.first

  errors = errors.respond_to?(:to_h) ? errors.to_h : errors

  case errors
  when Hash
    sort_hash_errors(errors, errors_key)
  when Array
    sort_array_errors(errors, errors_key)
  when String
    sort_string_errors(errors, errors_key)
  else
    raise StandardError
  end
end