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.
- #coerce!(val) ⇒ Object
- #decode(value) ⇒ Object
- #encode(value) ⇒ Object
- #wire_type ⇒ Object
Methods inherited from BaseField
#default, #default_value, #deprecated?, #enum?, #extension?, #getter, #initialize, #message?, #optional?, #packed?, #repeated?, #repeated_message?, #required?, #set, #setter, #to_s
Methods included from Logging
initialize_logger, #log_exception, #log_signature, #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 44 |
# File 'lib/protobuf/field/varint_field.rb', line 39 def acceptable?(val) int_val = coerce!(val) int_val >= self.class.min && int_val <= self.class.max rescue false end |
#coerce!(val) ⇒ Object
46 47 48 49 |
# File 'lib/protobuf/field/varint_field.rb', line 46 def coerce!(val) return val.to_i if val.is_a?(Numeric) Integer(val, 10) end |
#decode(value) ⇒ Object
51 52 53 |
# File 'lib/protobuf/field/varint_field.rb', line 51 def decode(value) value end |
#encode(value) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/protobuf/field/varint_field.rb', line 55 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 |