Class: Volt::Errors

Inherits:
ReactiveHash show all
Defined in:
lib/volt/models/errors.rb

Instance Method Summary collapse

Methods inherited from ReactiveHash

#==, #[], #[]=, #blank?, #clear, #delete, #initialize, #inspect, #method_missing, #replace, #to_h, #to_json

Constructor Details

This class inherits a constructor from Volt::ReactiveHash

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Volt::ReactiveHash

Instance Method Details

#add(field, error) ⇒ Object



5
6
7
8
# File 'lib/volt/models/errors.rb', line 5

def add(field, error)
  field_errors = (self[field] ||= [])
  field_errors << error unless field_errors.include?(error)
end

#merge!(errors) ⇒ Object

Merge another set of errors in



11
12
13
14
15
16
17
18
19
# File 'lib/volt/models/errors.rb', line 11

def merge!(errors)
  if errors
    errors.each_pair do |field, messages|
      messages.each do |message|
        add(field, message)
      end
    end
  end
end

#to_sObject

Generate a string version of all of the errors



22
23
24
25
26
27
28
29
30
# File 'lib/volt/models/errors.rb', line 22

def to_s
  str = []

  each_pair do |field, error|
    str << "#{field} #{error}"
  end

  str.join(', ')
end