<commit_before>require "active_support/core_ext/hash/keys"
require "active_support/inflector"

module FactoryGirl
  class Factory
    attr_reader :name #:nodoc:

    def initialize(name, options = {}) #:nodoc:
      assert_valid_options(options)
      @name             = name.to_s.underscore.to_sym
      @parent           = options[:parent]
      @aliases          = options[:aliases] || []
      @traits           = options[:traits]  || []
      @class_name       = options[:class]
      @default_strategy = options[:default_strategy]
      @defined_traits   = []
      @children         = []
      @attribute_list   = AttributeList.new
      @compiled         = false
    end

    def factory_name
      puts "WARNING: factory.factory_name is deprecated. Use factory.name instead."
      name
    end

    def build_class #:nodoc:
      @build_class ||= class_name.to_s.camelize.constantize
    end

    def default_strategy #:nodoc:
      @default_strategy || (parent && parent.default_strategy) || :create
    end

    def allow_overrides
      @compiled = false
      @attribute_list.overridable
      self
    end

    def allow_overrides?
      @attribute_list.overridable?
    end

    def define_trait(trait)
      @defined_traits << trait
    end

    def add_callback(name, &block)
      @attribute_list.add_callback(Callback.new(name, block))
    end

    def run(proxy_class, overrides) #:nodoc:
      ensure_compiled
      proxy = proxy_class.new(build_class)
      callbacks.each { |callback| proxy.add_callback(callback) }
      overrides = overrides.symbolize_keys

      attributes.each do |attribute|
        factory_overrides = overrides.select { |attr, val| attribute.aliases_for?(attr) }
        if factory_overrides.empty?
          attribute.add_to(proxy)
        else
          factory_overrides.each { |attr, val| proxy.set(attr, val, attribute.ignored); overrides.delete(attr) }
        end
      end
      overrides.each { |attr, val| proxy.set(attr, val) }
      proxy.result(@to_create_block)
    end

    def human_names
      names.map {|name| name.to_s.humanize.downcase }
    end

    def associations
      attributes.select {|attribute| attribute.association? }
    end

    def trait_by_name(name)
      if existing_attribute = trait_for(name)
        existing_attribute
      elsif parent
        parent.trait_by_name(name)
      else
        FactoryGirl.trait_by_name(name)
      end
    end

    # Names for this factory, including aliases.
    #
    # Example:
    #
    #   factory :user, :aliases => [:author] do
    #     # ...
    #   end
    #
    #   FactoryGirl.create(:author).class
    #   # => User
    #
    # Because an attribute defined without a value or block will build an
    # association with the same name, this allows associations to be defined
    # without factories, such as:
    #
    #   factory :user, :aliases => [:author] do
    #     # ...
    #   end
    #
    #   factory :post do
    #     author
    #   end
    #
    #   FactoryGirl.create(:post).author.class
    #   # => User
    def names
      [name] + @aliases
    end

    def to_create(&block)
      @to_create_block = block
    end

    def ensure_compiled
      compile unless @compiled
    end

    def declare_attribute(declaration)
      @attribute_list.declare_attribute(declaration)
    end

    protected

    def class_name #:nodoc:
      @class_name || (parent && parent.class_name) || name
    end

    def add_child(factory)
      @children << factory unless @children.include?(factory)
    end

    def attributes
      ensure_compiled
      AttributeList.new.tap do |list|
        @traits.reverse.map { |name| trait_by_name(name) }.each do |trait|
          list.apply_attributes(trait.attributes)
        end

        list.apply_attributes(@attribute_list)
        list.apply_attributes(parent.attributes) if parent
      end
    end

    private

    def callbacks
      attributes.callbacks
    end

    def compile
      inherit_factory(parent) if parent

      declarations.each do |declaration|
        declaration.to_attributes.each do |attribute|
          define_attribute(attribute)
        end
      end

      @compiled = true
    end

    def inherit_factory(parent) #:nodoc:
      parent.ensure_compiled
      allow_overrides if parent.allow_overrides?
      parent.add_child(self)
    end

    def declarations
      @attribute_list.declarations
    end

    def define_attribute(attribute)
      if attribute.respond_to?(:factory) && attribute.factory == self.name<commit_msg>Goodbye children<commit_after>require "active_support/core_ext/hash/keys"
require "active_support/inflector"

