Class: Protobuf::Field::VarintField
- Defined in:
- lib/protobuf/field/varint_field.rb
Direct Known Subclasses
BoolField, EnumField, IntegerField, SignedIntegerField, Uint32Field, Uint64Field
Constant Summary collapse
- INT32_MAX =
Constants
2**31 - 1
- INT32_MIN =
-2**31
- INT64_MAX =
2**63 - 1
- INT64_MIN =
-2**63
- UINT32_MAX =
2**32 - 1
- UINT64_MAX =
2**64 - 1
Constants inherited from BaseField
Instance Attribute Summary
Attributes inherited from BaseField
#message_class, #name, #options, #rule, #tag, #type_class
Class Method Summary collapse
-
.default ⇒ Object
Class Methods.
- .encode(value) ⇒ Object
Instance Method Summary collapse
-
#acceptable?(val) ⇒ Boolean
Public Instance Methods.
- #decode(value) ⇒ Object
- #encode(value) ⇒ Object
- #wire_type ⇒ Object
Methods inherited from BaseField
#coerce!, #default, #default_value, #deprecated?, #enum?, #extension?, #getter, #initialize, #message?, #optional?, #packed?, #repeated?, #repeated_message?, #required?, #set, #setter, #to_s, #type, #warn_if_deprecated
Methods included from Logging
initialize_logger, #log_exception, #log_signature, #logger, logger, logger=, #sign_message
Constructor Details
This class inherits a constructor from Protobuf::Field::BaseField
Class Method Details
.default ⇒ Object
Class Methods
22 23 24 |
# File 'lib/protobuf/field/varint_field.rb', line 22 def self.default 0 end |
.encode(value) ⇒ Object
26 27 28 29 30 31 32 33 |
# File 'lib/protobuf/field/varint_field.rb', line 26 def self.encode(value) bytes = [] until value < 128 bytes << (0x80 | (value & 0x7f)) value >>= 7 end (bytes << value).pack('C*') end |
Instance Method Details
#acceptable?(val) ⇒ Boolean
Public Instance Methods
39 40 41 42 43 |
# File 'lib/protobuf/field/varint_field.rb', line 39 def acceptable?(val) (val > self.class.min || val < self.class.max) rescue false end |
#decode(value) ⇒ Object
45 46 47 |
# File 'lib/protobuf/field/varint_field.rb', line 45 def decode(value) value end |
#encode(value) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/protobuf/field/varint_field.rb', line 49 def encode(value) return [value].pack('C') if value < 128 bytes = [] until value == 0 bytes << (0x80 | (value & 0x7f)) value >>= 7 end bytes[-1] &= 0x7f bytes.pack('C*') end |