<commit_before>require_relative '../abstract_container/structured_and_oo/attribute'
require_relative '../container_data/structured_and_oo/attribute_data'
require_relative '../../util/html_logger'
require_relative '../../core/setting'

module Languages

  module Ruby

    # Ruby Handling Ruby attributes
    class AttributeRuby < Languages::Attribute

      public

        def initialize
          @log = @settings = Kuniri::Setting.create.log
          @attributeList = []
        end

        # Get ruby attribute. 
        # @param pLine Verify if line has a ruby attribute.
        # @return Return AttributeData or nil.
        def get_attribute(pLine)
          result = detect_attribute(pLine)
          return nil unless result

          @log.write_log("Info: Prepare to get attribute.")

          listOfAttributes = []

          # Has comma? Split string by comma
          result = remove_unnecessary_information(result)

          # Separated by comma, equal or the common case
          if result.scan(/,/).count >= 1
            listOfAttributes = handle_multiple_declaration_with_comma(result)
            @log.write_log("Debug: Declared with comma: #{listOfAttributes}")
          elsif result.scan(/=/).count > 1
            listOfAttributes = handle_multiple_declaration_with_equal(result)
            @log.write_log("Debug: separed by comma: #{listOfAttributes}")
          else
            listOfAttributes = handle_line_declaration(result)
            @log.write_log("Debug: One line declaration #{listOfAttributes}")
          end

          return listOfAttributes
        end

    protected

      # Override
      def detect_attribute(pLine)
        regexExp = /^\s*(?:@|attr_(?:accessor|read|write))(.*)$/
        return nil unless pLine =~ regexExp
        return pLine.scan(regexExp)[0].join("")
      end

      # Override
      def remove_unnecessary_information(pString)
        return pString.gsub!(/\(.*\)/,"") if pString =~ /\(.*\)/
        return pString
      end

      # Override
      def prepare_final_string(pString)
        if pString =~ /\s+|:|@|=/
          return pString.gsub!(/\s+|:|@|=/,"")
        end
        return pString
      end

      # Override
      def handle_multiple_declaration_with_comma(pString)
        listOfAttributes = []
        pString = pString.split(",")
        pString.each do |variable|
          return nil if variable.scan(/=/).count > 1

          variable = variable.scan(/.*=/).join("") if variable =~ /.*=/

          return nil if variable =~ /\./

          variable = prepare_final_string(variable)
          attribute = Languages::AttributeData.new(variable)
          listOfAttributes.push(attribute)
        end

        return listOfAttributes
      end

      # Override
      def handle_multiple_declaration_with_equal(pString)
        listOfAttributes = []
        pString = pString.split("=")
        pString.each do |variable|
          return nil if variable =~ /\./

          variable = prepare_final_string(variable)
          attribute = Languages::AttributeData.new(variable)
          listOfAttributes.push(attribute)
        end

        return listOfAttributes
      end
<commit_msg>Refactor handle_multiple_declaration with_comma and with_equal<commit_after>require_relative '../abstract_container/structured_and_oo/attribute'
require_relative '../container_data/structured_and_oo/attribute_data'
require_relative '../../util/html_logger'
require_relative '../../core/setting'

module Languages

  module Ruby

    # Ruby Handling Ruby attributes
    class AttributeRuby < Languages::Attribute

      public

        def initialize
          @log = @settings = Kuniri::Setting.create.log
          @attributeList = []
        end

        # Get ruby attribute.
        # @param pLine Verify if line has a ruby attribute.
        # @return Return AttributeData or nil.
        def get_attribute(pLine)
          result = detect_attribute(pLine)
          return nil unless result

          @log.write_log("Info: Prepare to get attribute.")

          listOfAttributes = []

          # Has comma? Split string by comma
          result = remove_unnecessary_information(result)

          # Separated by comma, equal or the common case
          if result.scan(/,/).count >= 1
            listOfAttributes = handle_multiple_declaration_with_comma(result)
            @log.write_log("Debug: Declared with comma: #{listOfAttributes}")
          elsif result.scan(/=/).count > 1
            listOfAttributes = handle_multiple_declaration_with_equal(result)
            @log.write_log("Debug: separed by comma: #{listOfAttributes}")
          else
            listOfAttributes = handle_line_declaration(result)
            @log.write_log("Debug: One line declaration #{listOfAttributes}")
          end

          return listOfAttributes
        end

    protected

      # Override
      def detect_attribute(pLine)
        regexExp = /^\s*(?:@|attr_(?:accessor|read|write))(.*)$/
        return nil unless pLine =~ regexExp
        return pLine.scan(regexExp)[0].join("")
      end

      # Override
      def remove_unnecessary_information(pString)
        return pString.gsub!(/\(.*\)/,"") if pString =~ /\(.*\)/
        return pString
      end

      # Override
      def prepare_final_string(pString)
        if pString =~ /\s+|:|@|=/
          return pString.gsub!(/\s+|:|@|=/,"")
        end
        return pString
      end

      def handle_multiple_declaration(pString, splitChar)
        listOfAttributes = []
        pString = pString.split(splitChar)
        pString.each do |variable|
          return nil if variable.scan(/=/).count > 1

          variable = yield(variable)

          return nil if variable =~ /\./

          variable = prepare_final_string(variable)
          attribute = Languages::AttributeData.new(variable)
          listOfAttributes.push(attribute)
        end

        return listOfAttributes
      end

      # Override
      def handle_multiple_declaration_with_comma(pString)
        return handle_multiple_declaration(pString, ",") do |variable|
          variable = variable.scan(/.*=/).join("") if variable =~ /.*=/
          variable
        end
      end

      # Override
      def handle_multiple_declaration_with_equal(pString)
        return handle_multiple_declaration(pString, "="){ |variable| variable}
      end

{'fraction_non_alphanumeric': 0.10357815442561205, 'fraction_numerical': 0.0012554927809165098, 'mean_word_length': 2.2665299846232703, 'pattern_counts': {'":': 0, '<': 5, '<?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': '401459', 'n_tokens_mistral': 1790, 'n_tokens_neox': 1703, 'n_words': 392}