<commit_before>module Casting

  class MissingAttendant < StandardError
    def message
      "You must set your attendant object using `to'."
    end
  end

  class InvalidAttendant < StandardError; end

  class Delegation

    def self.prepare(delegated_method_name, client, &block)
      new(delegated_method_name: delegated_method_name, client: client, &block)
    end

    attr_accessor :client, :delegated_method_name, :attendant, :arguments, :block
    private :block

    def initialize(**settings, &block)
      @delegated_method_name = settings[:delegated_method_name]
      @client = settings[:client]
      @attendant = settings[:attendant]
      @arguments = settings[:arguments]
      @keyword_arguments = settings[:keyword_arguments]
      @block = block
    end

    def to(object_or_module)
      @attendant = object_or_module
      begin
        bound_method
      rescue TypeError
        @attendant = method_module || raise
      end
      self
    end

    def with(*args, **kwargs, &block)
      @arguments = args
      @keyword_arguments = kwargs
      @block = block
      self
    end

    def call(*args, **kwargs, &block)
      raise MissingAttendant.new unless attendant

      call_args = if args && !args.empty?
        args
      elsif @arguments && !@arguments.empty?
        @arguments
      end
      call_kwargs = if kwargs && !kwargs.empty?
        kwargs
      elsif @keyword_arguments && !@keyword_arguments.empty?
        @keyword_arguments
      end

      if call_block = block || @block
        if call_args
          if call_kwargs
            bound_method.call(*call_args, **call_kwargs, &call_block)
          else
            bound_method.call(*call_args, &call_block)
          end
        else
          if call_kwargs
            bound_method.call(**call_kwargs, &call_block)
          else
            bound_method.call(&call_block)
          end
        end
      else
        if call_args
          if call_kwargs
            bound_method.call(*call_args, **call_kwargs)
          else
            bound_method.call(*call_args)
          end
        else
          if call_kwargs
            bound_method.call(**call_kwargs)
          else
            bound_method.call
          end
        end
      end
    end

    private

    def bound_method
      begin
        delegated_method.bind(client)
      rescue TypeError<commit_msg>Simplify bound_method arguments<commit_after>module Casting

  class MissingAttendant < StandardError
    def message
      "You must set your attendant object using `to'."
    end
  end

  class InvalidAttendant < StandardError; end

  class Delegation

    def self.prepare(delegated_method_name, client, &block)
      new(delegated_method_name: delegated_method_name, client: client, &block)
    end

    attr_accessor :client, :delegated_method_name, :attendant, :arguments, :block
    private :block

    def initialize(**settings, &block)
      @delegated_method_name = settings[:delegated_method_name]
      @client = settings[:client]
      @attendant = settings[:attendant]
      @arguments = settings[:arguments]
      @keyword_arguments = settings[:keyword_arguments]
      @block = block
    end

    def to(object_or_module)
      @attendant = object_or_module
      begin
        bound_method
      rescue TypeError
        @attendant = method_module || raise
      end
      self
    end

    def with(*args, **kwargs, &block)
      @arguments = args
      @keyword_arguments = kwargs
      @block = block
      self
    end

    def call(*args, **kwargs, &block)
      raise MissingAttendant.new unless attendant

      call_args = if args && !args.empty?
        args
      elsif @arguments && !@arguments.empty?
        @arguments
      end
      call_kwargs = if kwargs && !kwargs.empty?
        kwargs
      elsif @keyword_arguments && !@keyword_arguments.empty?
        @keyword_arguments
      end

      call_block = block || @block
      if call_args
        if call_kwargs
          bound_method.call(*call_args, **call_kwargs, &call_block)
        else
          bound_method.call(*call_args, &call_block)
        end
      else
        if call_kwargs
          bound_method.call(**call_kwargs, &call_block)
        else
          bound_method.call(&call_block)
        end
      end
    end

    private

    def bound_method
      begin
        delegated_method.bind(client)
      rescue TypeError
{'fraction_non_alphanumeric': 0.07596371882086168, 'fraction_numerical': 0.0, 'mean_word_length': 2.2315018315018316, 'pattern_counts': {'":': 0, '<': 7, '<?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': '5292543', 'n_tokens_mistral': 1259, 'n_tokens_neox': 1235, 'n_words': 305}