Class: Cardia::Validateable::Error

Inherits:
Hash
  • Object
show all
Defined in:
lib/cardia/validateable.rb

Overview

This hash keeps the errors of the object

Instance Method Summary collapse

Constructor Details

#initialize(base) ⇒ Error

:nodoc:



35
36
37
# File 'lib/cardia/validateable.rb', line 35

def initialize(base)
  @base = base
end

Instance Method Details

#add(field, error) ⇒ Object



50
51
52
53
# File 'lib/cardia/validateable.rb', line 50

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

#add_to_base(error) ⇒ Object



55
56
57
# File 'lib/cardia/validateable.rb', line 55

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

#countObject



39
40
41
# File 'lib/cardia/validateable.rb', line 39

def count
  size
end

#each_fullObject



59
60
61
# File 'lib/cardia/validateable.rb', line 59

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

#full_messagesObject



63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/cardia/validateable.rb', line 63

def full_messages
  result = []

  self.each do |key, messages| 
    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



46
47
48
# File 'lib/cardia/validateable.rb', line 46

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