Class: ProtoPlugin::FileDescriptor
- Inherits:
-
SimpleDelegator
- Object
- SimpleDelegator
- ProtoPlugin::FileDescriptor
- Includes:
- Commentable
- Defined in:
- lib/proto_plugin/file_descriptor.rb
Overview
A wrapper class around Google::Protobuf::FileDescriptorProto which provides helpers and more idiomatic Ruby access patterns.
Any method not defined directly is delegated to the descriptor the wrapper was initialized with.
Instance Attribute Summary collapse
Instance Method Summary collapse
-
#enums ⇒ Array<EnumDescriptor>
The enums defined as children of this file.
-
#file ⇒ FileDescriptor
The file descriptor this element belongs to.
-
#imports ⇒ Array<FileDescriptor>
The files imported by this file, resolved to their descriptors.
-
#initialize(context, descriptor) ⇒ FileDescriptor
constructor
A new instance of FileDescriptor.
-
#location_for(proto) ⇒ Google::Protobuf::SourceCodeInfo::Location?
Returns the
SourceCodeInfo::Locationfor a given raw descriptor proto defined within this file, if source info was included in the request. -
#messages ⇒ Array<MessageDescriptor>
The messages defined as children of this file.
-
#namespace(split: false) ⇒ String+
Returns the Ruby namespace (module) for the file.
-
#public_imports ⇒ Array<FileDescriptor>
The subset of #imports that were declared with
import public, resolved to their descriptors. -
#services ⇒ Array<ServiceDescriptor>
The services defined in this file.
Methods included from Commentable
#comments, #leading_comments, #source_location, #trailing_comments
Constructor Details
#initialize(context, descriptor) ⇒ FileDescriptor
Returns a new instance of FileDescriptor.
21 22 23 24 25 |
# File 'lib/proto_plugin/file_descriptor.rb', line 21 def initialize(context, descriptor) super(descriptor) @context = context @descriptor = descriptor end |
Instance Attribute Details
#descriptor ⇒ Google::Protobuf::FileDescriptorProto (readonly)
17 18 19 |
# File 'lib/proto_plugin/file_descriptor.rb', line 17 def descriptor @descriptor end |
Instance Method Details
#enums ⇒ Array<EnumDescriptor>
The enums defined as children of this file.
53 54 55 56 57 |
# File 'lib/proto_plugin/file_descriptor.rb', line 53 def enums @enums ||= @descriptor.enum_type.map do |e| EnumDescriptor.new(e, self) end end |
#file ⇒ FileDescriptor
The file descriptor this element belongs to.
For a FileDescriptor this is the descriptor itself. Defined so that Commentable can resolve comments uniformly across all descriptor types.
33 34 35 |
# File 'lib/proto_plugin/file_descriptor.rb', line 33 def file self end |
#imports ⇒ Array<FileDescriptor>
The files imported by this file, resolved to their descriptors.
Imports that were not included in the request are omitted.
119 120 121 122 123 |
# File 'lib/proto_plugin/file_descriptor.rb', line 119 def imports @imports ||= @descriptor.dependency.filter_map do |name| @context.file_by_filename(name) end end |
#location_for(proto) ⇒ Google::Protobuf::SourceCodeInfo::Location?
Returns the SourceCodeInfo::Location for a given raw descriptor proto defined within this file, if source info was included in the request.
43 44 45 |
# File 'lib/proto_plugin/file_descriptor.rb', line 43 def location_for(proto) source_locations[proto] end |
#messages ⇒ Array<MessageDescriptor>
The messages defined as children of this file.
65 66 67 68 69 |
# File 'lib/proto_plugin/file_descriptor.rb', line 65 def @messages ||= @descriptor..map do |m| MessageDescriptor.new(m, self, @context) end end |
#namespace(split: false) ⇒ String+
Returns the Ruby namespace (module) for the file.
If the ruby_package option was specified, then that value is returned directly. Otherwise, the package value is transformed to Ruby module notation.
86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/proto_plugin/file_descriptor.rb', line 86 def namespace(split: false) @namespace ||= begin namespace = @descriptor.&.ruby_package if !namespace || namespace.empty? namespace = @descriptor.package.split(".") .map { |token| Utils.camelize(token) } .join("::") end namespace end split ? @namespace.split("::") : @namespace end |
#public_imports ⇒ Array<FileDescriptor>
The subset of #imports that were declared with import public, resolved to their descriptors.
132 133 134 135 136 137 |
# File 'lib/proto_plugin/file_descriptor.rb', line 132 def public_imports @public_imports ||= @descriptor.public_dependency.filter_map do |i| name = @descriptor.dependency[i] @context.file_by_filename(name) if name end end |
#services ⇒ Array<ServiceDescriptor>
The services defined in this file.
105 106 107 108 109 |
# File 'lib/proto_plugin/file_descriptor.rb', line 105 def services @services ||= @descriptor.service.map do |s| ServiceDescriptor.new(s, self, @context) end end |