Class: EasyCommand::Errors
- Inherits:
-
Hash
- Object
- Hash
- EasyCommand::Errors
- Defined in:
- lib/easy_command/errors.rb
Instance Attribute Summary collapse
-
#source ⇒ Object
readonly
Returns the value of attribute source.
Instance Method Summary collapse
- #add(attribute, code, message_or_key = nil, **options) ⇒ Object
- #add_multiple_errors(errors_hash) ⇒ Object
- #exists?(attribute, code) ⇒ Boolean (also: #has_error?)
- #full_message(attribute, message) ⇒ Object
-
#full_messages ⇒ Object
For SimpleCommand gem compatibility, to ease migration.
-
#initialize(source: nil) ⇒ Errors
constructor
A new instance of Errors.
- #merge_from(object) ⇒ Object
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
#source ⇒ Object (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, = nil, **) code = code.to_sym ||= code if defined?(I18n) # Can't use `I18n.exists?` because it doesn't accept a scope: kwarg = begin I18n.t!(, scope: source&.i18n_scope, **) rescue I18n::MissingTranslationData nil end end ||= self[attribute] ||= [] self[attribute] << { code: code, 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] = value[:message] else code = value.first = value.last || value.first end add(key, code, ) end end end |
#exists?(attribute, code) ⇒ Boolean Also known as: has_error?
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 (attribute, ) return if attribute == :base attr_name = attribute.to_s.tr('.', '_').capitalize format("%s %s", attr_name, ) end |
#full_messages ⇒ Object
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 = [] each do |attribute, errors| errors.each do |error| << (attribute, error[:message]) end end end |
#merge_from(object) ⇒ Object
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..each_with_object({}) do |(attribute, ), object_errors| object_errors[attribute] = . zip(object.errors.details[attribute]). map { |, detail| [detail[:error], ] } end else object.errors end add_multiple_errors(errors) end |