module FactoryGirl
  class Factory
    attr_reader :name #:nodoc:

    def initialize(name, options = {}) #:nodoc:
      assert_valid_options(options)
      @name             = name.to_s.underscore.to_sym
      @parent           = options[:parent]
      @aliases          = options[:aliases] || []
      @traits           = options[:traits]  || []
      @class_name       = options[:class]
      @default_strategy = options[:default_strategy]
      @defined_traits   = []
      @attribute_list   = AttributeList.new
      @compiled         = false
    end

    def factory_name
      puts "WARNING: factory.factory_name is deprecated. Use factory.name instead."
      name
    end

    def build_class #:nodoc:
      @build_class ||= class_name.to_s.camelize.constantize
    end

    def default_strategy #:nodoc:
      @default_strategy || (parent && parent.default_strategy) || :create
    end

    def allow_overrides
      @compiled = false
      @attribute_list.overridable
      self
    end

    def allow_overrides?
      @attribute_list.overridable?
    end

    def define_trait(trait)
      @defined_traits << trait
    end

    def add_callback(name, &block)
      @attribute_list.add_callback(Callback.new(name, block))
    end

    def run(proxy_class, overrides) #:nodoc:
      ensure_compiled
      proxy = proxy_class.new(build_class)
      callbacks.each { |callback| proxy.add_callback(callback) }
      overrides = overrides.symbolize_keys

      attributes.each do |attribute|
        factory_overrides = overrides.select { |attr, val| attribute.aliases_for?(attr) }
        if factory_overrides.empty?
          attribute.add_to(proxy)
        else
          factory_overrides.each { |attr, val| proxy.set(attr, val, attribute.ignored); overrides.delete(attr) }
        end
      end
      overrides.each { |attr, val| proxy.set(attr, val) }
      proxy.result(@to_create_block)
    end

    def human_names
      names.map {|name| name.to_s.humanize.downcase }
    end

    def associations
      attributes.select {|attribute| attribute.association? }
    end

    def trait_by_name(name)
      if existing_attribute = trait_for(name)
        existing_attribute
      elsif parent
        parent.trait_by_name(name)
      else
        FactoryGirl.trait_by_name(name)
      end
    end

    # Names for this factory, including aliases.
    #
    # Example:
    #
    #   factory :user, :aliases => [:author] do
    #     # ...
    #   end
    #
    #   FactoryGirl.create(:author).class
    #   # => User
    #
    # Because an attribute defined without a value or block will build an
    # association with the same name, this allows associations to be defined
    # without factories, such as:
    #
    #   factory :user, :aliases => [:author] do
    #     # ...
    #   end
    #
    #   factory :post do
    #     author
    #   end
    #
    #   FactoryGirl.create(:post).author.class
    #   # => User
    def names
      [name] + @aliases
    end

    def to_create(&block)
      @to_create_block = block
    end

    def ensure_compiled
      compile unless @compiled
    end

    def declare_attribute(declaration)
      @attribute_list.declare_attribute(declaration)
    end

    protected

    def class_name #:nodoc:
      @class_name || (parent && parent.class_name) || name
    end

    def attributes
      ensure_compiled
      AttributeList.new.tap do |list|
        @traits.reverse.map { |name| trait_by_name(name) }.each do |trait|
          list.apply_attributes(trait.attributes)
        end

        list.apply_attributes(@attribute_list)
        list.apply_attributes(parent.attributes) if parent
      end
    end

    private

    def callbacks
      attributes.callbacks
    end

    def compile
      inherit_factory(parent) if parent

      declarations.each do |declaration|
        declaration.to_attributes.each do |attribute|
          define_attribute(attribute)
        end
      end

      @compiled = true
    end

    def inherit_factory(parent) #:nodoc:
      parent.ensure_compiled
      allow_overrides if parent.allow_overrides?
    end

    def declarations
      @attribute_list.declarations
    end

    def define_attribute(attribute)
      if attribute.respond_to?(:factory) && attribute.factory == self.name
{'fraction_non_alphanumeric': 0.08948370478217045, 'fraction_numerical': 0.0, 'mean_word_length': 2.3333333333333335, 'pattern_counts': {'":': 0, '<': 9, '<?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': '3487162', 'n_tokens_mistral': 2723, 'n_tokens_neox': 2660, 'n_words': 591}