Class: Roart::Errors

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/roart/validations.rb

Instance Method Summary collapse

Constructor Details

#initialize(obj) ⇒ Errors

Returns a new instance of Errors.



6
7
8
# File 'lib/roart/validations.rb', line 6

def initialize(obj)
  @base, @errors = obj, {}
end

Instance Method Details

#add(field, message) ⇒ Object



14
15
16
17
# File 'lib/roart/validations.rb', line 14

def add(field, message)
  @errors[field.to_sym] ||= []
  @errors[field.to_sym] << message
end

#add_to_base(msg) ⇒ Object



10
11
12
# File 'lib/roart/validations.rb', line 10

def add_to_base(msg)
  add(:base, msg)
end

#clearObject

Removes all errors that have been added.



41
42
43
# File 'lib/roart/validations.rb', line 41

def clear
  @errors = {}
end

#eachObject



31
32
33
# File 'lib/roart/validations.rb', line 31

def each
  @errors.each_key { |attr| @errors[attr].each { |msg| yield attr, msg } }
end

#empty?Boolean

Returns true if no errors have been added.

Returns:

  • (Boolean)


36
37
38
# File 'lib/roart/validations.rb', line 36

def empty?
  @errors.empty?
end

#on(field) ⇒ Object Also known as: []



23
24
25
26
27
# File 'lib/roart/validations.rb', line 23

def on(field)
  errors = @errors[field.to_sym]
  return nil if errors.nil?
  errors
end

#on_baseObject



19
20
21
# File 'lib/roart/validations.rb', line 19

def on_base
  on(:base)
end

#sizeObject Also known as: count, length

Returns the total number of errors added. Two errors added to the same attribute will be counted as such.



46
47
48
# File 'lib/roart/validations.rb', line 46

def size
  @errors.values.inject(0) { |error_count, attribute| error_count + attribute.size }
end