Class: ProtocolBuffers::Field::EnumField
- Inherits:
-
Int32Field
- Object
- ProtocolBuffers::Field
- NumericField
- VarintField
- SignedVarintField
- Int32Field
- ProtocolBuffers::Field::EnumField
- Defined in:
- lib/protocol_buffers/runtime/field.rb
Instance Attribute Summary collapse
-
#valid_values ⇒ Object
readonly
Returns the value of attribute valid_values.
-
#value_to_name ⇒ Object
readonly
Returns the value of attribute value_to_name.
Attributes inherited from ProtocolBuffers::Field
Instance Method Summary collapse
- #check_value(value) ⇒ Object
- #default_value ⇒ Object
-
#initialize(proxy_enum, otype, name, tag, opts = {}) ⇒ EnumField
constructor
A new instance of EnumField.
- #inspect_value(value) ⇒ Object
Methods inherited from Int32Field
Methods inherited from SignedVarintField
Methods inherited from VarintField
Methods included from WireFormats::VARINT
Methods inherited from NumericField
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_values ⇒ Object (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_name ⇒ Object (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
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_value ⇒ Object
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 |