<commit_before>module StrokeDB
  module StrokeObjects
    module SlotHooks
      def self.included(mod)
        mod.extend(ClassMethods)
      end
      
      def initialize(*args)
        super
        @hooks ||= {}
      end
      
      def [](slotname)
        if hook = @get_hooks[slotname]
          return hook.get { super }
        end
        super
      end
      
      def []=(slotname, value)
        if hook = @set_hooks[slotname]
          return hook.set(value) { super }
        end
        super
      end
      
      module ClassMethods
        # def set_hook
        # end
      end
    end
  end
end
<commit_msg>simple slot hooks<commit_after>module StrokeDB
  module StrokeObjects
    module SlotHooks
      def self.included(mod)
        mod.extend(ClassMethods)
      end
      
      def slot_hooks
        @slot_hooks ||= self.class.slot_hooks
      end
      
      def [](slotname)
        return super unless hook = slot_hooks[slotname]
        return hook.get(self, slotname) { super }
      end
      
      def []=(slotname, value)
        return super unless hook = slot_hooks[slotname]
        return hook.set(self, slotname, value) { super }
      end
      
      module ClassMethods
        attr_accessor :slot_hooks
        def slot_hooks
          @slot_hooks ||= {}
        end
        
        # Example:
        #
        # class Person < Hash
        #   include SlotHooks
        #
        #   slot_hook :name do
        #     def get(doc, slotname)
        #       puts "Accessing slot #{slotname}"
        #       super
        #     end
        #     def set(doc, slotname, value)
        #       puts "Writing to slot #{slotname}"
        #       super
        #     end
        #   end
        # end
        #
        def slot_hook(slotname, mod = nil, &blk)
          slot_hooks[slotname] ||= SlotHook.new
          slot_hooks[slotname].extend(mod || Module.new(&blk))
        end
      end
      
      # Default slot hook: noop.
      class SlotHook        
        def get(doc, slotname)
          yield # returns super
        end
        
        def set(doc, slotname, value)
          yield # returns super
        end
      end
    end
  end
end

{'fraction_non_alphanumeric': 0.07660924750679964, 'fraction_numerical': 0.0, 'mean_word_length': 1.3629550321199144, 'pattern_counts': {'":': 0, '<': 4, '<?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': '1202160', 'n_tokens_mistral': 640, 'n_tokens_neox': 651, 'n_words': 165}