Class: Appfuel::Errors

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/appfuel/errors.rb

Overview

Feature handler, action handler, command handler all use this class. Presenters and validators will have there errors tranformed into this. Errors are a basic hash structure where each key has an array of strings that represent error messages.

Example

messages: {
  name: [
    'must be present',
    'can not be blank',
    'can not be Bob'
  ]
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(messages = {}) ⇒ Errors

Returns a new instance of Errors.



19
20
21
22
# File 'lib/appfuel/errors.rb', line 19

def initialize(messages = {})
  @messages = messages || {}
  @messages.stringify_keys! unless @messages.empty?
end

Instance Attribute Details

#messagesObject (readonly)

Returns the value of attribute messages.



17
18
19
# File 'lib/appfuel/errors.rb', line 17

def messages
  @messages
end

Instance Method Details

#[](key) ⇒ Object



72
73
74
# File 'lib/appfuel/errors.rb', line 72

def [](key)
  messages[key.to_s]
end

#add(key, msg) ⇒ Object

Add an error message to a given key

Parameters:

  • key

    Symbol key for this message

  • msg

    String the message to be stored



36
37
38
39
40
41
# File 'lib/appfuel/errors.rb', line 36

def add(key, msg)
  key = key.to_s
  msg = msg.to_s
  messages[key] = [] unless messages.key?(key)
  messages[key] << msg unless messages[key].include?(msg)
end

#clearObject



88
89
90
# File 'lib/appfuel/errors.rb', line 88

def clear
  messages.clear
end

#delete(key) ⇒ Object



68
69
70
# File 'lib/appfuel/errors.rb', line 68

def delete(key)
  messages.delete(key.to_s)
end

#eachObject

Defined to use Enumerable so that we can treat errors as an iterator



26
27
28
29
30
# File 'lib/appfuel/errors.rb', line 26

def each
  messages.each do|key, msgs|
    yield key, msgs
  end
end

#empty?Boolean

Returns:

  • (Boolean)


92
93
94
# File 'lib/appfuel/errors.rb', line 92

def empty?
  messages.empty?
end

#format(msg_separator = "\n", list_separator = "\n") ⇒ Object

Formats the list of messages for each key

Example

messages: {
  name: [
    ' must be present ',
    ' can not be blank ',
    ' can not be Bob '
  ]
}

note: spaces are used only for readability name: must be present n can not be blank n can not be Bob n n

Parameters:

  • msg_separator (defaults to: "\n")

    String separates each message default n

  • list_separator (defaults to: "\n")

    String separates each list of messages

Returns:

  • String



60
61
62
63
64
65
66
# File 'lib/appfuel/errors.rb', line 60

def format(msg_separator = "\n", list_separator = "\n")
  msg = ''
  each do |key, list|
    msg << "#{key}: #{list.join(msg_separator)}#{list_separator}"
  end
  msg
end

#keysObject



84
85
86
# File 'lib/appfuel/errors.rb', line 84

def keys
  messages.keys
end

#sizeObject



76
77
78
# File 'lib/appfuel/errors.rb', line 76

def size
  messages.length
end

#to_hObject



96
97
98
# File 'lib/appfuel/errors.rb', line 96

def to_h
  {errors: messages}
end

#to_sObject



100
101
102
# File 'lib/appfuel/errors.rb', line 100

def to_s
  format
end

#valuesObject



80
81
82
# File 'lib/appfuel/errors.rb', line 80

def values
  messages.values
end