File tree Expand file tree Collapse file tree 4 files changed +208
-0
lines changed
Expand file tree Collapse file tree 4 files changed +208
-0
lines changed Original file line number Diff line number Diff line change @@ -50,6 +50,52 @@ def self.from_bson(buffer)
5050 buffer . get_int32
5151 end
5252
53+ # Instantiate a BSON Int32.
54+ #
55+ # @param [ Integer ] integer The 32-bit integer.
56+ #
57+ # @see http://bsonspec.org/#/specification
58+ #
59+ # @since 4.2.0
60+ def initialize ( integer )
61+ out_of_range! unless integer . bson_int32?
62+ @integer = integer . freeze
63+ end
64+
65+ # Append the integer as encoded BSON to a ByteBuffer.
66+ #
67+ # @example Encoded the integer and append to a ByteBuffer.
68+ # int32.to_bson
69+ #
70+ # @return [ BSON::ByteBuffer ] The buffer with the encoded integer.
71+ #
72+ # @see http://bsonspec.org/#/specification
73+ #
74+ # @since 4.2.0
75+ def to_bson ( buffer = ByteBuffer . new , validating_keys = Config . validating_keys? )
76+ buffer . put_int32 ( @integer )
77+ end
78+
79+ # Convert the integer to a BSON string key.
80+ #
81+ # @example Convert the integer to a BSON key string.
82+ # int.to_bson_key
83+ #
84+ # @param [ true, false ] validating_keys If BSON should validate the key.
85+ #
86+ # @return [ String ] The string key.
87+ #
88+ # @since 4.2.0
89+ def to_bson_key ( validating_keys = Config . validating_keys? )
90+ @integer . to_bson_key ( validating_keys )
91+ end
92+
93+ private
94+
95+ def out_of_range!
96+ raise RangeError . new ( "#{ self } is not a valid 4 byte integer value." )
97+ end
98+
5399 # Register this type when the module is loaded.
54100 #
55101 # @since 2.0.0
Original file line number Diff line number Diff line change @@ -45,6 +45,52 @@ def self.from_bson(buffer)
4545 buffer . get_int64
4646 end
4747
48+ # Instantiate a BSON Int64.
49+ #
50+ # @param [ Integer ] integer The 64-bit integer.
51+ #
52+ # @see http://bsonspec.org/#/specification
53+ #
54+ # @since 4.2.0
55+ def initialize ( integer )
56+ out_of_range! unless integer . bson_int64?
57+ @integer = integer . freeze
58+ end
59+
60+ # Append the integer as encoded BSON to a ByteBuffer.
61+ #
62+ # @example Encoded the integer and append to a ByteBuffer.
63+ # int64.to_bson
64+ #
65+ # @return [ BSON::ByteBuffer ] The buffer with the encoded integer.
66+ #
67+ # @see http://bsonspec.org/#/specification
68+ #
69+ # @since 4.2.0
70+ def to_bson ( buffer = ByteBuffer . new , validating_keys = Config . validating_keys? )
71+ buffer . put_int64 ( @integer )
72+ end
73+
74+ # Convert the integer to a BSON string key.
75+ #
76+ # @example Convert the integer to a BSON key string.
77+ # int.to_bson_key
78+ #
79+ # @param [ true, false ] validating_keys If BSON should validate the key.
80+ #
81+ # @return [ String ] The string key.
82+ #
83+ # @since 4.2.0
84+ def to_bson_key ( validating_keys = Config . validating_keys? )
85+ @integer . to_bson_key ( validating_keys )
86+ end
87+
88+ private
89+
90+ def out_of_range!
91+ raise RangeError . new ( "#{ self } is not a valid 8 byte integer value." )
92+ end
93+
4894 # Register this type when the module is loaded.
4995 #
5096 # @since 2.0.0
Original file line number Diff line number Diff line change 1616
1717describe BSON ::Int32 do
1818
19+ describe "#intiialize" do
20+
21+ let ( :obj ) { described_class . new ( integer ) }
22+
23+ context "when the integer is 32-bit" do
24+
25+ let ( :integer ) { Integer ::MAX_32BIT }
26+
27+ it "wraps the integer" do
28+ expect ( obj . instance_variable_get ( :@integer ) ) . to be ( integer )
29+ end
30+ end
31+
32+ context "when the integer is too large" do
33+
34+ let ( :integer ) { Integer ::MAX_32BIT + 1 }
35+
36+ it "raises an out of range error" do
37+ expect {
38+ obj
39+ } . to raise_error ( RangeError )
40+ end
41+ end
42+
43+ context "when the integer is too small" do
44+
45+ let ( :integer ) { Integer ::MIN_32BIT - 1 }
46+
47+ it "raises an out of range error" do
48+ expect {
49+ obj
50+ } . to raise_error ( RangeError )
51+ end
52+ end
53+ end
54+
1955 describe "#from_bson" do
2056
2157 let ( :type ) { 16 . chr }
4177 expect ( BSON ::Int32 . from_bson ( encoded_2 ) ) . to eq ( decoded_2 )
4278 end
4379 end
80+
81+ describe "#to_bson" do
82+
83+ context "when the integer is 32 bit" do
84+
85+ let ( :type ) { 16 . chr }
86+ let ( :obj ) { BSON ::Int32 . new ( Integer ::MAX_32BIT - 1 ) }
87+ let ( :bson ) { [ Integer ::MAX_32BIT - 1 ] . pack ( BSON ::Int32 ::PACK ) }
88+
89+ it_behaves_like "a serializable bson element"
90+ end
91+ end
92+
93+ describe "#to_bson_key" do
94+
95+ let ( :obj ) { BSON ::Int32 . new ( Integer ::MAX_32BIT - 1 ) }
96+ let ( :encoded ) { ( Integer ::MAX_32BIT - 1 ) . to_s }
97+
98+ it "returns the key as a string" do
99+ expect ( obj . to_bson_key ) . to eq ( encoded )
100+ end
101+ end
44102end
Original file line number Diff line number Diff line change 1616
1717describe BSON ::Int64 do
1818
19+ describe "#intiialize" do
20+
21+ let ( :obj ) { described_class . new ( integer ) }
22+
23+ context "when the integer is 64-bit" do
24+
25+ let ( :integer ) { Integer ::MAX_64BIT - 1 }
26+
27+ it "wraps the integer" do
28+ expect ( obj . instance_variable_get ( :@integer ) ) . to be ( integer )
29+ end
30+ end
31+
32+ context "when the integer is too large" do
33+
34+ let ( :integer ) { Integer ::MAX_64BIT + 1 }
35+
36+ it "raises an out of range error" do
37+ expect {
38+ obj
39+ } . to raise_error ( RangeError )
40+ end
41+ end
42+
43+ context "when the integer is too small" do
44+
45+ let ( :integer ) { Integer ::MIN_64BIT - 1 }
46+
47+ it "raises an out of range error" do
48+ expect {
49+ obj
50+ } . to raise_error ( RangeError )
51+ end
52+ end
53+ end
54+
1955 describe "#from_bson" do
2056
2157 let ( :type ) { 18 . chr }
2561 it_behaves_like "a bson element"
2662 it_behaves_like "a deserializable bson element"
2763 end
64+
65+ describe "#to_bson" do
66+
67+ context "when the integer is 64 bit" do
68+
69+ let ( :type ) { 18 . chr }
70+ let ( :obj ) { BSON ::Int64 . new ( Integer ::MAX_64BIT - 1 ) }
71+ let ( :bson ) { [ Integer ::MAX_64BIT - 1 ] . pack ( BSON ::Int64 ::PACK ) }
72+
73+ it_behaves_like "a serializable bson element"
74+ end
75+ end
76+
77+ describe "#to_bson_key" do
78+
79+ let ( :obj ) { BSON ::Int64 . new ( Integer ::MAX_64BIT - 1 ) }
80+ let ( :encoded ) { ( Integer ::MAX_64BIT - 1 ) . to_s }
81+
82+ it "returns the key as a string" do
83+ expect ( obj . to_bson_key ) . to eq ( encoded )
84+ end
85+ end
2886end
You can’t perform that action at this time.
0 commit comments