<commit_before>module Mongomatic
  class Base
    include Mongomatic::Modifiers
    include Mongomatic::Validatable
    
    class << self
      def settings
        @settings || Mongomatic.settings
      end

      def settings=(hash)
        @settings = hash
        reconnect!; hash
      end
      
      def reconnect!
        @db = @collection = nil; true
      end
      
      def db
        @db ||= Mongo::Connection.new(*self.settings[:connection]).db(self.settings[:db])
      end

      def collection_name
        self.to_s
      end

      def collection
        @collection ||= self.db.collection(self.collection_name)
      end

      def find(query={})
        Mongomatic::Cursor.new(self, collection.find(query))
      end
      
      def all
        find
      end
      
      def each
        find.each { |found| yield(found) }
      end
      
      def first
        find.limit(1).next_document
      end
      
      def count
        find.count
      end
    end

    attr_accessor :removed

    def initialize(doc={})
      @doc = doc.stringify_keys
      self.removed = false
    end
    
    def []=(k,v)
      @doc[k.to_s] = v
    end
    
    def [](k)
      @doc[k.to_s]
    end
    
    def removed?
      self.removed == true
    end
    
    def ==(obj)
      obj.is_a?(self.class) && obj.doc["_id"] == @doc["_id"]
    end
    
    def new?
      @doc["_id"] == nil
    end

    def reload
      if obj = self.class.find({"_id" => @doc["_id"]}).next_document
        @doc = obj.doc; true
      end
    end

    def insert(opts={})
      return false unless new? && valid?
      if ret = self.class.collection.insert(@doc,opts)
        @doc["_id"] = @doc.delete(:_id); ret
      end
    end
    
    def update(opts={},update_doc=@doc)
      return false if new? || removed? || !valid?
      self.class.collection.update({"_id" => @doc["_id"]}, update_doc, opts)
    end
    
    def remove(opts={})
      return false if new?
      if ret = self.class.collection.remove({"_id" => @doc["_id"]})
        self.removed = true; freeze; ret
      end
    end
    
    def to_hash
      @doc || {}
    end
    
    protected
    
    def doc
      @doc
    end
    
    def doc=(v)
      @doc = v
    end
  end
end<commit_msg>allow opts to be sent to find<commit_after>module Mongomatic
  class Base
    include Mongomatic::Modifiers
    include Mongomatic::Validatable
    
    class << self
      def settings
        @settings || Mongomatic.settings
      end

      def settings=(hash)
        @settings = hash
        reconnect!; hash
      end
      
      def reconnect!
        @db = @collection = nil; true
      end
      
      def db
        @db ||= Mongo::Connection.new(*self.settings[:connection]).db(self.settings[:db])
      end

      def collection_name
        self.to_s
      end

      def collection
        @collection ||= self.db.collection(self.collection_name)
      end

      def find(query={}, opts={})
        Mongomatic::Cursor.new(self, collection.find(query, opts))
      end
      
      def all
        find
      end
      
      def each
        find.each { |found| yield(found) }
      end
      
      def first
        find.limit(1).next_document
      end
      
      def count
        find.count
      end
    end

    attr_accessor :removed

    def initialize(doc={})
      @doc = doc.stringify_keys
      self.removed = false
    end
    
    def []=(k,v)
      @doc[k.to_s] = v
    end
    
    def [](k)
      @doc[k.to_s]
    end
    
    def removed?
      self.removed == true
    end
    
    def ==(obj)
      obj.is_a?(self.class) && obj.doc["_id"] == @doc["_id"]
    end
    
    def new?
      @doc["_id"] == nil
    end

    def reload
      if obj = self.class.find({"_id" => @doc["_id"]}).next_document
        @doc = obj.doc; true
      end
    end

    def insert(opts={})
      return false unless new? && valid?
      if ret = self.class.collection.insert(@doc,opts)
        @doc["_id"] = @doc.delete(:_id); ret
      end
    end
    
    def update(opts={},update_doc=@doc)
      return false if new? || removed? || !valid?
      self.class.collection.update({"_id" => @doc["_id"]}, update_doc, opts)
    end
    
    def remove(opts={})
      return false if new?
      if ret = self.class.collection.remove({"_id" => @doc["_id"]})
        self.removed = true; freeze; ret
      end
    end
    
    def to_hash
      @doc || {}
    end
    
    protected
    
    def doc
      @doc
    end
    
    def doc=(v)
      @doc = v
    end
  end
end
{'fraction_non_alphanumeric': 0.12007050011015642, 'fraction_numerical': 0.0004406256884776382, 'mean_word_length': 1.6441467676179382, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, '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': '4516322', 'n_tokens_mistral': 1484, 'n_tokens_neox': 1459, 'n_words': 351}