Class: Protobuf::Descriptor::Descriptor

Inherits:
Object
  • Object
show all
Defined in:
lib/protobuf/descriptor/descriptor.rb

Instance Method Summary collapse

Constructor Details

#initialize(message_class) ⇒ Descriptor

Returns a new instance of Descriptor.



4
5
6
# File 'lib/protobuf/descriptor/descriptor.rb', line 4

def initialize(message_class)
  @message_class = message_class
end

Instance Method Details

#build(proto, opt = {}) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/protobuf/descriptor/descriptor.rb', line 12

def build(proto, opt={})
  mod = opt[:module]
  cls = mod.const_set proto.name, Class.new(@message_class)
  proto.nested_type.each do |message_proto|
    Protobuf::Message.descriptor.build message_proto, :module => cls
  end
  proto.enum_type.each do |enum_proto|
    Protobuf::Enum.descriptor.build enum_proto, :module => cls
  end
  proto.field.each do |field_proto|
    Protobuf::Field::BaseField.descriptor.build field_proto, :class => cls
  end
end

#innerclass?(parent, child) ⇒ Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/protobuf/descriptor/descriptor.rb', line 48

def innerclass?(parent, child)
  child.name =~ /::/ and child.name.split('::')[0..-2].join('::') == parent.name
end

#proto_typeObject



8
9
10
# File 'lib/protobuf/descriptor/descriptor.rb', line 8

def proto_type
  'Google::Protobuf::DescriptorProto'
end

#unbuild(parent_proto) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/protobuf/descriptor/descriptor.rb', line 26

def unbuild(parent_proto)
  message_proto = Google::Protobuf::DescriptorProto.new
  message_proto.name = @message_class.to_s.split('::').last
  @message_class.fields.each do |tag, field|
    field.descriptor.unbuild message_proto
  end
  ObjectSpace.each_object(Class) do |cls|
    if innerclass? @message_class, cls
      cls.descriptor.unbuild message_proto
    end
  end

  case parent_proto
  when Google::Protobuf::FileDescriptorProto
    parent_proto.message_type << message_proto
  when Google::Protobuf::DescriptorProto
    parent_proto.nested_type << message_proto
  else
    raise TypeError.new(parent_proto.class.name)
  end
end