Sha256: 6be2714f71804a0e04ee007dd6f3c1bb98b3f4a899d52ef85c96e0c7488909d0

Contents?: true

Size: 1.52 KB

Versions: 4

Compression:

Stored size: 1.52 KB

Contents

module Rails
  class Authentication
  
    def errors
      @errors ||= Errors.new
    end

    # Lifted from DataMapper's dm-validations plugin :)
    # @author Guy van den Berg
    # @since  DM 0.9
    class Errors

      include Enumerable

      # Clear existing authentication errors.
      def clear!
        errors.clear
      end

      # Add a authentication error. Use the field_name :general if the errors does
      # not apply to a specific field of the Resource.
      #
      # @param <Symbol> field_name the name of the field that caused the error
      # @param <String> message    the message to add
      def add(field_name, message)
        (errors[field_name] ||= []) << message
      end

      # Collect all errors into a single list.
      def full_messages
        errors.inject([]) do |list,pair|
          list += pair.last
        end
      end

      # Return authentication errors for a particular field_name.
      #
      # @param <Symbol> field_name the name of the field you want an error for
      def on(field_name)
        errors_for_field = errors[field_name]
        errors_for_field.blank? ? nil : errors_for_field
      end

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

      def empty?
        entries.empty?
      end

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

      private
      def errors
        @errors ||= {}
      end

    end # class Errors
  end # Authentication
end #  Rails

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
myobie-rails-auth-0.0.0 lib/rails-auth/errors.rb
myobie-rails-auth-0.0.2 lib/rails-auth/errors.rb
myobie-rails-auth-0.0.3 lib/rails-auth/errors.rb
myobie-rails-auth-0.0.4 lib/rails-auth/errors.rb