<commit_before>module MotionData
  class Base < NSManagedObject
    include MotionData::FinderMethods
    include MotionData::Persistence
    include MotionData::Validations

    def inspect
      properties = []
      entity.properties.select { |p| p.is_a?(NSAttributeDescription) }.each do |property|
        properties << "#{property.name}: #{valueForKey(property.name).inspect}"
      end

      "#<#{entity.name} #{properties.join(", ")}>"
    end

    def context
      managedObjectContext
    end

    def in_context(_context)
      if _context.is_a? Symbol
        _context = case _context
                     when :private
                       App.delegate.background_moc
                     else
                       App.delegate.moc
                   end
      end

      if _context != managedObjectContext
        if new_record?
          raise "You cannot change context on new record."
        else
          _context.objectWithID objectID
        end
      else
        self
      end
    end

    def _id
      objectID.URIRepresentation.absoluteString
    end

    class << self
      def serialize(*args)
        options = args.last.is_a?(::Hash) ? args.pop : {}

        if args.length > 1
          args.each { |name| serialize name, options }
        else
          name = args.first

          define_method name do
            willAccessValueForKey name<commit_msg>avoid race condition when switching object context<commit_after>module MotionData
  class Base < NSManagedObject
    include MotionData::FinderMethods
    include MotionData::Persistence
    include MotionData::Validations

    def inspect
      properties = []
      entity.properties.select { |p| p.is_a?(NSAttributeDescription) }.each do |property|
        properties << "#{property.name}: #{valueForKey(property.name).inspect}"
      end

      "#<#{entity.name} #{properties.join(", ")}>"
    end

    def context
      managedObjectContext
    end

    def in_context(_context)
      if _context.is_a? Symbol
        _context = case _context
                     when :private
                       App.delegate.background_moc
                     else
                       App.delegate.moc
                   end
      end

      if _context != managedObjectContext
        if new_record?
          raise "You cannot change context on new record."
        else
          obj_in_context = nil
          _context.performBlockAndWait -> { obj_in_context = _context.objectWithID objectID }
          obj_in_context
        end
      else
        self
      end
    end

    def _id
      objectID.URIRepresentation.absoluteString
    end

    class << self
      def serialize(*args)
        options = args.last.is_a?(::Hash) ? args.pop : {}

        if args.length > 1
          args.each { |name| serialize name, options }
        else
          name = args.first

          define_method name do
            willAccessValueForKey name
{'fraction_non_alphanumeric': 0.07517006802721088, 'fraction_numerical': 0.0006802721088435374, 'mean_word_length': 1.7982873453853472, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 8, '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': '6475910', 'n_tokens_mistral': 788, 'n_tokens_neox': 766, 'n_words': 200}