<commit_before>module Raven
  module Mixin
    # Maps passed *attributes* to `@ivar_variables` and `self.property_setters=`.
    #
    # ```
    # class Foo
    #   include Raven::Mixin::InitializeWith
    #
    #   @logger : String?
    #   property message : String?
    #
    #   def backtrace=(backtrace)
    #     # ...
    #   end
    # end
    #
    # foo = Foo.new
    # foo.initialize_with({
    #   logger:    "my-logger",
    #   message:   "boo!",
    #   backtrace: caller,
    # })
    # ```
    #
    # NOTE: Magic inside!
    module InitializeWith
      def initialize_with(**attributes)
        {% begin %}
          %set = [] of Symbol

          {%
            properties = @type.methods
              .select { |m| m.name.ends_with?('=') && m.args.size == 1 }
              .map(&.name[0...-1].id)
              .uniq
          %}

          {% for property in properties %}
            if arg = attributes[{{ property.symbolize }}]?
              unless %set.includes?({{ property.symbolize }})
                self.{{ property }} = arg
                %set << {{ property.symbolize }}
              end
            end
          {% end %}

          {%
            ivars = @type.instance_vars
              .map { |v| {v.name.id, v.type.name} }
              .uniq
          %}

          {% for ivar in ivars %}
            {% name = ivar[0]; type = ivar[1] %}
            if arg = attributes[{{ name.symbolize }}]?
              unless %set.includes?({{ name.symbolize }})
                if arg.is_a?({{ type }})
                  @{{ name }} = arg
                  %set << {{ name.symbolize }}
                end
              end
            end
          {% end %}
        {% end %}

        self
      end
    end
  end
end<commit_msg>Ger rid of runtime bloat from InitializeWith module<commit_after>module Raven
  module Mixin
    # Maps passed *attributes* to `@ivar_variables` and `self.property_setters=`.
    #
    # ```
    # class Foo
    #   include Raven::Mixin::InitializeWith
    #
    #   @logger : String?
    #   property message : String?
    #
    #   def backtrace=(backtrace)
    #     # ...
    #   end
    # end
    #
    # foo = Foo.new
    # foo.initialize_with({
    #   logger:    "my-logger",
    #   message:   "boo!",
    #   backtrace: caller,
    # })
    # ```
    #
    # NOTE: Magic inside!
    module InitializeWith
      def initialize_with(attributes)
        {% begin %}
          {%
            properties = @type.methods
              .select { |m| m.name.ends_with?('=') && m.args.size == 1 }
              .map(&.name[0...-1].symbolize)
              .uniq
          %}

          {% for name in properties %}
            if arg = attributes[{{ name }}]?
              self.{{ name.id }} = arg
            end
          {% end %}

          {%
            ivars = @type.instance_vars
              .map(&.name.symbolize)
              .uniq
          %}

          {% for name in ivars %}
            {% unless properties.includes?(name) %}
              if arg = attributes[{{ name }}]?
                @{{ name.id }} = arg
              end
            {% end %}
          {% end %}
        {% end %}

        self
      end

      def initialize_with(**attributes)
        initialize_with(attributes)
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.13961235614778922, 'fraction_numerical': 0.0024227740763173833, 'mean_word_length': 1.2561475409836065, 'pattern_counts': {'":': 0, '<': 7, '<?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': '2870125', 'n_tokens_mistral': 1030, 'n_tokens_neox': 970, 'n_words': 209}