Class: Protod::Proto::Oneof

Inherits:
Part
  • Object
show all
Includes:
FieldNumeringable, Findable
Defined in:
lib/protod/proto/oneof.rb

Instance Method Summary collapse

Methods included from FieldNumeringable

#numbering_fields_with

Methods inherited from Part

#ancestor_as, #has?, #ident=, #push, #root

Instance Method Details

#freezeObject



15
16
17
18
19
20
21
# File 'lib/protod/proto/oneof.rb', line 15

def freeze
  fields.each { _1.depth = depth + 1 }

  @field_map = self.class.findable_keys_for(:field).index_with { |k| fields.index_by(&k.to_sym) }

  super
end

#to_protoObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/protod/proto/oneof.rb', line 23

def to_proto
  field_part = fields.filter_map do |f|
    case f
    when Protod::Proto::Field
      next if f.void?

      f.to_proto
    when Protod::Proto::Oneof
      f.to_proto
    else
      raise NotImplementedError, "Sorry, this is bug forgetting to implement for #{f.class.name}"
    end
  end.join("\n").presence

  field_part = "\n#{field_part}\n" if field_part

  [
    format_proto("oneof %s {%s", ident, field_part),
    format_proto("}")
  ].join
end