Exception: NotNaughty::Violation

Inherits:
RuntimeError
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable
Defined in:
lib/not_naughty/violation.rb

Overview

Container for failed validations.

Instance Method Summary collapse

Constructor Details

#initializeViolation

:nodoc:



13
14
15
# File 'lib/not_naughty/violation.rb', line 13

def initialize() #:nodoc:
  @errors = Hash.new {|h, k| h[k] = []}
end

Instance Method Details

#add(k, msg) ⇒ Object

Adds an error for the given attribute.



17
# File 'lib/not_naughty/violation.rb', line 17

def add(k, msg)  @errors[k] << msg end

#full_messagesObject

Returns an array of fully-formatted error messages.



20
21
22
23
24
25
# File 'lib/not_naughty/violation.rb', line 20

def full_messages
  @errors.inject([]) do |messages, k_errors| k, errors = *k_errors
    errors.each {|e| messages << eval(e.inspect.delete('\\') % k) }
    messages
  end
end

#on(attribute) ⇒ Object

Returns an array of evaluated error messages for given attribute.



28
29
30
31
32
# File 'lib/not_naughty/violation.rb', line 28

def on(attribute)
  @errors[attribute].map do |message|
    eval(message.inspect.delete('\\') % attribute)
  end
end