Sha256: 7d18ab97d1a71cbc5e8a5821d7d68c4b5b1ffe25b33e695e8b0ce19c9c3a85d4

Contents?: true

Size: 1.79 KB

Versions: 2

Compression:

Stored size: 1.79 KB

Contents

module Validatable
  class Errors
    extend Forwardable
    include Enumerable

    def_delegators :errors, :clear, :each, :each_pair, :empty?, :length, :size

    # call-seq: on(attribute)
    # 
    # * Returns nil, if no errors are associated with the specified +attribute+.
    # * Returns the error message, if one error is associated with the specified +attribute+.
    # * Returns an array of error messages, if more than one error is associated with the specified +attribute+.
    def on(attribute)
      return nil if errors[attribute.to_sym].nil?
      errors[attribute.to_sym].size == 1 ? errors[attribute.to_sym].first : errors[attribute.to_sym]
    end
    
    def add(attribute, message) #:nodoc:
      errors[attribute.to_sym] = [] if errors[attribute.to_sym].nil?
      errors[attribute.to_sym] << message
    end

    def merge!(errors) #:nodoc:
      errors.each_pair{|k, v| add(k,v)}
      self
    end

    def replace(attribute, value)
      errors[attribute.to_sym] = value
    end

    def raw(attribute)
      errors[attribute.to_sym]
    end
    
    def errors #:nodoc:
      @errors ||= {}
    end

    def count #:nodoc:
      size
    end

    # call-seq: full_messages -> an_array_of_messages
    # 
    # Returns an array containing the full list of error messages.
    def full_messages
      full_messages = []

      errors.each_key do |attribute|
        errors[attribute].each do |msg|
          next if msg.nil?

          if attribute.to_s == "base"
            full_messages << msg
          else
            full_messages << humanize(attribute.to_s) + " " + msg
          end
        end
      end
      full_messages
    end
    
    def humanize(lower_case_and_underscored_word) #:nodoc:
      lower_case_and_underscored_word.to_s.gsub(/_id$/, "").gsub(/_/, " ").capitalize
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
validatable-1.3.4 lib/errors.rb
validatable-1.4.0 lib/errors.rb