Sha256: 71feafade3887da01aa84370dc85cb867952e7f80b2379da0d94460d876e7a1c

Contents?: true

Size: 1.31 KB

Versions: 22

Compression:

Stored size: 1.31 KB

Contents

require 'protobuf/field/varint_field'

module Protobuf
  module Field
    class EnumField < VarintField

      ##
      # Class Methods
      #

      def self.default
        fail NoMethodError, "#{self}.#{__method__} must be called on an instance"
      end

      ##
      # Public Instance Methods
      #

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

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

      def decode(value)
        value if acceptable?(value)
      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)
              fail 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
          type_class.fetch(default) || type_class.enums.first
        end
      end

    end
  end
end

Version data entries

22 entries across 22 versions & 3 rubygems

Version Path
protobuf-core-3.5.0 lib/protobuf/field/enum_field.rb
protobuf-3.6.12 lib/protobuf/field/enum_field.rb
protobuf-3.6.11 lib/protobuf/field/enum_field.rb
protobuf-3.6.10 lib/protobuf/field/enum_field.rb
protobuf-3.6.9 lib/protobuf/field/enum_field.rb
protobuf-3.6.7 lib/protobuf/field/enum_field.rb
protobuf-3.6.2 lib/protobuf/field/enum_field.rb
protobuf-3.6.1 lib/protobuf/field/enum_field.rb
protobuf-3.6.0 lib/protobuf/field/enum_field.rb
protobuf-3.5.5 lib/protobuf/field/enum_field.rb
protobuf-3.5.4 lib/protobuf/field/enum_field.rb
protobuf-3.5.3 lib/protobuf/field/enum_field.rb
protobuf-3.5.2 lib/protobuf/field/enum_field.rb
protobuf-3.5.1 lib/protobuf/field/enum_field.rb
prepor-protobuf-3.5.1 lib/protobuf/field/enum_field.rb
prepor-protobuf-3.5.0 lib/protobuf/field/enum_field.rb
protobuf-3.5.0 lib/protobuf/field/enum_field.rb
protobuf-3.4.4 lib/protobuf/field/enum_field.rb
protobuf-3.4.3 lib/protobuf/field/enum_field.rb
protobuf-3.4.2 lib/protobuf/field/enum_field.rb