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, #add_reader_to, #add_writer_to, #check_valid, create, #deserialize, #packed?, #repeated?, #serialize, #valid_type?

Constructor Details

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

Returns a new instance of EnumField.



569
570
571
572
573
574
575
576
# File 'lib/protocol_buffers/runtime/field.rb', line 569

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.



567
568
569
# File 'lib/protocol_buffers/runtime/field.rb', line 567

def valid_values
  @valid_values
end

#value_to_nameObject (readonly)

Returns the value of attribute value_to_name.



567
568
569
# File 'lib/protocol_buffers/runtime/field.rb', line 567

def value_to_name
  @value_to_name
end

Instance Method Details

#check_value(value) ⇒ Object

Raises:

  • (ArgumentError)


578
579
580
# File 'lib/protocol_buffers/runtime/field.rb', line 578

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

#default_valueObject



582
583
584
# File 'lib/protocol_buffers/runtime/field.rb', line 582

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

#inspect_value(value) ⇒ Object



586
587
588
# File 'lib/protocol_buffers/runtime/field.rb', line 586

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