Class: SimplerCommand::Errors

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

Overview

Provides an Errors implementation similar to ActiveModel::Errors

Instance Method Summary collapse

Instance Method Details

#add(key, value, _opts = {}) ⇒ Object



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

def add(key, value, _opts = {})
  self[key] ||= []
  self[key] << value
  self[key].uniq!
end

#add_all(errors_hash) ⇒ Object



12
13
14
15
16
17
18
# File 'lib/simpler_command/errors.rb', line 12

def add_all(errors_hash)
  errors_hash.each do |key, values|
    Array(values).each do |value|
      add key, value
    end
  end
end

#as_json(options = nil) ⇒ Object

Allow ActiveSupport to render errors similar to ActiveModel::Errors



31
32
33
34
35
36
37
38
# File 'lib/simpler_command/errors.rb', line 31

def as_json(options = nil)
  {}.tap do |output|
    each do |field, value|
      output[field] ||= []
      output[field] << value
    end
  end.as_json(options)
end

#eachObject



20
21
22
23
24
# File 'lib/simpler_command/errors.rb', line 20

def each
  each_key do |field|
    self[field].each { |message| yield field, message }
  end
end

#full_messagesObject



26
27
28
# File 'lib/simpler_command/errors.rb', line 26

def full_messages
  map { |attribute, message| full_message(attribute, message) }
end