Class: ActiveUtils::Validateable::Errors

Inherits:
HashWithIndifferentAccess
  • Object
show all
Defined in:
lib/active_utils/validateable.rb

Overview

This hash keeps the errors of the object

Instance Method Summary collapse

Constructor Details

#initialize(base) ⇒ Errors

Returns a new instance of Errors.


33
34
35
36
# File 'lib/active_utils/validateable.rb', line 33

def initialize(base)
  super() { |h, k|  h[k] = [] ; h[k] }
  @base = base
end

Instance Method Details

#add(field, error) ⇒ Object


53
54
55
# File 'lib/active_utils/validateable.rb', line 53

def add(field, error)
  self[field] << error
end

#add_to_base(error) ⇒ Object


57
58
59
# File 'lib/active_utils/validateable.rb', line 57

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

#countObject


38
39
40
# File 'lib/active_utils/validateable.rb', line 38

def count
  size
end

#each_fullObject


61
62
63
# File 'lib/active_utils/validateable.rb', line 61

def each_full
  full_messages.each { |msg| yield msg }
end

#empty?Boolean

Returns:

  • (Boolean)

42
43
44
# File 'lib/active_utils/validateable.rb', line 42

def empty?
  all? { |k, v| v && v.empty? }
end

#full_messagesObject


65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/active_utils/validateable.rb', line 65

def full_messages
  result = []

  self.each do |key, messages|
    next if messages.blank?
    if key == 'base'
      result << "#{messages.first}"
    else
      result << "#{key.to_s.humanize} #{messages.first}"
    end
  end

  result
end

#on(field) ⇒ Object

returns a specific fields error message. if more than one error is available we will only return the first. If no error is available we return an empty string


49
50
51
# File 'lib/active_utils/validateable.rb', line 49

def on(field)
  self[field].to_a.first
end