class Protobuf::Generators::FieldGenerator

Constants

PROTO_INFINITY_DEFAULT

Constants

PROTO_NAN_DEFAULT
PROTO_NEGATIVE_INFINITY_DEFAULT
RUBY_INFINITY_DEFAULT
RUBY_NAN_DEFAULT
RUBY_NEGATIVE_INFINITY_DEFAULT

Attributes

field_options[R]

Attributes

Public Instance Methods

applicable_options() click to toggle source
# File lib/protobuf/generators/field_generator.rb, line 22
def applicable_options
  @applicable_options ||= field_options.map { |k, v| ":#{k} => #{v}" }
end
compile() click to toggle source
# File lib/protobuf/generators/field_generator.rb, line 55
def compile
  run_once(:compile) do
    field_definition = ["#{label} #{type_name}", name, number, applicable_options]
    puts field_definition.flatten.compact.join(', ')
  end
end
default_value() click to toggle source
# File lib/protobuf/generators/field_generator.rb, line 26
def default_value
  @default_value ||= begin
                       if defaulted?
                         case descriptor.type.name
                         when :TYPE_ENUM then
                           enum_default_value
                         when :TYPE_STRING, :TYPE_BYTES then
                           string_default_value
                         when :TYPE_FLOAT, :TYPE_DOUBLE then
                           float_double_default_value
                         else
                           verbatim_default_value
                         end
                       end
                     end
end
defaulted?() click to toggle source
# File lib/protobuf/generators/field_generator.rb, line 43
def defaulted?
  descriptor.respond_to_has_and_present?(:default_value)
end
deprecated?() click to toggle source
# File lib/protobuf/generators/field_generator.rb, line 47
def deprecated?
  descriptor.options.try(:deprecated?) { false }
end
extension?() click to toggle source
# File lib/protobuf/generators/field_generator.rb, line 51
def extension?
  descriptor.respond_to_has_and_present?(:extendee)
end
label() click to toggle source
# File lib/protobuf/generators/field_generator.rb, line 62
def label
  @label ||= descriptor.label.name.to_s.downcase.sub(/label_/, '') # required, optional, repeated
end
name() click to toggle source
# File lib/protobuf/generators/field_generator.rb, line 66
def name
  @name ||= ":#{descriptor.name}"
end
number() click to toggle source
# File lib/protobuf/generators/field_generator.rb, line 70
def number
  @number ||= descriptor.number
end
packed?() click to toggle source
# File lib/protobuf/generators/field_generator.rb, line 85
def packed?
  descriptor.options.try(:packed?) { false }
end
type_name() click to toggle source

Determine the field type

# File lib/protobuf/generators/field_generator.rb, line 90
def type_name
  @type_name ||= begin
                   case descriptor.type.name
                   when :TYPE_MESSAGE, :TYPE_ENUM, :TYPE_GROUP then
                     modulize(descriptor.type_name)
                   else
                     type_name = descriptor.type.name.to_s.downcase.sub(/type_/, '')
                     ":#{type_name}"
                   end
                 end
end

Private Instance Methods

enum_default_value() click to toggle source
# File lib/protobuf/generators/field_generator.rb, line 104
def enum_default_value
  "#{type_name}::#{verbatim_default_value}"
end
float_double_default_value() click to toggle source
# File lib/protobuf/generators/field_generator.rb, line 108
def float_double_default_value
  case verbatim_default_value
  when PROTO_INFINITY_DEFAULT then
    RUBY_INFINITY_DEFAULT
  when PROTO_NEGATIVE_INFINITY_DEFAULT then
    RUBY_NEGATIVE_INFINITY_DEFAULT
  when PROTO_NAN_DEFAULT then
    RUBY_NAN_DEFAULT
  else
    verbatim_default_value
  end
end
string_default_value() click to toggle source
# File lib/protobuf/generators/field_generator.rb, line 121
def string_default_value
  %("#{verbatim_default_value.gsub(/'/, '\\\\\'')}")
end
verbatim_default_value() click to toggle source
# File lib/protobuf/generators/field_generator.rb, line 125
def verbatim_default_value
  descriptor.default_value
end