Sha256: caec79ae99a88bec346fd70d9cc8d0ade14172bed82f2b00fadbc897b6b38ab7

Contents?: true

Size: 1.06 KB

Versions: 14

Compression:

Stored size: 1.06 KB

Contents

require 'protobuf/field/varint_field'

module Protobuf
  module Field
    class EnumField < VarintField

      ##
      # Public Instance Methods
      #

      def acceptable?(val)
        ! type_class.fetch(val).nil?
      end

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

      def enum?
        true
      end

      private

      ##
      # Private Instance Methods
      #

      def define_setter
        field = self
        message_class.class_eval do
          define_method("#{field.name}=") do |value|
            orig_value = value
            if value.nil?
              @values.delete(field.name)
            else
              value = field.type_class.fetch(value)
              raise TypeError, "Invalid Enum value: #{orig_value.inspect} for #{field.name}" unless value

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

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

    end
  end
end

Version data entries

14 entries across 14 versions & 2 rubygems

Version Path
protobuffy-3.6.0 lib/protobuf/field/enum_field.rb
protobuffy-3.5.1 lib/protobuf/field/enum_field.rb
protobuffy-3.4.0 lib/protobuf/field/enum_field.rb
protobuffy-3.3.0 lib/protobuf/field/enum_field.rb
protobuffy-3.2.0 lib/protobuf/field/enum_field.rb
protobuffy-3.1.0 lib/protobuf/field/enum_field.rb
protobuf-3.0.5 lib/protobuf/field/enum_field.rb
protobuf-3.0.4 lib/protobuf/field/enum_field.rb
protobuf-3.0.3 lib/protobuf/field/enum_field.rb
protobuf-3.0.2 lib/protobuf/field/enum_field.rb
protobuf-3.0.1 lib/protobuf/field/enum_field.rb
protobuf-3.0.0 lib/protobuf/field/enum_field.rb
protobuf-3.0.0.rc2 lib/protobuf/field/enum_field.rb
protobuf-3.0.0.rc1 lib/protobuf/field/enum_field.rb