<commit_before>require 'mongoid'
require "mongoid-embedded-errors/version"
require "mongoid-embedded-errors/embedded_in"

module Mongoid
  module EmbeddedErrors

    def self.included(klass)
      # make sure that the alias only happens once:
      unless klass.instance_methods.include?(:errors_without_embedded_errors)
        klass.alias_method_chain(:errors, :embedded_errors)
      end
    end

    def errors_with_embedded_errors
      errs = errors_without_embedded_errors
      self.embedded_relations.each do |name, metadata|
        if errs[name]
          errs.delete(name.to_sym)
          self.send(name).each_with_index do |rel, i|
            errs[name] = {i => rel.errors.messages} if rel.errors.any?
          end
        end
      end
      return errs
    end

  end
end<commit_msg>improved documentation<commit_after>require 'mongoid'
require "mongoid-embedded-errors/version"
require "mongoid-embedded-errors/embedded_in"

module Mongoid
  module EmbeddedErrors

    def self.included(klass)
      # make sure that the alias only happens once:
      unless klass.instance_methods.include?(:errors_without_embedded_errors)
        klass.alias_method_chain(:errors, :embedded_errors)
      end
    end

    def errors_with_embedded_errors
      errs = errors_without_embedded_errors
      self.embedded_relations.each do |name, metadata|
        # name is something like pages or sections
        # if there is an 'is invalid' message for the relation then let's work it:
        if errs[name]
          # first delete the unless 'is invalid' error for the relation
          errs.delete(name.to_sym)
          # next, loop through each of the relations (pages, sections, etc...)
          self.send(name).each_with_index do |rel, i|
            # get each of their individual message and add them to the parent's errors:
            if rel.errors.any?
              errs[name] = rel.errors.messages
            end
          end
        end
      end
      return errs
    end

  end
end
{'fraction_non_alphanumeric': 0.07125062282012955, 'fraction_numerical': 0.0, 'mean_word_length': 2.618018018018018, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, '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': '219922', 'n_tokens_mistral': 591, 'n_tokens_neox': 567, 'n_words': 162}