Class: FileDescriptorToRuby

Inherits:
Struct
  • Object
show all
Includes:
Google::Protobuf::FieldDescriptorProto::Label, Google::Protobuf::FieldDescriptorProto::Type
Defined in:
lib/protocol_buffers/compiler/file_descriptor_to_ruby.rb

Constant Summary

Constants included from Google::Protobuf::FieldDescriptorProto::Label

Google::Protobuf::FieldDescriptorProto::Label::LABEL_OPTIONAL, Google::Protobuf::FieldDescriptorProto::Label::LABEL_REPEATED, Google::Protobuf::FieldDescriptorProto::Label::LABEL_REQUIRED

Constants included from Google::Protobuf::FieldDescriptorProto::Type

Google::Protobuf::FieldDescriptorProto::Type::TYPE_BOOL, Google::Protobuf::FieldDescriptorProto::Type::TYPE_BYTES, Google::Protobuf::FieldDescriptorProto::Type::TYPE_DOUBLE, Google::Protobuf::FieldDescriptorProto::Type::TYPE_ENUM, Google::Protobuf::FieldDescriptorProto::Type::TYPE_FIXED32, Google::Protobuf::FieldDescriptorProto::Type::TYPE_FIXED64, Google::Protobuf::FieldDescriptorProto::Type::TYPE_FLOAT, Google::Protobuf::FieldDescriptorProto::Type::TYPE_GROUP, Google::Protobuf::FieldDescriptorProto::Type::TYPE_INT32, Google::Protobuf::FieldDescriptorProto::Type::TYPE_INT64, Google::Protobuf::FieldDescriptorProto::Type::TYPE_MESSAGE, Google::Protobuf::FieldDescriptorProto::Type::TYPE_SFIXED32, Google::Protobuf::FieldDescriptorProto::Type::TYPE_SFIXED64, Google::Protobuf::FieldDescriptorProto::Type::TYPE_SINT32, Google::Protobuf::FieldDescriptorProto::Type::TYPE_SINT64, Google::Protobuf::FieldDescriptorProto::Type::TYPE_STRING, Google::Protobuf::FieldDescriptorProto::Type::TYPE_UINT32, Google::Protobuf::FieldDescriptorProto::Type::TYPE_UINT64

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ProtocolBuffers::Enum

included

Constructor Details

#initialize(descriptor) ⇒ FileDescriptorToRuby

Returns a new instance of FileDescriptorToRuby.



9
10
11
12
13
# File 'lib/protocol_buffers/compiler/file_descriptor_to_ruby.rb', line 9

def initialize(descriptor)
  super
  @package_modules = descriptor.package ? descriptor.package.split('.') : []
  @ns = []
end

Instance Attribute Details

#descriptorObject

Returns the value of attribute descriptor

Returns:

  • (Object)

    the current value of descriptor



4
5
6
# File 'lib/protocol_buffers/compiler/file_descriptor_to_ruby.rb', line 4

def descriptor
  @descriptor
end

Instance Method Details

#write(io) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/protocol_buffers/compiler/file_descriptor_to_ruby.rb', line 15

def write(io)
  @io = io

  @io.write <<HEADER
#!/usr/bin/env ruby
# Generated by the protocol buffer compiler. DO NOT EDIT!

require 'protocol_buffers'

HEADER

  descriptor.dependency.each do |dep|
    dir      = File.dirname(dep)
    filename = File.basename(dep, ".proto") + ".pb"
    path = if dir == '.'
      filename
    else
      File.join(dir, filename)
    end
    @io.write("begin; require '#{path}'; rescue LoadError; end\n")
  end
  @io.write("\n") unless descriptor.dependency.empty?

  in_namespace("module", @package_modules) do
    declare(descriptor.package, descriptor.message_type, descriptor.enum_type)

    descriptor.message_type.each do |message|
      dump_message(descriptor.package, message)
    end

    descriptor.service.each do |service|
      dump_service(descriptor.package, service)
    end
  end
  
end