Module: BSON::Integer
- Defined in:
- lib/bson/integer.rb
Overview
Injects behaviour for encoding and decoding integer values to and from raw bytes as specified by the BSON spec.
Constant Summary collapse
- INT32_TYPE =
A 32bit integer is type 0x10 in the BSON spec.
16.chr.force_encoding(BINARY).freeze
- INT64_TYPE =
A 64bit integer is type 0x12 in the BSON spec.
18.chr.force_encoding(BINARY).freeze
- MAX_32BIT =
The maximum 32 bit integer value.
(1 << 31) - 1
- MAX_64BIT =
The maximum 64 bit integer value.
(1 << 63) - 1
- MIN_32BIT =
The minimum 32 bit integer value.
-(1 << 31)
- MIN_64BIT =
The minimum 64 bit integer value.
-(1 << 63)
- BSON_INDEX_SIZE =
The BSON index size.
1024.freeze
- BSON_ARRAY_INDEXES =
A hash of index values for array optimization.
::Array.new(BSON_INDEX_SIZE) do |i| (i.to_s.force_encoding(BINARY) << NULL_BYTE).freeze end.freeze
Instance Method Summary collapse
-
#bson_int32? ⇒ true, false
Is this integer a valid BSON 32 bit value?.
-
#bson_int64? ⇒ true, false
Is this integer a valid BSON 64 bit value?.
-
#bson_type ⇒ String
Get the BSON type for this integer.
-
#to_bson(encoded = ''.force_encoding(BINARY)) ⇒ String
Get the integer as encoded BSON.
-
#to_bson_int32(encoded) ⇒ String
Convert the integer to a 32 bit (4 bytes) raw bytes string.
-
#to_bson_int64(encoded) ⇒ String
Convert the integer to a 64 bit (8 bytes) raw bytes string.
- #to_bson_key(encoded = ''.force_encoding(BINARY)) ⇒ Object
Instance Method Details
#bson_int32? ⇒ true, false
Is this integer a valid BSON 32 bit value?
75 76 77 |
# File 'lib/bson/integer.rb', line 75 def bson_int32? (MIN_32BIT <= self) && (self <= MAX_32BIT) end |
#bson_int64? ⇒ true, false
Is this integer a valid BSON 64 bit value?
87 88 89 |
# File 'lib/bson/integer.rb', line 87 def bson_int64? (MIN_64BIT <= self) && (self <= MAX_64BIT) end |
#bson_type ⇒ String
Get the BSON type for this integer. Will depend on whether the integer is 32 bit or 64 bit.
102 103 104 |
# File 'lib/bson/integer.rb', line 102 def bson_type bson_int32? ? INT32_TYPE : (bson_int64? ? INT64_TYPE : out_of_range!) end |
#to_bson(encoded = ''.force_encoding(BINARY)) ⇒ String
Get the integer as encoded BSON.
116 117 118 119 120 121 122 123 124 |
# File 'lib/bson/integer.rb', line 116 def to_bson(encoded = ''.force_encoding(BINARY)) if bson_int32? to_bson_int32(encoded) elsif bson_int64? to_bson_int64(encoded) else out_of_range! end end |
#to_bson_int32(encoded) ⇒ String
Convert the integer to a 32 bit (4 bytes) raw bytes string.
136 137 138 |
# File 'lib/bson/integer.rb', line 136 def to_bson_int32(encoded) append_bson_int32(encoded) end |
#to_bson_int64(encoded) ⇒ String
Convert the integer to a 64 bit (8 bytes) raw bytes string.
150 151 152 153 154 155 156 |
# File 'lib/bson/integer.rb', line 150 def to_bson_int64(encoded) append_bson_int32(encoded) encoded << ((self >> 32) & 255) encoded << ((self >> 40) & 255) encoded << ((self >> 48) & 255) encoded << ((self >> 56) & 255) end |
#to_bson_key(encoded = ''.force_encoding(BINARY)) ⇒ Object
158 159 160 161 162 163 164 |
# File 'lib/bson/integer.rb', line 158 def to_bson_key(encoded = ''.force_encoding(BINARY)) if self < BSON_INDEX_SIZE encoded << BSON_ARRAY_INDEXES[self] else self.to_s.to_bson_cstring(encoded) end end |