bson 4.0.0.beta → 4.0.0.rc0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 30f969161b2003e31eff164aeece7642d94d1569
4
- data.tar.gz: a6d728c087dcc9aed56d9a272ded92d1cdf661c6
3
+ metadata.gz: 50450bf61d6b631052b5a15fe98bcd5f572679d8
4
+ data.tar.gz: 079657ee59b117dcabc37e762642a90d5890f235
5
5
  SHA512:
6
- metadata.gz: 4b5ec98b56e79843b4351b2a001852c83b5f8ed360096fee11f8579a2d78acde5c57f0f77d05b6d894829ace638c6f754eb9b79640fd0059730c32522c132c34
7
- data.tar.gz: b4456f8ea0924747e9a253e63aaeb8972267964b249656603f765a8b0f61b6832ea3f12d1dd6ac0d0c2a6a5fbac03b98ea736e9d9cefc261ac364590fce60d50
6
+ metadata.gz: 5981ca13936db57eda138264169994bfa188cf0d79cb8808d1e7e1182dcf7de604017fa1b37e9c3179ed9154383b3ff2d0d449a72899e754b4e9b788235e0df9
7
+ data.tar.gz: 7142366162a5ac4648887be40a77285cff05f9e6c7198d47e36c37c298e26dba9c6b0ba11d429dea917460df304eeb88e5bbcc9f17443ae85ca31217c7f65f67
Binary file
data.tar.gz.sig CHANGED
Binary file
@@ -1,11 +1,18 @@
1
- // "License": Public Domain
2
- // I, Mathias PanzenbГ¶ck, place this file hereby into the public domain. Use it at your own risk for whatever you like.
3
- // In case there are jurisdictions that don't support putting things in the public domain you can also consider it to
4
- // be "dual licensed" under the BSD, MIT and Apache licenses, if you want to. This code is trivial anyway. Consider it
5
- // an example on how to get the endian conversion functions on different platforms.
6
-
7
- #ifndef PORTABLE_ENDIAN_H__
8
- #define PORTABLE_ENDIAN_H__
1
+ /*
2
+ * Copyright 2015 MongoDB, Inc.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
9
16
 
10
17
  #if (defined(_WIN16) || defined(_WIN32) || defined(_WIN64)) && !defined(__WINDOWS__)
11
18
 
@@ -16,105 +23,152 @@
16
23
  # include <sys/types.h>
17
24
  #endif
18
25
 
19
- #if defined(__linux__) || defined(__CYGWIN__)
20
-
21
- # include <endian.h>
22
-
23
- #elif defined(__APPLE__)
24
-
25
- # include <libkern/OSByteOrder.h>
26
-
27
- # define htobe16(x) OSSwapHostToBigInt16(x)
28
- # define htole16(x) OSSwapHostToLittleInt16(x)
29
- # define be16toh(x) OSSwapBigToHostInt16(x)
30
- # define le16toh(x) OSSwapLittleToHostInt16(x)
31
-
32
- # define htobe32(x) OSSwapHostToBigInt32(x)
33
- # define htole32(x) OSSwapHostToLittleInt32(x)
34
- # define be32toh(x) OSSwapBigToHostInt32(x)
35
- # define le32toh(x) OSSwapLittleToHostInt32(x)
36
-
37
- # define htobe64(x) OSSwapHostToBigInt64(x)
38
- # define htole64(x) OSSwapHostToLittleInt64(x)
39
- # define be64toh(x) OSSwapBigToHostInt64(x)
40
- # define le64toh(x) OSSwapLittleToHostInt64(x)
41
-
42
- # define __BYTE_ORDER BYTE_ORDER
43
- # define __BIG_ENDIAN BIG_ENDIAN
44
- # define __LITTLE_ENDIAN LITTLE_ENDIAN
45
- # define __PDP_ENDIAN PDP_ENDIAN
46
-
47
- #elif defined(__OpenBSD__)
48
-
49
- # include <sys/endian.h>
50
-
51
- #elif defined(__NetBSD__) || defined(__FreeBSD__) || defined(__DragonFly__)
26
+ #define BSON_BIG_ENDIAN 4321
27
+ #define BSON_LITTLE_ENDIAN 1234
52
28
 
53
- # include <sys/endian.h>
54
-
55
- # define be16toh(x) betoh16(x)
56
- # define le16toh(x) letoh16(x)
57
-
58
- # define be32toh(x) betoh32(x)
59
- # define le32toh(x) letoh32(x)
60
-
61
- # define be64toh(x) betoh64(x)
62
- # define le64toh(x) letoh64(x)
63
-
64
- #elif defined(__WINDOWS__)
65
-
66
- # include <sys/param.h>
29
+ #if defined(__sun)
30
+ # include <sys/byteorder.h>
31
+ # if defined(_LITTLE_ENDIAN)
32
+ # define BSON_BYTE_ORDER 1234
33
+ # else
34
+ # define BSON_BYTE_ORDER 4321
35
+ # endif
36
+ #endif
67
37
 
38
+ #ifndef BSON_BYTE_ORDER
68
39
  # if BYTE_ORDER == LITTLE_ENDIAN
69
-
70
- # define htobe16(x) htons(x)
71
- # define htole16(x) (x)
72
- # define be16toh(x) ntohs(x)
73
- # define le16toh(x) (x)
74
-
75
- # define htobe32(x) htonl(x)
76
- # define htole32(x) (x)
77
- # define be32toh(x) ntohl(x)
78
- # define le32toh(x) (x)
79
-
80
- # define htobe64(x) htonll(x)
81
- # define htole64(x) (x)
82
- # define be64toh(x) ntohll(x)
83
- # define le64toh(x) (x)
84
-
40
+ # define BSON_BYTE_ORDER 1234
85
41
  # elif BYTE_ORDER == BIG_ENDIAN
86
-
87
- /* that would be xbox 360 */
88
- # define htobe16(x) (x)
89
- # define htole16(x) __builtin_bswap16(x)
90
- # define be16toh(x) (x)
91
- # define le16toh(x) __builtin_bswap16(x)
92
-
93
- # define htobe32(x) (x)
94
- # define htole32(x) __builtin_bswap32(x)
95
- # define be32toh(x) (x)
96
- # define le32toh(x) __builtin_bswap32(x)
97
-
98
- # define htobe64(x) (x)
99
- # define htole64(x) __builtin_bswap64(x)
100
- # define be64toh(x) (x)
101
- # define le64toh(x) __builtin_bswap64(x)
102
-
103
- # else
104
-
105
- # error byte order not supported
106
-
42
+ # define BSON_BYTE_ORDER 4321
107
43
  # endif
