Class: ActionProcessor::Errors
- Inherits:
-
Object
- Object
- ActionProcessor::Errors
- Defined in:
- lib/action_processor/errors.rb
Instance Attribute Summary collapse
-
#all ⇒ Object
readonly
Returns the value of attribute all.
Instance Method Summary collapse
- #add(messages, step = :not_specified, attribute = :not_specified) ⇒ Object
- #concat(more_errors) ⇒ Object
- #for_attribute(attr) ⇒ Object
- #grouped_by_attribute ⇒ Object
-
#initialize ⇒ Errors
constructor
A new instance of Errors.
-
#messages ⇒ Object
(also: #full_messages)
returns array of strings with user friendly error messages.
Constructor Details
#initialize ⇒ Errors
Returns a new instance of Errors.
9 10 11 |
# File 'lib/action_processor/errors.rb', line 9 def initialize @all = [] end |
Instance Attribute Details
#all ⇒ Object (readonly)
Returns the value of attribute all.
7 8 9 |
# File 'lib/action_processor/errors.rb', line 7 def all @all end |
Instance Method Details
#add(messages, step = :not_specified, attribute = :not_specified) ⇒ Object
13 14 15 16 |
# File 'lib/action_processor/errors.rb', line 13 def add(, step = :not_specified, attribute = :not_specified) step ||= :not_specified # in case we will receive explicit nil as step parameter @all << {messages: [].flatten, step: step.to_sym, attribute: attribute.to_sym} end |
#concat(more_errors) ⇒ Object
18 19 20 21 |
# File 'lib/action_processor/errors.rb', line 18 def concat(more_errors) raise ArgumentError, "Expected an ActionProcessor::Errors object" unless more_errors.is_a?(ActionProcessor::Errors) @all += more_errors.all end |
#for_attribute(attr) ⇒ Object
34 35 36 |
# File 'lib/action_processor/errors.rb', line 34 def for_attribute(attr) @grouped_by_attribute[attr] end |
#grouped_by_attribute ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/action_processor/errors.rb', line 38 def grouped_by_attribute return @grouped_by_attribute if @grouped_by_attribute.present? # we assume that all errors will be present # at the time when this method called first time @grouped_by_attribute = {}.with_indifferent_access @all.each do |err| @grouped_by_attribute[err.attribute] ||= [] @grouped_by_attribute[err.attribute] += err[:messages] end @grouped_by_attribute end |
#messages ⇒ Object Also known as: full_messages
returns array of strings with user friendly error messages
24 25 26 27 28 29 30 |
# File 'lib/action_processor/errors.rb', line 24 def = [] @all.each do |e| += e[:messages] end end |