<commit_before>module Formalist
  class Form
    class Result
      class Attr
        attr_reader :definition, :input, :errors

        def initialize(definition, input, errors)
          @definition = definition
          @input = input.fetch(definition.name, {})
          @errors = errors.fetch(definition.name, [])[0] || []
        end

        def to_ary
          # Errors, if the attr hash is present and its members have errors:
          # {:meta=>[[{:pages=>[["pages is missing"], nil]}], {}]}

          # Errors, if the attr hash hasn't been provided
          # {:meta=>[["meta is missing"], nil]}

          local_errors = errors[0].is_a?(Hash) ? [] : errors
          child_errors = errors[0].is_a?(Hash) ? errors[0] : {}

          [:attr, [definition.name, definition.children.map { |el| el.(input, child_errors).to_ary }, local_errors]]
        end
      end
    end
  end
end<commit_msg>Eagerly build children for attr results<commit_after>module Formalist
  class Form
    class Result
      class Attr
        attr_reader :definition, :input, :errors
        attr_reader :children

        def initialize(definition, input, errors)
          @definition = definition
          @input = input.fetch(definition.name, {})
          @errors = errors.fetch(definition.name, [])[0] || []
          @children = build_children
        end

        def to_ary
          # Errors, if the attr hash is present and its members have errors:
          # {:meta=>[[{:pages=>[["pages is missing"], nil]}], {}]}

          # Errors, if the attr hash hasn't been provided
          # {:meta=>[["meta is missing"], nil]}

          local_errors = errors[0].is_a?(Hash) ? [] : errors
          [:attr, [definition.name, children.map(&:to_ary), local_errors]]
        end

        private

        def build_children
          child_errors = errors[0].is_a?(Hash) ? errors[0] : {}
          definition.children.map { |el| el.(input, child_errors) }
        end
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.13021618903971846, 'fraction_numerical': 0.004022121669180492, 'mean_word_length': 2.2463295269168024, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, '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': '1344211', 'n_tokens_mistral': 578, 'n_tokens_neox': 566, 'n_words': 157}