<commit_before>#
# Copyright (C) 2015-2017 <NAME>  <<EMAIL>>
#
# This source code is licensed under the GNU lesser general public license,
# Version 3.  See the file COPYING for more details

require_relative '../abstract_container/structured_and_oo/function_behavior'
require_relative '../../core/setting'
require_relative '../container_data/structured_and_oo/function_data'

module Languages

  module Ruby

    # Handling Ruby method
    class FunctionBehaviorRuby < Languages::FunctionBehavior
      # Get Ruby function.
      def get_function(pLine, _type = 'globalFunction')
        result = detect_function(pLine)
        return nil unless result

        functionRuby = Languages::FunctionData.new(result)

        parameters = handling_parameter(pLine)
        parameters&.each do |parameter|
          functionRuby.add_parameters(parameter)
        end
        return functionRuby
      end

      protected

      # Override
      def detect_function(pLine)
        regexExpression = /^\s*def\b\s*(\w*)\b/
        return nil unless pLine =~ regexExpression
        return pLine.scan(regexExpression)[0].join('')
      end

      # Override
      def handling_default_parameter(pLine)
        return pLine unless pLine =~ /=/

        partialParameters = if pLine =~ /,/
                              pLine.split(',')
                            else
                              [pLine]
                            end

        newList = []
        partialParameters.each do |element|
          if element =~ /=/
            parameter = element.scan(/(\w*)=/).join('')
            value = element.scan(/=(\w*)/).join('')
            defaultParameter = { parameter => value }
            newList.push(defaultParameter)
            next
          end
          newList.push(element)
        end

        return newList
      end

      # Override
      def remove_unnecessary_information(pLine)
        return pLine if pLine !~ /\s+|\(|\)/

        nLine = pLine.gsub(/\s+|\(|\)/, '')
        nLine != '' ? nLine : nil
      end

      # Override
      def handling_parameter(pLine)
        # Handling with parenthesis and without it.
        if pLine =~ /\(.+\)/
          partial = get_parameters(pLine, /\(.+\)/)
        elsif pLine =~ /def\s+\w+[\s]+(.+)/
          partial = get_parameters(pLine, /def\s+\w+[\s]+(.+)/)
        else
          return nil
        end

        return handling_default_parameter(partial) if partial =~ /=/
        return split_string_by_comma(partial)
      end

      private

      # Override
      def get_parameters(pLine, pRegex)
        partialParameters = pLine.scan(pRegex).join('')
        partialParameters = remove_unnecessary_information(partialParameters)
        return partialParameters
      end

      # Override
      def split_string_by_comma(pString)
        return pString.split(',') if pString =~ /,/
        return [pString]
      end

    end # Class
  end # Ruby
end # Language
<commit_msg>changed handling_parameter regex which handles the case of arguments inside parenthesis<commit_after>#
# Copyright (C) 2015-2017 <NAME>  <<EMAIL>>
#
# This source code is licensed under the GNU lesser general public license,
# Version 3.  See the file COPYING for more details

require_relative '../abstract_container/structured_and_oo/function_behavior'
require_relative '../../core/setting'
require_relative '../container_data/structured_and_oo/function_data'

module Languages

  module Ruby

    # Handling Ruby method
    class FunctionBehaviorRuby < Languages::FunctionBehavior
      # Get Ruby function.
      def get_function(pLine, _type = 'globalFunction')
        result = detect_function(pLine)
        return nil unless result

        functionRuby = Languages::FunctionData.new(result)

        parameters = handling_parameter(pLine)
        parameters&.each do |parameter|
          functionRuby.add_parameters(parameter)
        end
        return functionRuby
      end

      protected

      # Override
      def detect_function(pLine)
        regexExpression = /^\s*def\b\s*(\w*)\b/
        return nil unless pLine =~ regexExpression
        return pLine.scan(regexExpression)[0].join('')
      end

      # Override
      def handling_default_parameter(pLine)
        return pLine unless pLine =~ /=/

        partialParameters = if pLine =~ /,/
                              pLine.split(',')
                            else
                              [pLine]
                            end

        newList = []
        partialParameters.each do |element|
          if element =~ /=/
            parameter = element.scan(/(\w*)=/).join('')
            value = element.scan(/=(\w*)/).join('')
            defaultParameter = { parameter => value }
            newList.push(defaultParameter)
            next
          end
          newList.push(element)
        end

        return newList
      end

      # Override
      def remove_unnecessary_information(pLine)
        return pLine if pLine !~ /\s+|\(|\)/

        nLine = pLine.gsub(/\s+|\(|\)/, '')

        nLine != '' ? nLine : nil
      end

      # Override
      def handling_parameter(pLine)
        # Handling with parenthesis and without it.
        if pLine =~ /\(.*\)/
          partial = get_parameters(pLine, /\(.*\)/)
        elsif pLine =~ /def\s+\w+[\s]+(.+)/
          partial = get_parameters(pLine, /def\s+\w+[\s]+(.+)/)
        else
          return nil
        end

        return handling_default_parameter(partial) if partial =~ /=/
        return split_string_by_comma(partial)
      end

      private

      # Override
      def get_parameters(pLine, pRegex)
        partialParameters = pLine.scan(pRegex).join('')
        partialParameters = remove_unnecessary_information(partialParameters)
        return partialParameters
      end

      # Override
      def split_string_by_comma(pString)
        return pString.split(',') if pString =~ /,/
        return [pString]
      end

    end # Class
  end # Ruby
end # Language

{'fraction_non_alphanumeric': 0.10712497914233272, 'fraction_numerical': 0.0033372267645586516, 'mean_word_length': 1.9527093596059113, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, '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': '1685777', 'n_tokens_mistral': 1725, 'n_tokens_neox': 1633, 'n_words': 388}