<commit_before>require_relative 'document/query'
require_relative 'document/timestamps'
require_relative 'document/associations'

module Mongol
  module Document
    extend ActiveSupport::Concern

    module InstanceMethods
      def initialize(new_attributes = {})
        self.attributes = new_attributes.is_a?(Hash) ? new_attributes.symbolize_keys : {}
        dup_original_attributes
      end

      def attributes
        @attributes ||= {}
      end
      def attributes=(new_attributes)
        @attributes = new_attributes.symbolize_keys if new_attributes.is_a? Hash
      end

      def [](attribute_name)
        attributes[attribute_name.to_sym]
      end
      def []=(attribute_name, new_value)
        attributes[attribute_name.to_sym] = new_value
      end

      def id
        attributes[:_id]
      end
      def id=(new_id)
        self.attributes[:_id] = new_id
      end

      def new_document?
        id.nil?
      end
      def saved? # rename this persisted?
        !new_document?
      end

      def dirty?
        new_document? || attributes != @original_attributes
      end
      def not_dirty?
        !dirty?
      end

      def savable?
        dirty? && not_being_saved?
      end

      def being_saved?
        @being_saved
      end
      def not_being_saved?
        !being_saved?
      end

    private
      def dup_original_attributes
        @original_attributes = attributes.dup
      end
    public

      def save
        @being_saved = true # this would work betterw with an Identity Map

        save_associations || ( return false ) # this will cause an infinite loop

        if (new_document?)
          # puts attributes
          new_id = self.class.collection.insert(attributes)
          if new_id
            self.id = new_id<commit_msg>Spelling error.<commit_after>require_relative 'document/query'
require_relative 'document/timestamps'
require_relative 'document/associations'

module Mongol
  module Document
    extend ActiveSupport::Concern

    module InstanceMethods
      def initialize(new_attributes = {})
        self.attributes = new_attributes.is_a?(Hash) ? new_attributes.symbolize_keys : {}
        dup_original_attributes
      end

      def attributes
        @attributes ||= {}
      end
      def attributes=(new_attributes)
        @attributes = new_attributes.symbolize_keys if new_attributes.is_a? Hash
      end

      def [](attribute_name)
        attributes[attribute_name.to_sym]
      end
      def []=(attribute_name, new_value)
        attributes[attribute_name.to_sym] = new_value
      end

      def id
        attributes[:_id]
      end
      def id=(new_id)
        self.attributes[:_id] = new_id
      end

      def new_document?
        id.nil?
      end
      def saved? # rename this persisted?
        !new_document?
      end

      def dirty?
        new_document? || attributes != @original_attributes
      end
      def not_dirty?
        !dirty?
      end

      def savable?
        dirty? && not_being_saved?
      end

      def being_saved?
        @being_saved
      end
      def not_being_saved?
        !being_saved?
      end

    private
      def dup_original_attributes
        @original_attributes = attributes.dup
      end
    public

      def save
        @being_saved = true # this would work better with an Identity Map

        save_associations || ( return false ) # this will cause an infinite loop

        if (new_document?)
          # puts attributes
          new_id = self.class.collection.insert(attributes)
          if new_id
            self.id = new_id
{'fraction_non_alphanumeric': 0.06874478151962149, 'fraction_numerical': 0.0, 'mean_word_length': 2.1116883116883116, 'pattern_counts': {'":': 0, '<': 3, '<?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': '4474710', 'n_tokens_mistral': 1008, 'n_tokens_neox': 983, 'n_words': 248}