class Google::Apis::Generator::Names

Helper for picking names for methods, properties, types, etc. Performs various normaliations as well as allows for overriding individual names from a configuration file for cases where algorithmic approaches produce poor APIs.

Public Class Methods

new(file_path = nil) click to toggle source
# File lib/google/apis/generator/annotator.rb, line 37
def initialize(file_path = nil)
  if file_path
    logger.info { sprintf('Loading API names from %s', file_path) }
    @names = YAML.load(File.read(file_path)) || {}
  else
    @names = {}
  end
  @path = []
end

Public Instance Methods

[]=(key, value) click to toggle source
# File lib/google/apis/generator/annotator.rb, line 81
def []=(key, value)
  @names[key] = value
end
dump() click to toggle source
# File lib/google/apis/generator/annotator.rb, line 85
def dump
  YAML.dump(@names)
end
infer_method_name(method) click to toggle source

Determine the ruby method name to generate for a given method in discovery. @param [Google::Apis::DiscoveryV1::RestMethod] method

Fragment of the discovery doc describing the method
# File lib/google/apis/generator/annotator.rb, line 63
def infer_method_name(method)
  pick_name(infer_method_name_for_rpc(method) || infer_method_name_from_id(method))
end
infer_parameter_name() click to toggle source
# File lib/google/apis/generator/annotator.rb, line 56
def infer_parameter_name
  pick_name(normalize_param_name(@path.last))
end
infer_property_name() click to toggle source
# File lib/google/apis/generator/annotator.rb, line 67
def infer_property_name
  pick_name(normalize_property_name(@path.last))
end
key() click to toggle source
# File lib/google/apis/generator/annotator.rb, line 89
def key
  @path.reduce('') { |a, e| a + '/' + e }
end
option(opt_name) click to toggle source
# File lib/google/apis/generator/annotator.rb, line 93
def option(opt_name)
  @names[sprintf('%s?%s', key, opt_name)]
end
pick_name(alt_name) click to toggle source
# File lib/google/apis/generator/annotator.rb, line 71
def pick_name(alt_name)
  preferred_name = @names[key]
  if preferred_name && preferred_name == alt_name
    logger.warn { sprintf("Unnecessary name override '%s': %s", key, alt_name) }
  elsif preferred_name.nil?
    preferred_name = @names[key] = alt_name
  end
  preferred_name
end
with_path(path) { || ... } click to toggle source
# File lib/google/apis/generator/annotator.rb, line 47
def with_path(path)
  @path.push(path)
  begin
    yield
  ensure
    @path.pop
  end
end

Private Instance Methods

infer_method_name_for_rpc(method) click to toggle source

For RPC style methods, pick a name based off the request objects. @param [Google::Apis::DiscoveryV1::RestMethod] method

Fragment of the discovery doc describing the method
# File lib/google/apis/generator/annotator.rb, line 102
def infer_method_name_for_rpc(method)
  return nil if method.request.nil?
  parts = method.id.split('.')
  parts.shift
  verb = ActiveSupport::Inflector.underscore(parts.pop)
  match = method.request._ref.match(/(.*)(?i:request)/)
  return nil if match.nil?
  name = ActiveSupport::Inflector.underscore(match[1])
  return nil unless name == verb || name.start_with?(verb + '_')
  if !parts.empty?
    resource_name = ActiveSupport::Inflector.singularize(parts.pop)
    resource_name = ActiveSupport::Inflector.underscore(resource_name)
    if !name.include?(resource_name)
      name = name.split('_').insert(1, resource_name).join('_')
    end
  end
  name
end
infer_method_name_from_id(method) click to toggle source

For REST style methods, build a method name from the verb/resource(s) in the method id. IDs are in the form <api>.<resource>.<verb> @param [Google::Apis::DiscoveryV1::RestMethod] method

Fragment of the discovery doc describing the method
# File lib/google/apis/generator/annotator.rb, line 125
def infer_method_name_from_id(method)
  parts = method.id.split('.')
  parts.shift
  verb = ActiveSupport::Inflector.underscore(parts.pop)
  return verb if parts.empty?
  resource_name = ActiveSupport::Inflector.underscore(parts.pop)
  if pluralize_method?(verb)
    resource_name = ActiveSupport::Inflector.pluralize(resource_name)
  else
    resource_name = ActiveSupport::Inflector.singularize(resource_name)
  end
  if parts.empty?
    resource_path = resource_name
  else
    resource_path = parts.map do |p|
      p = ActiveSupport::Inflector.singularize(p)
      ActiveSupport::Inflector.underscore(p)
    end.join('_') + '_' + resource_name
  end
  method_name = verb.split('_').insert(1, resource_path.split('_')).join('_')
  method_name
end