Module: BSON::String
- Defined in:
- lib/bson/string.rb
Overview
Injects behaviour for encoding and decoding string values to and from raw bytes as specified by the BSON spec.
Defined Under Namespace
Modules: ClassMethods
Constant Summary collapse
- BSON_TYPE =
A string is type 0x02 in the BSON spec.
::String.new(2.chr, encoding: BINARY).freeze
- ILLEGAL_KEY =
Regex for matching illegal BSON keys.
/(\A[$])|(\.)/
Instance Method Summary collapse
-
#to_bson(buffer = ByteBuffer.new) ⇒ BSON::ByteBuffer
Get the string as encoded BSON.
-
#to_bson_key ⇒ String
Get the string as a BSON key name encoded C string with checking for special characters.
-
#to_bson_object_id ⇒ String
Convert the string to an object id.
-
#to_hex_string ⇒ String
Convert the string to a hexidecimal representation.
Instance Method Details
#to_bson(buffer = ByteBuffer.new) ⇒ BSON::ByteBuffer
Get the string as encoded BSON.
50 51 52 |
# File 'lib/bson/string.rb', line 50 def to_bson(buffer = ByteBuffer.new) buffer.put_string(self) end |
#to_bson_key ⇒ String
Get the string as a BSON key name encoded C string with checking for special characters.
66 67 68 |
# File 'lib/bson/string.rb', line 66 def to_bson_key self end |
#to_bson_object_id ⇒ String
Note:
This is used for repairing legacy bson data.
Convert the string to an object id. This will only work for strings of size 12.
83 84 85 |
# File 'lib/bson/string.rb', line 83 def to_bson_object_id ObjectId.repair(self) end |
#to_hex_string ⇒ String
Convert the string to a hexidecimal representation.
95 96 97 |
# File 'lib/bson/string.rb', line 95 def to_hex_string unpack("H*")[0] end |