<commit_before>class ModelFactory::Factory
  
  def initialize(klass)
    @klass = klass
    @map = Hash.new
  end
  
  def clear!
    @map.clear
  end
  
  def define(name, attributes = {}, &block)
    @map[name.to_s] = ModelFactory::ProtoModel.new(@klass, attributes, &block)
  end
    
  def create(name, attributes = {}, &block)
    with_proto_model(name) {|proto| proto.create(attributes, &block)}
  end
  
  def use(name)
    with_proto_model(name) {|proto| proto.use}
  end
  
  alias_method :[], :use
  
  protected
  
  def with_proto_model(name, &block)
    proto_model = lookup(name.to_s)
    raise "Can't find model factory #{name.to_s} for #{@klass}!" if proto_model.nil?
    yield proto_model
  end
  
  def lookup(name)
    if @map[name]
      @map[name]
    else
      # Descendant classes should be loaded for this to work
      @klass.descendants.each do |klass|
        proto_model = klass.factory.lookup(name)
        return proto_model if proto_model
      end
      
      nil
    end
  end

end<commit_msg>added a note for future<commit_after>class ModelFactory::Factory
  
  def initialize(klass)
    @klass = klass
    @map = Hash.new
  end
  
  def clear!
    @map.clear
  end
  
  def define(name, attributes = {}, &block)
    @map[name.to_s] = ModelFactory::ProtoModel.new(@klass, attributes, &block)
  end
    
  def create(name, attributes = {}, &block)
    with_proto_model(name) {|proto| proto.create(attributes, &block)}
  end
  
  def use(name)
    with_proto_model(name) {|proto| proto.use}
  end
  
  alias_method :[], :use
  
  protected
  
  def with_proto_model(name, &block)
    proto_model = lookup(name.to_s)
    raise "Can't find model factory #{name.to_s} for #{@klass}!" if proto_model.nil?
    yield proto_model
  end
  
  # TODO consider: aren't we going in reverse direction here?
  # class SpecialBook < Book
  # class OtherSpecialBook < Book
  # 
  # SpecialBook.factory.define :x
  # OtherSpecialBook.factory.define :y
  # 
  # Book.factory.create :x => an instance of SpecialBook :x
  # Book.factory.create :y => an instance of OtherSpecialBook :y
  # 
  # works pretty much like AR STI: Book.find(id) => an instance of SpecialBook
  # 
  # (but requires traversing hierarchy in unusual direction, i.e. against 'super')
  
  def lookup(name)
    if @map[name]
      @map[name]
    else
      # Descendant classes should be loaded for this to work
      @klass.descendants.each do |klass|
        proto_model = klass.factory.lookup(name)
        return proto_model if proto_model
      end
      
      nil
    end
  end

end
{'fraction_non_alphanumeric': 0.10520186335403726, 'fraction_numerical': 0.0, 'mean_word_length': 2.863568215892054, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 6, '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': '4305052', 'n_tokens_mistral': 862, 'n_tokens_neox': 872, 'n_words': 231}