class Google::Ads::GoogleAds::Utils::PathLookupDefiner

Public Class Methods

kwargs_from(method) click to toggle source
# File lib/google/ads/google_ads/utils/path_lookup_definer.rb, line 84
def self.kwargs_from(method)
  method.parameters.select { |param|
    param.first == :keyreq
  }.map { |param|
    param[1]
  }
end
load_path_helper(name, version) click to toggle source
# File lib/google/ads/google_ads/utils/path_lookup_definer.rb, line 92
def self.load_path_helper(name, version)
  require "google/ads/google_ads/#{version}/services/#{name}_service/paths"

  const_name = ::Google::Ads::GoogleAds::Utils.camelize(name.to_s)

  mod_host = Object.new
  mod = Kernel.const_get("Google::Ads::GoogleAds::#{version.upcase}::Services::#{const_name}Service::Paths")
  mod_host.extend(mod)
  mod_host
end
new(target, name) click to toggle source
# File lib/google/ads/google_ads/utils/path_lookup_definer.rb, line 25
def initialize(target, name)
  @target = target
  @name = name
end
verify_args(name, params, args, kwargs) click to toggle source
# File lib/google/ads/google_ads/utils/path_lookup_definer.rb, line 65
def self.verify_args(name, params, args, kwargs)
  total_arg_count = params.length
  positional_form = "#{name}(#{params.join(", ")})"
  kwargs_form = "#{name}(#{params.map { |arg| "#{arg}:" }.join(", ")})"

  if kwargs.empty? && !args.empty?
    if args.length != total_arg_count
      raise ArgumentError, "must specify #{positional_form} if using " \
        "positional arguments (DEPRECATED), or #{kwargs_form}"
    end
  elsif !args.empty?
    raise ArgumentError, "#{name} does not simultaneously accept " \
      "positional arguments and keyword arguments, must specify " \
      "#{kwargs_form}"
  elsif !(params.all? { |k| kwargs.include?(k) })
    raise ArgumentError, "must specify #{kwargs_form}"
  end
end

Public Instance Methods

call(version) click to toggle source

Defines a path lookup method on the target object instance that calls the underlying gapic path helper. While this implementation looks pretty scary, all it's really doing is a bunch of argument transforms from one kwarg form to another. There are a bunch of class level helper methods below this call funciton that extract some of the gnarlier behaviour. Usually class methods come before instance methods, but they aren't super important to the implementation here by comparison with this method, so I moved them below.

# File lib/google/ads/google_ads/utils/path_lookup_definer.rb, line 39
def call(version)
  name = @name
  @target.send(:define_singleton_method, name) do |*args, **kwargs|
    mod_host = Utils::PathLookupDefiner.load_path_helper(name, version)
    target_method = mod_host.method(:"#{name}_path")

    params = Utils::PathLookupDefiner.kwargs_from(target_method)

    Utils::PathLookupDefiner.verify_args(name, params, args, kwargs)

    if !args.empty?
      params.each_with_index do |arg, idx|
        kwargs[arg] = args[idx]
      end
    end

    compound_paths = params.map { |arg_group|
      kwargs.fetch_values(*arg_group).join("~")
    }

    new_kwargs = Hash[params.zip(compound_paths)]

    target_method.call(**new_kwargs)
  end
end