<commit_before>module UiComponents
  module DocuCop
    extend ActiveSupport::Concern

    def initialize(*args)
      super
      options.except(:controller).each do |k, v|
        public_send(:"#{k}=", v)
      end
      validate_mandatory_attributes
    end

    def attributes
      self.class.attributes.keys.map do |k|
        [k, public_send(k)]
      end.to_h
    end

    private

    def validate_mandatory_attributes
      missing_attributes = self.class.attributes
        .select { |a| a[:mandatory] && public_send(a[:name]).nil? }
      return unless missing_attributes.present?
      fail MandatoryAttributeNotSet,
        'Following mandatory attribute(s) have not been provided: ' +
          missing_attributes.map { |m| m[:name] }.join(', ')
    end

    class MandatoryAttributeNotSet < StandardError; end

    class_methods do
      def attribute(name, options = {})
        options = ActionController::Parameters.new(options)
        attributes << {
          name: name,
          mandatory: options[:mandatory] == true,
          description: options.require(:description)
        }
        define_accessors(name)
      end

      def define_accessors(name)
        define_method(name) do
          instance_variable_get(:"@#{name}")
        end

        define_method(:"#{name}=") do |value|
          instance_variable_set(:"@#{name}", value)
        end
      end

      def attributes
        @attributes ||= []
      end<commit_msg>Use attr_accessor<commit_after>module UiComponents
  module DocuCop
    extend ActiveSupport::Concern

    def initialize(*args)
      super
      options.except(:controller).each do |k, v|
        public_send(:"#{k}=", v)
      end
      validate_mandatory_attributes
    end

    def attributes
      self.class.attributes.keys.map do |k|
        [k, public_send(k)]
      end.to_h
    end

    private

    def validate_mandatory_attributes
      missing_attributes = self.class.attributes
        .select { |a| a[:mandatory] && public_send(a[:name]).nil? }
      return unless missing_attributes.present?
      fail MandatoryAttributeNotSet,
        'Following mandatory attribute(s) have not been provided: ' +
          missing_attributes.map { |m| m[:name] }.join(', ')
    end

    class MandatoryAttributeNotSet < StandardError; end

    class_methods do
      def attribute(name, options = {})
        options = ActionController::Parameters.new(options)
        attributes << {
          name: name,
          mandatory: options[:mandatory] == true,
          description: options.require(:description)
        }
        attr_accessor name
      end

      def attributes
        @attributes ||= []
      end
{'fraction_non_alphanumeric': 0.1022769690182904, 'fraction_numerical': 0.0, 'mean_word_length': 2.4271099744245523, 'pattern_counts': {'":': 0, '<': 9, '<?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': '7032500', 'n_tokens_mistral': 753, 'n_tokens_neox': 744, 'n_words': 170}