<commit_before>module Dragonfly
  module Delegator
    
    # This gets raised if no delegated objects are able to handle
    # the method call, even though they respond to that method.
    class UnableToHandle < StandardError; end
    
    def register(klass, *args, &block)
      object = klass.new(*args)
      object.configure(&block) if block
      registered_objects << object
    end
    
    def unregister(klass)
      registered_objects.delete_if{|obj| obj.is_a?(klass) }
    end
    
    def unregister_all
      self.registered_objects = []
    end
    
    def registered_objects
      @registered_objects ||= []
    end
    
    def callable_methods
      registered_objects.map{|a| a.delegatable_methods }.flatten.uniq
    end
    
    def has_callable_method?(method)
      callable_methods.include?(method.to_method_name)
    end
    
    private
    
    attr_writer :registered_objects
    
    def method_missing(meth, *args)
      registered_objects.reverse.each do |object|
        catch :unable_to_handle do
          return object.send(meth, *args) if object.respond_to?(meth)
        end
      end
      raise UnableToHandle, "None of the registered objects for #{self} were able to deal with the method call " +
        "#{meth}(#{args.map{|a| a.inspect[0..100]}.join(',')}), even though the method is implemented" if self.has_callable_method?(meth)
      super
    end
    
  end
end<commit_msg>Slightly better error message<commit_after>module Dragonfly
  module Delegator
    
    # This gets raised if no delegated objects are able to handle
    # the method call, even though they respond to that method.
    class UnableToHandle < StandardError; end
    
    def register(klass, *args, &block)
      object = klass.new(*args)
      object.configure(&block) if block
      registered_objects << object
    end
    
    def unregister(klass)
      registered_objects.delete_if{|obj| obj.is_a?(klass) }
    end
    
    def unregister_all
      self.registered_objects = []
    end
    
    def registered_objects
      @registered_objects ||= []
    end
    
    def callable_methods
      registered_objects.map{|a| a.delegatable_methods }.flatten.uniq
    end
    
    def has_callable_method?(method)
      callable_methods.include?(method.to_method_name)
    end
    
    private
    
    attr_writer :registered_objects
    
    def method_missing(meth, *args)
      registered_objects.reverse.each do |object|
        catch :unable_to_handle do
          return object.send(meth, *args) if object.respond_to?(meth)
        end
      end
      raise UnableToHandle, "None of the objects registered with #{self} were able to deal with the method call " +
        "#{meth}(#{args.map{|a| a.inspect[0..100]}.join(',')}). You need to register one that can." if self.has_callable_method?(meth)
      super
    end
    
  end
end
{'fraction_non_alphanumeric': 0.08432470258922323, 'fraction_numerical': 0.0027991602519244225, 'mean_word_length': 2.6513409961685825, 'pattern_counts': {'":': 0, '<': 9, '<?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': '6688184', 'n_tokens_mistral': 871, 'n_tokens_neox': 863, 'n_words': 231}