44
+ #endif
108
45
 
109
- # define __BYTE_ORDER BYTE_ORDER
110
- # define __BIG_ENDIAN BIG_ENDIAN
111
- # define __LITTLE_ENDIAN LITTLE_ENDIAN
112
- # define __PDP_ENDIAN PDP_ENDIAN
113
-
114
- #else
46
+ #if defined(__sun)
47
+ # define BSON_UINT32_SWAP_LE_BE(v) __bson_uint32_swap_slow((uint32_t)v)
48
+ # define BSON_UINT64_SWAP_LE_BE(v) __bson_uint64_swap_slow((uint64_t)v)
49
+ #elif defined(__clang__) && defined(__clang_major__) && defined(__clang_minor__) && \
50
+ (__clang_major__ >= 3) && (__clang_minor__ >= 1)
51
+ # if __has_builtin(__builtin_bswap32)
52
+ # define BSON_UINT32_SWAP_LE_BE(v) __builtin_bswap32(v)
53
+ # endif
54
+ # if __has_builtin(__builtin_bswap64)
55
+ # define BSON_UINT64_SWAP_LE_BE(v) __builtin_bswap64(v)
56
+ # endif
57
+ #elif defined(__GNUC__) && (__GNUC__ >= 4)
58
+ # if __GNUC__ >= 4 && defined (__GNUC_MINOR__) && __GNUC_MINOR__ >= 3
59
+ # define BSON_UINT32_SWAP_LE_BE(v) __builtin_bswap32 ((uint32_t)v)
60
+ # define BSON_UINT64_SWAP_LE_BE(v) __builtin_bswap64 ((uint64_t)v)
61
+ # endif
62
+ #endif
115
63
 
116
- # error platform not supported
64
+ #ifndef BSON_UINT32_SWAP_LE_BE
65
+ # define BSON_UINT32_SWAP_LE_BE(v) __bson_uint32_swap_slow((uint32_t)v)
66
+ #endif
117
67
 
