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(names_out_file_path = nil, names_file_path = nil)
click to toggle source
# File lib/google/apis/generator/annotator.rb, line 45 def initialize(names_out_file_path = nil, names_file_path = nil) if names_out_file_path && File.file?(names_out_file_path) logger.info { sprintf('Loading API names from %s', names_out_file_path) } @names = YAML.load(File.read(names_out_file_path)) || {} else @names = {} end if names_file_path && File.file?(names_file_path) logger.info { sprintf('Loading API names from %s', names_file_path) } @names = @names.merge(YAML.load(File.read(names_file_path)) || {}) end @path = [] end
Public Instance Methods
[](key)
click to toggle source
# File lib/google/apis/generator/annotator.rb, line 86 def [](key) @names[key] end
[]=(key, value)
click to toggle source
# File lib/google/apis/generator/annotator.rb, line 90 def []=(key, value) @names[key] = value end
dump()
click to toggle source
# File lib/google/apis/generator/annotator.rb, line 94 def dump YAML.dump(Hash[@names.sort]) end
infer_method_name_for_rpc(method, pick_name = true)
click to toggle source
For RPC style methods, pick a name based off the request objects. @param [Google::Apis::DiscoveryV1::RestMethod] method @param [Boolean] pick_name
Fragment of the discovery doc describing the method
# File lib/google/apis/generator/annotator.rb, line 110 def infer_method_name_for_rpc(method, pick_name = true) 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 if pick_name pick_name(name) else name end 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 137 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('_') pick_name(method_name) end
infer_parameter_name()
click to toggle source
# File lib/google/apis/generator/annotator.rb, line 68 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 72 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 98 def key @path.reduce('') { |a, e| a + '/' + e } end
option(opt_name)
click to toggle source
# File lib/google/apis/generator/annotator.rb, line 102 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 76 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 59 def with_path(path) @path.push(path) begin yield ensure @path.pop end end