<commit_before>module ActiveMerchant #:nodoc:
  module Validateable #:nodoc:
    def valid?
      errors.clear

      before_validate if respond_to?(:before_validate, true)
      validate if respond_to?(:validate, true)

      errors.empty?
    end

    def initialize(attributes = {})
      self.attributes = attributes
    end

    def errors
      @errors ||= Errors.new(self)
    end

    private

    def attributes=(attributes)
      unless attributes.nil?
        for key, value in attributes
          send("#{key}=", value )
        end
      end
    end

    # This hash keeps the errors of the object
    class Errors < HashWithIndifferentAccess

      def initialize(base)
        super() { |h, k|  h[k] = [] ; h[k] }
        @base = base
      end

      def count
        size
      end

      def empty?
        all? { |k, v| v && v.empty? }
      end

      # returns a specific fields error message.
      # if more than one error is available we will only return the first. If no error is available
      # we return an empty string
      def on(field)
        self[field].to_a.first<commit_msg>returning errors count as 0 when errors not present<commit_after>module ActiveMerchant #:nodoc:
  module Validateable #:nodoc:
    def valid?
      errors.clear

      before_validate if respond_to?(:before_validate, true)
      validate if respond_to?(:validate, true)

      errors.empty?
    end

    def initialize(attributes = {})
      self.attributes = attributes
    end

    def errors
      @errors ||= Errors.new(self)
    end

    private

    def attributes=(attributes)
      unless attributes.nil?
        for key, value in attributes
          send("#{key}=", value )
        end
      end
    end

    # This hash keeps the errors of the object
    class Errors < HashWithIndifferentAccess

      def initialize(base)
        super() { |h, k|  h[k] = [] ; h[k] }
        @base = base
      end

      def count
        empty? ? 0 : size
      end

      def empty?
        all? { |k, v| v && v.empty? }
      end

      # returns a specific fields error message.
      # if more than one error is available we will only return the first. If no error is available
      # we return an empty string
      def on(field)
        self[field].to_a.first if self[:field].present?
{'fraction_non_alphanumeric': 0.08601216333622937, 'fraction_numerical': 0.0008688097306689834, 'mean_word_length': 2.0543766578249336, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6333744', 'n_tokens_mistral': 670, 'n_tokens_neox': 651, 'n_words': 229}