Class: Google::Ads::GoogleAds::Utils::PathLookupDefiner

Inherits:
Object
  • Object
show all
Defined in:
lib/google/ads/google_ads/utils/path_lookup_definer.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(target, name) ⇒ PathLookupDefiner

Returns a new instance of PathLookupDefiner.



25
26
27
28
# File 'lib/google/ads/google_ads/utils/path_lookup_definer.rb', line 25

def initialize(target, name)
  @target = target
  @name = name
end

Class Method Details

.kwargs_from(method) ⇒ Object



84
85
86
87
88
89
90
# 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) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# 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/google_ads_service/paths"

  mod_host = Object.new
  mod = Kernel.const_get("Google::Ads::GoogleAds::#{version.upcase}::Services::GoogleAdsService::Paths")

  unless mod.respond_to?(:"#{name}_path")
    require "google/ads/google_ads/#{version}/services/#{name}_service/paths"
    const_name = ::Google::Ads::GoogleAds::Utils.camelize(name.to_s)
    mod = Kernel.const_get("Google::Ads::GoogleAds::#{version.upcase}::Services::#{const_name}Service::Paths")
  end

  mod_host.extend(mod)
  mod_host
end

.verify_args(name, params, args, kwargs) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# 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

Instance Method Details

#call(version) ⇒ Object

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.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# 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