class Bodhi::Errors
Attributes
messages[RW]
Public Class Methods
new(errors={})
click to toggle source
# File lib/bodhi-slam/errors.rb, line 6 def initialize(errors={}) @messages = errors end
Public Instance Methods
[](attribute)
click to toggle source
When passed a symbol or a name of a method, returns an array of errors for the method.
user.errors[:name] # => ["is required"] user.errors['name'] # => ["is required"]
# File lib/bodhi-slam/errors.rb, line 82 def [](attribute) @messages[attribute.to_sym] end
add(name, message)
click to toggle source
Adds the given message
to the errors hash under the name
key
user.errors.add(:test, "has bad value") user.errors.any? # => true
# File lib/bodhi-slam/errors.rb, line 14 def add(name, message) @messages.has_key?(name) ? @messages[name].push(message) : @messages[name] = [message] end
clear()
click to toggle source
Clears all current error messages
user.errors.add(:name, "is wrong") user.errors.any? # => true user.errors.clear # => nil user.errors.any? # => false
# File lib/bodhi-slam/errors.rb, line 24 def clear @messages.clear end
each() { |attribute, error| ... }
click to toggle source
Iterates through each error key, value pair in the error messages hash. Yields the attribute and the error for that attribute. If the attribute has more than one error message, yields once for each error message.
user.errors.add(:test, "is required") user.errors.each do |attribute, error| # yields :test and "is required" end user.errors.add(:foo, "is awesome!") user.errors.each do |attribute, error| # yields :test and "is required" # then yields :foo and "is awesome!" end
# File lib/bodhi-slam/errors.rb, line 59 def each @messages.each_key do |attribute| @messages[attribute].each{ |error| yield attribute, error } end end
empty?()
click to toggle source
Returns true
if no errors are present, false
otherwise.
user.errors.add(:name, "test error") user.errors.empty? # => false
# File lib/bodhi-slam/errors.rb, line 102 def empty? size == 0 end
Also aliased as: blank?
full_messages()
click to toggle source
Returns an array of all error messages
user.errors.add(:name, "is wrong") user.errors.add(:address, "is not valid") user.errors.full_messages # => ["name is wrong", "address is not valid"]
# File lib/bodhi-slam/errors.rb, line 33 def full_messages results = [] @messages.each{ |key, values| values.each{ |value| results.push("#{key} #{value}") }} results end
Also aliased as: to_a
include?(attribute)
click to toggle source
Returns true
if the error messages include an error for the given key attribute
, false
otherwise.
user.errors.messages # => {:name=>["is required"]} user.errors.include?(:name) # => true user.errors.include?(:foo) # => false
# File lib/bodhi-slam/errors.rb, line 71 def include?(attribute) !@messages[attribute].nil? end
size()
click to toggle source
Returns the number of error messages.
user.errors.add(:name, "is required") user.errors.size # => 1 user.errors.add(:name, "can not be blank") user.errors.size # => 2
# File lib/bodhi-slam/errors.rb, line 93 def size full_messages.size end
Also aliased as: count
to_json()
click to toggle source
Converts the messages hash to json
# File lib/bodhi-slam/errors.rb, line 41 def to_json @messages.to_json end