68
+ #ifndef BSON_UINT64_SWAP_LE_BE
69
+ # define BSON_UINT64_SWAP_LE_BE(v) __bson_uint64_swap_slow((uint64_t)v)
118
70
  #endif
119
71
 
72
+ #if BSON_BYTE_ORDER == BSON_LITTLE_ENDIAN
73
+ # define BSON_UINT32_FROM_LE(v) ((uint32_t)v)
74
+ # define BSON_UINT32_TO_LE(v) ((uint32_t)v)
75
+ # define BSON_UINT32_FROM_BE(v) BSON_UINT32_SWAP_LE_BE(v)
76
+ # define BSON_UINT32_TO_BE(v) BSON_UINT32_SWAP_LE_BE(v)
77
+ # define BSON_UINT64_FROM_LE(v) ((uint64_t)v)
78
+ # define BSON_UINT64_TO_LE(v) ((uint64_t)v)
79
+ # define BSON_UINT64_FROM_BE(v) BSON_UINT64_SWAP_LE_BE(v)
80
+ # define BSON_UINT64_TO_BE(v) BSON_UINT64_SWAP_LE_BE(v)
81
+ # define BSON_DOUBLE_FROM_LE(v) ((double)v)
82
+ # define BSON_DOUBLE_TO_LE(v) ((double)v)
83
+ #elif BSON_BYTE_ORDER == BSON_BIG_ENDIAN
84
+ # define BSON_UINT32_FROM_LE(v) BSON_UINT32_SWAP_LE_BE(v)
85
+ # define BSON_UINT32_TO_LE(v) BSON_UINT32_SWAP_LE_BE(v)
86
+ # define BSON_UINT32_FROM_BE(v) ((uint32_t)v)
87
+ # define BSON_UINT32_TO_BE(v) ((uint32_t)v)
88
+ # define BSON_UINT64_FROM_LE(v) BSON_UINT64_SWAP_LE_BE(v)
89
+ # define BSON_UINT64_TO_LE(v) BSON_UINT64_SWAP_LE_BE(v)
90
+ # define BSON_UINT64_FROM_BE(v) ((uint64_t)v)
91
+ # define BSON_UINT64_TO_BE(v) ((uint64_t)v)
92
+ # define BSON_DOUBLE_FROM_LE(v) (__bson_double_swap_slow(v))
93
+ # define BSON_DOUBLE_TO_LE(v) (__bson_double_swap_slow(v))
94
+ #else
95
+ # error "The endianness of target architecture is unknown."
120
96
  #endif
