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



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

#for_attribute(attr) ⇒ Object



29
30
31
# File 'lib/action_processor/errors.rb', line 29

def for_attribute(attr)
  @grouped_by_attribute[attr] 
end

#grouped_by_attributeObject



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/action_processor/errors.rb', line 33

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



19
20
21
22
23
24
25
# File 'lib/action_processor/errors.rb', line 19

def messages
  all_messages = []
  @all.each do |e|
    all_messages += e[:messages]
  end
  all_messages
end