Sha256: bb4a5211f9a80d7f9fd8cebf9060c0af0eb7048b85f9e6d9e5340a0599120489

Contents?: true

Size: 1.07 KB

Versions: 3

Compression:

Stored size: 1.07 KB

Contents

require 'protobuf/generators/base'
require 'protobuf/generators/option_generator'

module Protobuf
  module Generators
    class EnumGenerator < Base

      def compile
        run_once(:compile) do
          tags = []

          print_class(descriptor.name, :enum) do
            if descriptor.options
              print OptionGenerator.new(descriptor.options, current_indent).to_s
              puts
            end

            descriptor.value.each do |enum_value_descriptor|
              tags << enum_value_descriptor.number
              puts build_value(enum_value_descriptor)
            end
          end

          unless descriptor.options.try(:allow_alias)
            self.class.validate_tags(fully_qualified_type_namespace, tags)
          end
        end
      end

      def build_value(enum_value_descriptor)
        name = enum_value_descriptor.name
        name.capitalize! if ENV.key?('PB_CAPITALIZE_ENUMS')
        name.upcase! if ENV.key?('PB_UPCASE_ENUMS')
        number = enum_value_descriptor.number
        "define :#{name}, #{number}"
      end

    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
protobuf-3.10.9 lib/protobuf/generators/enum_generator.rb
protobuf-3.10.8 lib/protobuf/generators/enum_generator.rb
protobuf-3.10.7 lib/protobuf/generators/enum_generator.rb