Module: BandwidthIris::ClientWrapper

Overview

Allows to make first argument (client instance) of defined singletom function optional

Instance Method Summary collapse

Instance Method Details

#wrap_client_arg(method) ⇒ Object

Make first argument (client instance) of a method optional

Examples:

class MyClass
  extend ClientWrapper

  def self.do_something(client, arg1, arg2)
  end

  wrap_client_arg :do_something
  # Now you can make calls like MyClass.do_something(client, arg1, arg2) and MyClass.do_something(arg1, arg2)
  # In last case Client instance with default parameters (from Client.global_options) will be used
end

Parameters:

  • method (Symbol)

    singleton method name



18
19
20
21
22
23
24
25
26
# File 'lib/bandwidth-iris/client_wrapper.rb', line 18

def wrap_client_arg(method)
  old = method(method)
  define_singleton_method(method) do |*args|
    if(args.size == 0 || !(args[0] || {}).is_a?(Client))
      args.unshift(Client.new())
    end
    old.call(*args)
  end
end