<commit_before>require "metamorpher/builders/ast"

module Metamorpher
  module Builders
    module Ruby
      class UppercaseConstantRewriter
        include Metamorpher::Rewriter
        include Metamorpher::Builders::AST

        def pattern
          builder.const(
            builder.literal!(nil),
            builder.VARIABLE_TO_BE { |v| v.name && v.name.to_s[/^[A-Z_]*$/] }
          )
        end

        def replacement
          builder.derivation!(:variable_to_be) do |variable_to_be, builder|
            name = variable_to_be.name.to_s

            if name.end_with?("_")
              builder.greedy_variable! name.chomp("_").downcase.to_sym
            else
              builder.variable! name.downcase.to_sym
            end
          end
        end
      end
    end
  end
end<commit_msg>Reduce duplication in Ruby builder code.<commit_after>require "metamorpher/builders/ast"
require "metamorpher/builders/ruby/uppercase_rewriter"

module Metamorpher
  module Builders
    module Ruby
      class UppercaseConstantRewriter < UppercaseRewriter
        include Metamorpher::Rewriter
        include Metamorpher::Builders::AST

        def pattern
          builder.const(builder.literal!(nil), super)
        end
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.07857142857142857, 'fraction_numerical': 0.0, 'mean_word_length': 2.1924050632911394, 'pattern_counts': {'":': 0, '<': 4, '<?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': '2274215', 'n_tokens_mistral': 385, 'n_tokens_neox': 371, 'n_words': 75}