Class: EasyCommand::Errors

Inherits:
Hash
  • Object
show all
Defined in:
lib/easy_command/errors.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source: nil) ⇒ Errors

Returns a new instance of Errors.



7
8
9
10
# File 'lib/easy_command/errors.rb', line 7

def initialize(source: nil)
  @source = source
  super()
end

Instance Attribute Details

#sourceObject (readonly)

Returns the value of attribute source.



5
6
7
# File 'lib/easy_command/errors.rb', line 5

def source
  @source
end

Instance Method Details

#add(attribute, code, message_or_key = nil, **options) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/easy_command/errors.rb', line 17

def add(attribute, code, message_or_key = nil, **options)
  code = code.to_sym
  message_or_key ||= code

  if defined?(I18n)
    # Can't use `I18n.exists?` because it doesn't accept a scope: kwarg
    message =
      begin
        I18n.t!(message_or_key, scope: source&.i18n_scope, **options)
      rescue I18n::MissingTranslationData
        nil
      end
  end
  message ||= message_or_key

  self[attribute] ||= []
  self[attribute] << { code: code, message: message }
  self[attribute].uniq!
end

#add_multiple_errors(errors_hash) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/easy_command/errors.rb', line 53

def add_multiple_errors(errors_hash)
  errors_hash.each do |key, values|
    values.each do |value|
      if value.is_a?(Hash)
        code = value[:code]
        message_or_key = value[:message]
      else
        code = value.first
        message_or_key = value.last || value.first
      end
      add(key, code, message_or_key)
    end
  end
end

#exists?(attribute, code) ⇒ Boolean Also known as: has_error?

Returns:

  • (Boolean)


12
13
14
# File 'lib/easy_command/errors.rb', line 12

def exists?(attribute, code)
  fetch(attribute, []).any? { |e| e[:code] == code }
end

#full_message(attribute, message) ⇒ Object



79
80
81
82
83
# File 'lib/easy_command/errors.rb', line 79

def full_message(attribute, message)
  return message if attribute == :base
  attr_name = attribute.to_s.tr('.', '_').capitalize
  format("%s %s", attr_name, message)
end

#full_messagesObject

For SimpleCommand gem compatibility, to ease migration.



69
70
71
72
73
74
75
76
77
# File 'lib/easy_command/errors.rb', line 69

def full_messages
  messages = []
  each do |attribute, errors|
    errors.each do |error|
      messages << full_message(attribute, error[:message])
    end
  end
  messages
end

#merge_from(object) ⇒ Object

Raises:

  • (ArgumentError)


37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/easy_command/errors.rb', line 37

def merge_from(object)
  raise ArgumentError unless object.respond_to?(:errors)
  errors =
    if object.errors.respond_to?(:messages)
      object.errors.messages.each_with_object({}) do |(attribute, messages), object_errors|
        object_errors[attribute] = messages.
          zip(object.errors.details[attribute]).
          map { |message, detail| [detail[:error], message] }
      end
    else
      object.errors
    end

  add_multiple_errors(errors)
end