Class: Merb::Authentication::Errors

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/merb-auth-core/errors.rb

Overview

Lifted from DataMapper’s dm-validations plugin :)

Author:

  • Guy van den Berg

Since:

  • DM 0.9

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &block) ⇒ Object

Since:

  • DM 0.9



66
67
68
# File 'lib/merb-auth-core/errors.rb', line 66

def method_missing(meth, *args, &block)
  errors.send(meth, *args, &block)
end

Instance Method Details

#[](field_name) ⇒ Array<Array<String>>

Return validation errors for a particular field name or an empty array

This method is a necessary requirement for active_model compatibility.

Parameters:

  • field_name (Symbol)

    the name of the field you want an error for

Returns:

  • (Array<Array<String>>)

    array of validation errors or empty array, if there are no errors on given field

Since:

  • DM 0.9



43
44
45
# File 'lib/merb-auth-core/errors.rb', line 43

def [](field_name)
  errors[field_name] ||= []
end

#add(field_name, message) ⇒ Object

Add a authentication error. Use the field_name :general if the errors does not apply to a specific field of the Resource.

Parameters:

  • field_name (Symbol)

    the name of the field that caused the error

  • message (String)

    the message to add

Since:

  • DM 0.9



25
26
27
# File 'lib/merb-auth-core/errors.rb', line 25

def add(field_name, message)
  (errors[field_name] ||= []) << message
end

#clear!Object

Clear existing authentication errors.

Since:

  • DM 0.9



16
17
18
# File 'lib/merb-auth-core/errors.rb', line 16

def clear!
  errors.clear
end

#eachObject

Since:

  • DM 0.9



55
56
57
58
59
60
# File 'lib/merb-auth-core/errors.rb', line 55

def each
  errors.map.each do |k,v|
    next if v.blank?
    yield(v)
  end
end

#empty?Boolean

Returns:

  • (Boolean)

Since:

  • DM 0.9



62
63
64
# File 'lib/merb-auth-core/errors.rb', line 62

def empty?
  entries.empty?
end

#full_messagesObject

Collect all errors into a single list.

Since:

  • DM 0.9



30
31
32
33
34
# File 'lib/merb-auth-core/errors.rb', line 30

def full_messages
  errors.inject([]) do |list,pair|
    list += pair.last
  end
end

#on(field_name) ⇒ Object

Return authentication errors for a particular field_name.

Parameters:

  • field_name (Symbol)

    the name of the field you want an error for

Since:

  • DM 0.9



50
51
52
53
# File 'lib/merb-auth-core/errors.rb', line 50

def on(field_name)
  errors_for_field = errors[field_name]
  errors_for_field.blank? ? nil : errors_for_field
end