Class: ProtocolBuffers::Field::EnumField

Inherits:
Int32Field show all
Defined in:
lib/protocol_buffers/runtime/field.rb

Instance Attribute Summary collapse

Attributes inherited from ProtocolBuffers::Field

#name, #otype, #tag

Instance Method Summary collapse

Methods inherited from Int32Field

#bits, #max, #min

Methods inherited from SignedVarintField

#deserialize

Methods inherited from VarintField

#valid_type?

Methods included from WireFormats::VARINT

#wire_type

Methods inherited from NumericField

#max, #min

Methods inherited from ProtocolBuffers::Field

#add_methods_to, #check_valid, create, #deserialize, #repeated?, #serialize, #valid_type?

Constructor Details

#initialize(proxy_enum, otype, name, tag, opts = {}) ⇒ EnumField

Returns a new instance of EnumField.



499
500
501
502
503
504
505
506
# File 'lib/protocol_buffers/runtime/field.rb', line 499

def initialize(proxy_enum, otype, name, tag, opts = {})
  super(otype, name, tag, opts)
  @proxy_enum = proxy_enum
  @valid_values = @proxy_enum.constants.map { |c| @proxy_enum.const_get(c) }.sort
  @value_to_name = @proxy_enum.constants.inject({}) { |h, c|
    h[@proxy_enum.const_get(c)] = c.to_s; h
  }
end

Instance Attribute Details

#valid_valuesObject (readonly)

Returns the value of attribute valid_values.



497
498
499
# File 'lib/protocol_buffers/runtime/field.rb', line 497

def valid_values
  @valid_values
end

#value_to_nameObject (readonly)

Returns the value of attribute value_to_name.



497
498
499
# File 'lib/protocol_buffers/runtime/field.rb', line 497

def value_to_name
  @value_to_name
end

Instance Method Details

#check_value(value) ⇒ Object

Raises:

  • (ArgumentError)


508
509
510
# File 'lib/protocol_buffers/runtime/field.rb', line 508

def check_value(value)
  raise(ArgumentError, "value is out of range for #{self.class.name}: #{value}") unless @valid_values.include?(value)
end

#default_valueObject



512
513
514
# File 'lib/protocol_buffers/runtime/field.rb', line 512

def default_value
  @opts[:default] || @valid_values.first
end

#inspect_value(value) ⇒ Object



516
517
518
# File 'lib/protocol_buffers/runtime/field.rb', line 516

def inspect_value(value)
  "#{@value_to_name[value]}(#{value})"
end