Class: FileDescriptorToRuby

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

Constant Summary

Constants included from FieldDescriptorProto::Label

FieldDescriptorProto::Label::LABEL_OPTIONAL, FieldDescriptorProto::Label::LABEL_REPEATED, FieldDescriptorProto::Label::LABEL_REQUIRED

Constants included from FieldDescriptorProto::Type

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

Instance Attribute Summary collapse

Instance Method Summary collapse

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 = capfirst(descriptor.package_)
  @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
# 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|
    path = File.basename(dep, ".proto") + ".pb"
    @io.write("begin; require '#{path}'; rescue LoadError; end\n")
  end
  @io.write("\n") unless descriptor.dependency.empty?

  # in_namespace correctly handles the case where @package.nil?
  unless @package.empty?
    @io.write("# Reload support\nObject.__send__(:remove_const, :#{@package}) if defined?(#{@package})\n\n")
  end

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

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

end