Sha256: 85e91fa27b255d886083a95f42144de6f2b27cc55b1084f3c602ab2addff0fa8

Contents?: true

Size: 1.61 KB

Versions: 7

Compression:

Stored size: 1.61 KB

Contents

# encoding: utf-8
# frozen_string_literal: true
module Warden
  class Proxy
    # 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]
        blank?(errors_for_field) ? nil : errors_for_field
      end

      def each
        errors.map.each do |_k,v|
          next if blank?(v)
          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

      def blank?(thing)
        thing.nil? || thing == "" || (thing.respond_to?(:empty?) && thing.empty?)
      end

    end # class Errors
  end # Proxy
end # Warden

Version data entries

7 entries across 6 versions & 4 rubygems

Version Path
trusty-cms-7.0.9.1 vendor/bundle/ruby/3.3.0/gems/warden-1.2.9/lib/warden/errors.rb
trusty-cms-7.0.9.1 vendor/bundle/ruby/3.1.0/gems/warden-1.2.9/lib/warden/errors.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/warden-1.2.9/lib/warden/errors.rb
date_n_time_picker_activeadmin-0.1.2 vendor/bundle/ruby/2.6.0/gems/warden-1.2.9/lib/warden/errors.rb
date_n_time_picker_activeadmin-0.1.1 vendor/bundle/ruby/2.6.0/gems/warden-1.2.9/lib/warden/errors.rb
warden-1.2.9 lib/warden/errors.rb
warden-1.2.8 lib/warden/errors.rb