Class: Module

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_llm/skills.rb

Instance Method Summary collapse

Instance Method Details

#delegate(*methods, to:, prefix: nil, allow_nil: false, private: false, **_options) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/ruby_llm/skills.rb', line 7

def delegate(*methods, to:, prefix: nil, allow_nil: false, private: false, **_options)
  target_method = to.to_sym

  methods.each do |method_name|
    delegated_method = delegated_method_name(method_name, target_method, prefix)

    define_method(delegated_method) do |*args, **kwargs, &block|
      target = public_send(target_method)
      if target.nil?
        return nil if allow_nil

        raise NoMethodError,
          "#{self.class}##{delegated_method} delegated to ##{target_method}, but ##{target_method} is nil"
      end

      if kwargs.empty?
        target.public_send(method_name, *args, &block)
      else
        target.public_send(method_name, *args, **kwargs, &block)
      end
    end

    private delegated_method if binding.local_variable_get(:private)
  end
end