Sha256: 9bce1690fc28be7d7af1bda4f71a51bd31a647cfe89fa7f771ff9e885de2b512

Contents?: true

Size: 1.49 KB

Versions: 2

Compression:

Stored size: 1.49 KB

Contents

require 'protobuf/field/varint_field'

module Protobuf
  module Field
    class EnumField < VarintField
      def acceptable?(val)
        case val
        when Symbol then
          raise TypeError unless @type.const_defined?(val)
        when EnumValue then
          raise TypeError if val.parent_class != @type
        else
          raise TypeError unless @type.valid_tag?(val)
        end
        true
      end

      def encode(value)
        super(value.to_i)
      end

      private

      def typed_default_value
        if @default.is_a?(Symbol)
          @type.const_get(@default)
        else
          self.class.default
        end
      end

      def define_setter
        field = self
        @message_class.class_eval do
          define_method("#{field.name}=") do |val|
            if val.nil?
              @values.delete(field.name)
            else
              val = \
                case val
                when Symbol then
                  field.type.const_get(val) rescue nil
                when Integer then
                  field.type.const_get(field.type.name_by_value(val)) rescue nil
                when EnumValue then
                  raise TypeError, "Invalid value: #{val.inspect} for #{field.name}" if val.parent_class != field.type
                  val
                end
              raise TypeError, "Invalid value: #{val.inspect} for #{field.name}" unless val

              @values[field.name] = val
            end
          end
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
protobuf-2.0.0.rc2 lib/protobuf/field/enum_field.rb
protobuf-2.0.0.rc1 lib/protobuf/field/enum_field.rb