Class: ActionProcessor::Errors

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeErrors

Returns a new instance of Errors.



9
10
11
# File 'lib/action_processor/errors.rb', line 9

def initialize
  @all = []
end

Instance Attribute Details

#allObject (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(messages, step = :not_specified, attribute = :not_specified)
  step ||= :not_specified # in case we will receive explicit nil as step parameter
  @all << {messages: [messages].flatten, step: step.to_sym, attribute: attribute.to_sym}
end

#concat(more_errors) ⇒ Object

Raises:

  • (ArgumentError)


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_attributeObject



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

#messagesObject 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 messages
  all_messages = []
  @all.each do |e|
    all_messages += e[:messages]
  end
  all_messages
end