Class: ProtoPlugin::FileDescriptor

Inherits:
SimpleDelegator
  • Object
show all
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

Constructor Details

#initialize(descriptor) ⇒ FileDescriptor

Returns a new instance of FileDescriptor.

Parameters:

  • descriptor (Google::Protobuf::FileDescriptorProto)


18
19
20
21
# File 'lib/proto_plugin/file_descriptor.rb', line 18

def initialize(descriptor)
  super
  @descriptor = descriptor
end

Instance Attribute Details

#descriptorGoogle::Protobuf::FileDescriptorProto (readonly)

Returns:

  • (Google::Protobuf::FileDescriptorProto)


15
16
17
# File 'lib/proto_plugin/file_descriptor.rb', line 15

def descriptor
  @descriptor
end

Instance Method Details

#enumsArray<EnumDescriptor>

The enums defined as children of this file.



29
30
31
32
33
# File 'lib/proto_plugin/file_descriptor.rb', line 29

def enums
  @enums ||= @descriptor.enum_type.map do |e|
    EnumDescriptor.new(e, self)
  end
end

#messagesArray<MessageDescriptor>

The messages defined as children of this file.



41
42
43
44
45
# File 'lib/proto_plugin/file_descriptor.rb', line 41

def messages
  @messages ||= @descriptor.message_type.map do |m|
    MessageDescriptor.new(m, self)
  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.

Examples:

Using package my.protobuf.package;

file.namespace #=> "My::Protobuf::Package"

Using option ruby_package = "My::Ruby::Package";

file.namespace #=> "My::Ruby::Package"

Parameters:

  • split (Boolean) (defaults to: false)

    Returns the namespace as an array of module names.

Returns:

  • (String)

    The namespace for the file.

  • (Array<String>)

    If split: true, the namespace as an array of module names.



62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/proto_plugin/file_descriptor.rb', line 62

def namespace(split: false)
  @namespace ||= begin
    namespace = @descriptor.options&.ruby_package
    if !namespace || namespace.empty?
      namespace = @descriptor.package.split(".")
        .map { |token| Utils.camelize(token) }
        .join("::")
    end
    namespace
  end
  split ? @namespace.split("::") : @namespace
end

#servicesArray<ServiceDescriptor>

The services defined in this file.



81
82
83
84
85
# File 'lib/proto_plugin/file_descriptor.rb', line 81

def services
  @services ||= @descriptor.service.map do |s|
    ServiceDescriptor.new(s, self)
  end
end