97
+
98
+ /*
99
+ *--------------------------------------------------------------------------
100
+ *
101
+ * __bson_uint32_swap_slow --
102
+ *
103
+ * Fallback endianness conversion for 32-bit integers.
104
+ *
105
+ * Returns:
106
+ * The endian swapped version.
107
+ *
108
+ * Side effects:
109
+ * None.
110
+ *
111
+ *--------------------------------------------------------------------------
112
+ */
113
+ static uint32_t __bson_uint32_swap_slow(uint32_t v)
114
+ {
115
+ return ((v & 0x000000FFU) << 24) |
116
+ ((v & 0x0000FF00U) << 8) |
117
+ ((v & 0x00FF0000U) >> 8) |
118
+ ((v & 0xFF000000U) >> 24);
119
+ }
120
+
121
+
122
+ /*
123
+ *--------------------------------------------------------------------------
124
+ *
125
+ * __bson_uint64_swap_slow --
126
+ *
127
+ * Fallback endianness conversion for 64-bit integers.
128
+ *
129
+ * Returns:
130
+ * The endian swapped version.
131
+ *
132
+ * Side effects:
133
+ * None.
134
+ *
135
+ *--------------------------------------------------------------------------
136
+ */
137
+ static uint64_t __bson_uint64_swap_slow(uint64_t v)
138
+ {
139
+ return ((v & 0x00000000000000FFULL) << 56) |
140
+ ((v & 0x000000000000FF00ULL) << 40) |
141
+ ((v & 0x0000000000FF0000ULL) << 24) |
142
+ ((v & 0x00000000FF000000ULL) << 8) |
143
+ ((v & 0x000000FF00000000ULL) >> 8) |
144
+ ((v & 0x0000FF0000000000ULL) >> 24) |
145
+ ((v & 0x00FF000000000000ULL) >> 40) |
146
+ ((v & 0xFF00000000000000ULL) >> 56);
147
+ }
148
+
149
+ /*
150
+ *--------------------------------------------------------------------------
151
+ *
152
+ * __bson_double_swap_slow --
153
+ *
154
+ * Fallback endianness conversion for double floating point.
155
+ *
156
+ * Returns:
157
+ * The endian swapped version.
158
+ *
159
+ * Side effects:
160
+ * None.
161
+ *
162
+ *--------------------------------------------------------------------------
163
+ */
164
+ static double __bson_double_swap_slow(double v)
165
+ {
166
+ uint64_t uv;
167
+
168
+ memcpy(&uv, &v, sizeof(v));
169
+ uv = BSON_UINT64_SWAP_LE_BE(uv);
170
+ memcpy(&v, &uv, sizeof(v));
171
+
172
+ return v;
173
+ }
174
+
@@ -203,7 +203,7 @@ VALUE rb_bson_byte_buffer_get_bytes(VALUE self, VALUE i)
203
203
  {
204
204
  byte_buffer_t *b;
205
205
  VALUE bytes;
206
- const long length = FIX2LONG(i);
206
+ const uint32_t length = FIX2LONG(i);
207
207
 
208
208
  TypedData_Get_Struct(self, byte_buffer_t, &rb_byte_buffer_data_type, b);
209
209
  ENSURE_BSON_READ(b, length);
@@ -235,13 +235,13 @@ VALUE rb_bson_byte_buffer_get_cstring(VALUE self)
235
235
  VALUE rb_bson_byte_buffer_get_double(VALUE self)
236
236
  {
237
237
  byte_buffer_t *b;
238
- union { uint64_t i64; double d; } ucast;
238
+ double d;
239
239
 
240
240
  TypedData_Get_Struct(self, byte_buffer_t, &rb_byte_buffer_data_type, b);
241
241
  ENSURE_BSON_READ(b, 8);
242
- ucast.i64 = le64toh(*(uint64_t*)READ_PTR(b));
242
+ memcpy(&d, READ_PTR(b), 8);
243
243
  b->read_position += 8;
244
- return DBL2NUM(ucast.d);
244
+ return DBL2NUM(BSON_DOUBLE_FROM_LE(d));
245
245
  }
246
246
 
247
247
  /**
@@ -254,9 +254,9 @@ VALUE rb_bson_byte_buffer_get_int32(VALUE self)
254
254
 
255
255
  TypedData_Get_Struct(self, byte_buffer_t, &rb_byte_buffer_data_type, b);
256
256
  ENSURE_BSON_READ(b, 4);
257
- i32 = le32toh(*((int32_t*)READ_PTR(b)));
257
+ memcpy(&i32, READ_PTR(b), 4);
258
258
  b->read_position += 4;
259
- return INT2NUM(i32);
259
+ return INT2NUM(BSON_UINT32_FROM_LE(i32));
260
260
  }
261
261
 
262
262
  /**
@@ -269,9 +269,9 @@ VALUE rb_bson_byte_buffer_get_int64(VALUE self)
269
269
 
270
270
  TypedData_Get_Struct(self, byte_buffer_t, &rb_byte_buffer_data_type, b);
271
271
  ENSURE_BSON_READ(b, 8);
272
- i64 = le64toh(*((int64_t*)READ_PTR(b)));
272
+ memcpy(&i64, READ_PTR(b), 8);
273
273
  b->read_position += 8;
274
- return LONG2NUM(i64);
274
+ return LL2NUM(BSON_UINT64_FROM_LE(i64));
275
275
  }
276
276
 
277
277
  /**
@@ -281,15 +281,17 @@ VALUE rb_bson_byte_buffer_get_string(VALUE self)
281
281
  {
282
282
  byte_buffer_t *b;
283
283
  int32_t length;
284
+ int32_t length_le;
284
285
  VALUE string;
285
286
 
286
287
  TypedData_Get_Struct(self, byte_buffer_t, &rb_byte_buffer_data_type, b);
287
288
  ENSURE_BSON_READ(b, 4);
288
- length = le32toh(*((int32_t*)READ_PTR(b)));
289
+ memcpy(&length, READ_PTR(b), 4);
290
+ length_le = BSON_UINT32_FROM_LE(length);
289
291
  b->read_position += 4;
290
- ENSURE_BSON_READ(b, length);
291
- string = rb_enc_str_new(READ_PTR(b), length - 1, rb_utf8_encoding());
292
- b->read_position += length;
292
+ ENSURE_BSON_READ(b, length_le);
293
+ string = rb_enc_str_new(READ_PTR(b), length_le - 1, rb_utf8_encoding());
294
+ b->read_position += length_le;
293
295
  return string;
294
296
  }
295
297
 
@@ -351,13 +353,10 @@ VALUE rb_bson_byte_buffer_put_cstring(VALUE self, VALUE string)
351
353
  VALUE rb_bson_byte_buffer_put_double(VALUE self, VALUE f)
352
354
  {
353
355
  byte_buffer_t *b;
354
- union {double d; uint64_t i64;} ucast;
355
-
356
- ucast.d = NUM2DBL(f);
356
+ const double d = BSON_DOUBLE_TO_LE(NUM2DBL(f));
357
357
  TypedData_Get_Struct(self, byte_buffer_t, &rb_byte_buffer_data_type, b);
358
358
  ENSURE_BSON_WRITE(b, 8);
359
- ucast.i64 = htole64(ucast.i64);
360
- *(int64_t*)WRITE_PTR(b) = ucast.i64;
359
+ memcpy(WRITE_PTR(b), (char*)&d, 8);
361
360
  b->write_position += 8;
362
361
 
363
362
  return self;
@@ -369,11 +368,11 @@ VALUE rb_bson_byte_buffer_put_double(VALUE self, VALUE f)
369
368
  VALUE rb_bson_byte_buffer_put_int32(VALUE self, VALUE i)
370
369
  {
371
370
  byte_buffer_t *b;
372
- const int32_t i32 = NUM2INT(i);
371
+ const int32_t i32 = BSON_UINT32_TO_LE(NUM2INT(i));
373
372
 
374
373
  TypedData_Get_Struct(self, byte_buffer_t, &rb_byte_buffer_data_type, b);
375
374
  ENSURE_BSON_WRITE(b, 4);
376
- *((int32_t*)WRITE_PTR(b)) = htole32(i32);
375
+ memcpy(WRITE_PTR(b), (char*)&i32, 4);
377
376
  b->write_position += 4;
378
377
 
379
378
  return self;
@@ -385,11 +384,11 @@ VALUE rb_bson_byte_buffer_put_int32(VALUE self, VALUE i)
385
384
  VALUE rb_bson_byte_buffer_put_int64(VALUE self, VALUE i)
386
385
  {
387
386
  byte_buffer_t *b;
388
- const int64_t i64 = NUM2LONG(i);
387
+ const int64_t i64 = BSON_UINT64_TO_LE(NUM2LL(i));
389
388
 
390
389
  TypedData_Get_Struct(self, byte_buffer_t, &rb_byte_buffer_data_type, b);
391
390
  ENSURE_BSON_WRITE(b, 8);
392
- *((int64_t*)WRITE_PTR(b)) = htole64(i64);
391
+ memcpy(WRITE_PTR(b), (char*)&i64, 8);
393
392
  b->write_position += 8;
394
393
 
395
394
  return self;
@@ -401,9 +400,11 @@ VALUE rb_bson_byte_buffer_put_int64(VALUE self, VALUE i)
401
400
  VALUE rb_bson_byte_buffer_put_string(VALUE self, VALUE string)
402
401
  {
403
402
  byte_buffer_t *b;
403
+ int32_t length_le;
404
404
 
405
405
  char *str = RSTRING_PTR(string);
406
- const size_t length = RSTRING_LEN(string) + 1;
406
+ const int32_t length = RSTRING_LEN(string) + 1;
407
+ length_le = BSON_UINT32_TO_LE(length);
407
408
 
408
409
  if (!rb_bson_utf8_validate(str, length - 1, true)) {
409
410
  rb_raise(rb_eArgError, "String %s is not valid UTF-8.", str);
@@ -411,7 +412,7 @@ VALUE rb_bson_byte_buffer_put_string(VALUE self, VALUE string)
411
412
 
412
413
  TypedData_Get_Struct(self, byte_buffer_t, &rb_byte_buffer_data_type, b);
413
414
  ENSURE_BSON_WRITE(b, length + 4);
414
- *((int32_t*)WRITE_PTR(b)) = htole32(length);
415
+ memcpy(WRITE_PTR(b), (char*)&length_le, 4);
415
416
  b->write_position += 4;
416
417
  memcpy(WRITE_PTR(b), str, length);
417
418
  b->write_position += length;
@@ -435,8 +436,8 @@ VALUE rb_bson_byte_buffer_read_position(VALUE self)
435
436
  VALUE rb_bson_byte_buffer_replace_int32(VALUE self, VALUE index, VALUE i)
436
437
  {
437
438
  byte_buffer_t *b;
438
- const int32_t position = NUM2INT(index);
439
- const int32_t i32 = htole32(NUM2INT(i));
439
+ const int32_t position = NUM2LONG(index);
440
+ const int32_t i32 = BSON_UINT32_TO_LE(NUM2LONG(i));
440
441
 
441
442
  TypedData_Get_Struct(self, byte_buffer_t, &rb_byte_buffer_data_type, b);
442
443
 
@@ -516,25 +517,25 @@ void rb_bson_expand_buffer(byte_buffer_t* buffer_ptr, size_t length)
516
517
  VALUE rb_bson_object_id_generator_next(int argc, VALUE* args, VALUE self)
517
518
  {
518
519
  char bytes[12];
519
- unsigned long t;
520
- unsigned long c;
520
+ uint32_t t;
521
+ uint32_t c;
521
522
  unsigned short pid = htons(getpid());
522
523
 
523
524
  if (argc == 0 || (argc == 1 && *args == Qnil)) {
524
525
  t = htonl((int) time(NULL));
525
526
  }
526
527
  else {
527
- t = htonl(NUM2UINT(rb_funcall(*args, rb_intern("to_i"), 0)));
528
+ t = htonl(NUM2ULONG(rb_funcall(*args, rb_intern("to_i"), 0)));
528
529
  }
529
530
 
530
531
  c = htonl(rb_bson_object_id_counter << 8);
531
532
 
532
- # if __BYTE_ORDER == __LITTLE_ENDIAN
533
+ # if BSON_BYTE_ORDER == BSON_LITTLE_ENDIAN
533
534
  memcpy(&bytes, &t, 4);
534
535
  memcpy(&bytes[4], rb_bson_machine_id_hash, 3);
535
536
  memcpy(&bytes[7], &pid, 2);
536
537
  memcpy(&bytes[9], (unsigned char*) &c, 3);
537
- #elif __BYTE_ORDER == __BIG_ENDIAN
538
+ #elif BSON_BYTE_ORDER == BSON_BIG_ENDIAN
538
539
  memcpy(&bytes, ((unsigned char*) &t) + 4, 4);
539
540
  memcpy(&bytes[4], rb_bson_machine_id_hash, 3);
540
541
  memcpy(&bytes[7], &pid, 2);
@@ -74,9 +74,11 @@ module BSON
74
74
  # @example Test if a key exists using a symbol
75
75
  # document.has_key?(:test)
76
76
  #
77
+ # @param [ Object ] key The key to check for.
78
+ #
77
79
  # @return [ true, false]
78
80
  #
79
- # @since 3.2.7
81
+ # @since 4.0.0
80
82
  def has_key?(key)
81
83
  super(convert_key(key))
82
84
  end
@@ -85,22 +87,41 @@ module BSON
85
87
  alias :key? :has_key?
86
88
  alias :member? :has_key?
87
89
 
88
-
89
90
  # Returns true if the given value is present in the document. Will normalize
90
91
  # symbols into strings.
91
92
  #
92
93
  # @example Test if a key exists using a symbol
93
94
  # document.has_value?(:test)
94
95
  #
96
+ # @param [ Object ] value THe value to check for.
97
+ #
95
98
  # @return [ true, false]
96
99
  #
97
- # @since 3.2.7
100
+ # @since 4.0.0
98
101
  def has_value?(value)
99
102
  super(convert_value(value))
100
103
  end
101
104
 
102
105
  alias :value :has_value?
103
106
 
107
+ # Deletes the key-value pair and returns the value from the document
108
+ # whose key is equal to key.
109
+ # If the key is not found, returns the default value. If the optional code
110
+ # block is given and the key is not found, pass in the key and return the
111
+ # result of block.
112
+ #
113
+ # @example Delete a key-value pair
114
+ # document.delete(:test)
115
+ #
116
+ # @param [ Object ] key The key of the key-value pair to delete.
117
+ #
118
+ # @return [ Object ]
119
+ #
120
+ # @since 4.0.0
121
+ def delete(key, &block)
122
+ super(convert_key(key), &block)
123
+ end
124
+
104
125
  # Instantiate a new Document. Valid parameters for instantiation is a hash
105
126
  # only or nothing.
106
127
  #
@@ -13,5 +13,5 @@
13
13
  # limitations under the License.
14
14
 
15
15
  module BSON
16
- VERSION = "4.0.0.beta"
16
+ VERSION = "4.0.0.rc0"
17
17
  end
@@ -98,33 +98,61 @@ describe BSON::Document do
98
98
 
99
99
  describe "#delete" do
100
100
 
101
- let(:key) { "white" }
102
- let(:val) { "ffffff" }
103
- let(:bad_key) { "black" }
101
+ shared_examples_for "a document with deletable pairs" do
104
102
 
105
- before do
106
- doc[key] = val
107
- end
103
+ let!(:deleted) { doc.delete(key) }
108
104
 
109
- let!(:deleted) { doc.delete(key) }
105
+ it "returns the deleted value" do
106
+ expect(deleted).to eq(val)
107
+ end
110
108
 
111
- it "returns the deleted value" do
112
- expect(deleted).to eq(val)
113
- end
109
+ it "removes the key from the list" do
110
+ expect(doc.keys.length).to eq(keys.length)
111
+ end
114
112
 
115
- it "removes the key from the list" do
116
- expect(doc.keys.length).to eq(keys.length)
113
+ it "matches the keys length to the document length" do
114
+ expect(doc.length).to eq(doc.keys.length)
115
+ end
116
+
117
+ context "when removing a bad key" do
118
+
119
+ it "returns nil" do
120
+ expect(doc.delete(bad_key)).to be_nil
121
+ end
122
+
123
+ context "when a block is provided" do
124
+
125
+ it "returns the result of the block" do
126
+ expect(doc.delete(bad_key) { |k| "golden key" }).to eq("golden key")
127
+ end
128
+ end
129
+ end
117
130
  end
118
131
 
119
- it "matches the keys length to the document length" do
120
- expect(doc.length).to eq(doc.keys.length)
132
+ context "when keys are strings" do
133
+
134
+ let(:key) { "white" }
135
+ let(:val) { "ffffff" }
136
+ let(:bad_key) { "black" }
137
+
138
+ before do
139
+ doc[key] = val
140
+ end
141
+
142
+ it_behaves_like "a document with deletable pairs"
121
143
  end
122
144
 
123
- context "when removind a bad key" do
145
+ context "when keys are symbols" do
146
+
147
+ let(:key) { :white }
148
+ let(:val) { "ffffff" }
149
+ let(:bad_key) { :black }
124
150
 
125
- it "returns nil" do
126
- expect(doc.delete(bad_key)).to be_nil
151
+ before do
152
+ doc[key] = val
127
153
  end
154
+
155
+ it_behaves_like "a document with deletable pairs"
128
156
  end
129
157
  end
130
158
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bson
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.0.0.beta
4
+ version: 4.0.0.rc0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tyler Brock
@@ -34,7 +34,7 @@ cert_chain:
34
34
  XZOS48LlWh15EG4yZo/gRzqNAW2LUIkYA5eMS2Kp6r+KV8IBUO/LaHdrXbdilpa8
35
35
  BRsuCo7UZDbFVRns04HLyjVvkj+K/ywIcdKdS0csz5M=
36
36
  -----END CERTIFICATE-----
37
- date: 2015-10-28 00:00:00.000000000 Z
37
+ date: 2015-11-10 00:00:00.000000000 Z
38
38
  dependencies: []
39
39
  description: A full featured BSON specification implementation, in Ruby
40
40
  email:
@@ -137,7 +137,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
137
137
  version: 1.3.6
138
138
  requirements: []
139
139
  rubyforge_project: bson
140
- rubygems_version: 2.4.6
140
+ rubygems_version: 2.4.5.1
141
141
  signing_key:
142
142
  specification_version: 4
143
143
  summary: Ruby Implementation of the BSON specification
metadata.gz.sig CHANGED
Binary file