moped 1.3.2 → 1.4.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of moped might be problematic. Click here for more details.

@@ -1,9 +1,11 @@
1
1
  module Moped
2
2
  module BSON
3
- # @private
4
3
  module Extensions
4
+
5
5
  module Boolean
6
+
6
7
  module ClassMethods
8
+
7
9
  def __bson_load__(io)
8
10
  io.readbyte == 1
9
11
  end
@@ -1,8 +1,9 @@
1
1
  module Moped
2
2
  module BSON
3
- # @private
4
3
  module Extensions
4
+
5
5
  module FalseClass
6
+
6
7
  def __bson_dump__(io, key)
7
8
  io << Types::BOOLEAN
8
9
  io << key.to_bson_cstring
@@ -1,14 +1,8 @@
1
1
  module Moped
2
2
  module BSON
3
- # @private
4
3
  module Extensions
5
- module Float
6
4
 
7
- module ClassMethods
8
- def __bson_load__(io)
9
- io.read(8).unpack(FLOAT_PACK)[0]
10
- end
11
- end
5
+ module Float
12
6
 
13
7
  def __bson_dump__(io, key)
14
8
  io << Types::FLOAT
@@ -16,6 +10,12 @@ module Moped
16
10
  io << [self].pack(FLOAT_PACK)
17
11
  end
18
12
 
13
+ module ClassMethods
14
+
15
+ def __bson_load__(io)
16
+ io.read(8).unpack(FLOAT_PACK)[0]
17
+ end
18
+ end
19
19
  end
20
20
  end
21
21
  end
@@ -1,47 +1,38 @@
1
1
  module Moped
2
2
  module BSON
3
- # @private
4
3
  module Extensions
5
- module Hash
6
4
 
7
- module ClassMethods
8
- def __bson_load__(io, doc = new)
9
- # Swallow the first four (length) bytes
10
- io.read 4
11
-
12
- while (buf = io.readbyte) != 0
13
- key = io.gets(NULL_BYTE).from_utf8_binary.chop!
14
-
15
- if native_class = Types::MAP[buf]
16
- doc[key] = native_class.__bson_load__(io)
17
- end
18
- end
19
-
20
- doc
21
- end
22
- end
5
+ module Hash
23
6
 
24
7
  def __bson_dump__(io = "", key = nil)
25
8
  if key
26
9
  io << Types::HASH
27
10
  io << key.to_bson_cstring
28
11
  end
29
-
30
12
  start = io.bytesize
31
-
32
- # write dummy length
33
- io << START_LENGTH
34
-
13
+ io << START_LENGTH # write dummy length
35
14
  each do |k, v|
36
15
  v.__bson_dump__(io, k.to_s)
37
16
  end
38
17
  io << EOD
39
-
40
18
  stop = io.bytesize
41
19
  io[start, 4] = [stop - start].pack INT32_PACK
42
-
43
20
  io
44
21
  end
22
+
23
+ module ClassMethods
24
+
25
+ def __bson_load__(io, doc = new)
26
+ io.read(4) # Swallow the first four (length) bytes
27
+ while (buf = io.readbyte) != 0
28
+ key = io.gets(NULL_BYTE).from_utf8_binary.chop!
29
+ if native_class = Types::MAP[buf]
30
+ doc[key] = native_class.__bson_load__(io)
31
+ end
32
+ end
33
+ doc
34
+ end
35
+ end
45
36
  end
46
37
  end
47
38
  end
@@ -1,20 +1,15 @@
1
1
  module Moped
2
2
  module BSON
3
- # @private
4
3
  module Extensions
4
+
5
5
  module Integer
6
+
6
7
  INT32_MIN = (-(1 << 31)+1)
7
8
  INT32_MAX = ((1<<31)-1)
8
9
 
9
10
  INT64_MIN = (-2**64 / 2)
10
11
  INT64_MAX = (2**64 / 2 - 1)
11
12
 
12
- module ClassMethods
13
- def __bson_load__(io, bignum=false)
14
- io.read(4).unpack(INT32_PACK)[0]
15
- end
16
- end
17
-
18
13
  def __bson_dump__(io, key)
19
14
  if self >= INT32_MIN && self <= INT32_MAX
20
15
  io << Types::INT32
@@ -29,6 +24,12 @@ module Moped
29
24
  end
30
25
  end
31
26
 
27
+ module ClassMethods
28
+
29
+ def __bson_load__(io, bignum=false)
30
+ io.read(4).unpack(INT32_PACK)[0]
31
+ end
32
+ end
32
33
  end
33
34
  end
34
35
  end
@@ -1,18 +1,18 @@
1
1
  module Moped
2
2
  module BSON
3
- # @private
4
3
  module Extensions
4
+
5
5
  module NilClass
6
- module ClassMethods
7
- def __bson_load__(io)
8
- nil
9
- end
10
- end
11
6
 
12
7
  def __bson_dump__(io, key)
13
8
  io << Types::NULL
14
9
  io << key.to_bson_cstring
15
10
  end
11
+
12
+ module ClassMethods
13
+
14
+ def __bson_load__(io); nil; end
15
+ end
16
16
  end
17
17
  end
18
18
  end
@@ -1,12 +1,10 @@
1
1
  module Moped
2
2
  module BSON
3
- # @private
4
3
  module Extensions
4
+
5
5
  module Object
6
6
 
7
- def __safe_options__
8
- self
9
- end
7
+ def __safe_options__; self; end
10
8
  end
11
9
  end
12
10
  end
@@ -1,9 +1,21 @@
1
1
  module Moped
2
2
  module BSON
3
- # @private
4
3
  module Extensions
4
+
5
5
  module Regexp
6
+
7
+ def __bson_dump__(io, key)
8
+ io << Types::REGEX
9
+ io << key.to_bson_cstring
10
+ io << source.to_bson_cstring
11
+ io << 'i' if (options & ::Regexp::IGNORECASE) != 0
12
+ io << 'ms' if (options & ::Regexp::MULTILINE) != 0
13
+ io << 'x' if (options & ::Regexp::EXTENDED) != 0
14
+ io << NULL_BYTE
15
+ end
16
+
6
17
  module ClassMethods
18
+
7
19
  def __bson_load__(io)
8
20
  source = io.gets(NULL_BYTE).from_utf8_binary.chop!
9
21
  options = 0
@@ -17,22 +29,9 @@ module Moped
17
29
  options |= ::Regexp::EXTENDED
18
30
  end
19
31
  end
20
-
21
32
  new(source, options)
22
33
  end
23
34
  end
24
-
25
- def __bson_dump__(io, key)
26
- io << Types::REGEX
27
- io << key.to_bson_cstring
28
-
29
- io << source.to_bson_cstring
30
-
31
- io << 'i' if (options & ::Regexp::IGNORECASE) != 0
32
- io << 'ms' if (options & ::Regexp::MULTILINE) != 0
33
- io << 'x' if (options & ::Regexp::EXTENDED) != 0
34
- io << NULL_BYTE
35
- end
36
35
  end
37
36
  end
38
37
  end
@@ -1,20 +1,13 @@
1
1
  module Moped
2
2
  module BSON
3
- # @private
4
3
  module Extensions
4
+
5
5
  module String
6
- module ClassMethods
7
- def __bson_load__(io)
8
- io.read(*io.read(4).unpack(INT32_PACK)).from_utf8_binary.chop!
9
- end
10
- end
11
6
 
12
7
  def __bson_dump__(io, key)
13
8
  io << Types::STRING
14
9
  io << key.to_bson_cstring
15
-
16
10
  data = to_utf8_binary
17
-
18
11
  io << [ data.bytesize + 1 ].pack(INT32_PACK)
19
12
  io << data
20
13
  io << NULL_BYTE
@@ -25,7 +18,6 @@ module Moped
25
18
  raise EncodingError, "#{inspect} cannot be converted to a BSON " \
26
19
  "cstring because it contains a null byte"
27
20
  end
28
-
29
21
  to_utf8_binary << NULL_BYTE
30
22
  end
31
23
 
@@ -40,6 +32,13 @@ module Moped
40
32
  def from_utf8_binary
41
33
  force_encoding(UTF8_ENCODING).encode!
42
34
  end
35
+
36
+ module ClassMethods
37
+
38
+ def __bson_load__(io)
39
+ io.read(*io.read(4).unpack(INT32_PACK)).from_utf8_binary.chop!
40
+ end
41
+ end
43
42
  end
44
43
  end
45
44
  end
@@ -1,20 +1,13 @@
1
1
  module Moped
2
2
  module BSON
3
- # @private
4
3
  module Extensions
4
+
5
5
  module Symbol
6
- module ClassMethods
7
- def __bson_load__(io)
8
- io.read(*io.read(4).unpack(INT32_PACK)).from_utf8_binary.chop!.intern
9
- end
10
- end
11
6
 
12
7
  def __bson_dump__(io, key)
13
8
  io << Types::SYMBOL
14
9
  io << key.to_bson_cstring
15
-
16
10
  data = to_utf8_binary
17
-
18
11
  io << [ data.bytesize + 1 ].pack(INT32_PACK)
19
12
  io << data
20
13
  io << NULL_BYTE
@@ -27,6 +20,13 @@ module Moped
27
20
  def to_utf8_binary
28
21
  to_s.to_utf8_binary
29
22
  end
23
+
24
+ module ClassMethods
25
+
26
+ def __bson_load__(io)
27
+ io.read(*io.read(4).unpack(INT32_PACK)).from_utf8_binary.chop!.intern
28
+ end
29
+ end
30
30
  end
31
31
  end
32
32
  end
@@ -1,20 +1,22 @@
1
1
  module Moped
2
2
  module BSON
3
- # @private
4
3
  module Extensions
4
+
5
5
  module Time
6
- module ClassMethods
7
- def __bson_load__(io)
8
- seconds, fragment = io.read(8).unpack(INT64_PACK)[0].divmod 1000
9
- at(seconds, fragment * 1000).utc
10
- end
11
- end
12
6
 
13
7
  def __bson_dump__(io, key)
14
8
  io << Types::TIME
15
9
  io << key.to_bson_cstring
16
10
  io << [(to_i * 1000) + (usec / 1000)].pack(INT64_PACK)
17
11
  end
12
+
13
+ module ClassMethods
14
+
15
+ def __bson_load__(io)
16
+ seconds, fragment = io.read(8).unpack(INT64_PACK)[0].divmod 1000
17
+ at(seconds, fragment * 1000).utc
18
+ end
19
+ end
18
20
  end
19
21
  end
20
22
  end
@@ -1,8 +1,9 @@
1
1
  module Moped
2
2
  module BSON
3
- # @private
4
3
  module Extensions
4
+
5
5
  module TrueClass
6
+
6
7
  def __bson_dump__(io, key)
7
8
  io << Types::BOOLEAN
8
9
  io << key.to_bson_cstring
@@ -1,15 +1,46 @@
1
1
  module Moped
2
2
  module BSON
3
+
4
+ # Represents the maximum key value in the database.
3
5
  class MaxKey
6
+
4
7
  class << self
8
+
9
+ # Check equality on the object.
10
+ #
11
+ # @example Check equality.
12
+ # object == other
13
+ #
14
+ # @param [ Object ] other The object to check against.
15
+ #
16
+ # @return [ true, false ] If the objects are equal.
17
+ #
18
+ # @since 1.0.0
5
19
  def ===(other)
6
20
  other == self
7
21
  end
8
22
 
9
- def __bson_load__(io)
10
- self
11
- end
23
+ # Load the max key from the raw data.
24
+ #
25
+ # @example Load the max key.
26
+ # Moped::BSON::MaxKey.__bson_load("")
27
+ #
28
+ # @param [ String ] io The raw bytes.
29
+ #
30
+ # @return [ Class ] The Moped::BSON::MaxKey class.
31
+ #
32
+ # @since 1.0.0
33
+ def __bson_load__(io); self; end
12
34
 
35
+ # Dump the max key to the raw bytes.
36
+ #
37
+ # @example Dump the max key.
38
+ # Moped::BSON::MaxKey.__bson_dump__("", "max")
39
+ #
40
+ # @param [ String ] io The raw bytes to write to.
41
+ # @param [ String ] key The field name.
42
+ #
43
+ # @since 1.0.0
13
44
  def __bson_dump__(io, key)
14
45
  io << Types::MAX_KEY
15
46
  io << key.to_bson_cstring
@@ -1,15 +1,46 @@
1
1
  module Moped
2
2
  module BSON
3
+
4
+ # Represents the minimum key value in the database.
3
5
  class MinKey
6
+
4
7
  class << self
8
+
9
+ # Check equality on the object.
10
+ #
11
+ # @example Check equality.
12
+ # object == other
13
+ #
14
+ # @param [ Object ] other The object to check against.
15
+ #
16
+ # @return [ true, false ] If the objects are equal.
17
+ #
18
+ # @since 1.0.0
5
19
  def ===(other)
6
20
  other == self
7
21
  end
8
22
 
9
- def __bson_load__(io)
10
- self
11
- end
23
+ # Load the min key from the raw data.
24
+ #
25
+ # @example Load the min key.
26
+ # Moped::BSON::MinKey.__bson_load("")
27
+ #
28
+ # @param [ String ] io The raw bytes.
29
+ #
30
+ # @return [ Class ] The Moped::BSON::MinKey class.
31
+ #
32
+ # @since 1.0.0
33
+ def __bson_load__(io); self; end
12
34
 
35
+ # Dump the min key to the raw bytes.
36
+ #
37
+ # @example Dump the min key.
38
+ # Moped::BSON::MinKey.__bson_dump__("", "min")
39
+ #
40
+ # @param [ String ] io The raw bytes to write to.
41
+ # @param [ String ] key The field name.
42
+ #
43
+ # @since 1.0.0
13
44
  def __bson_dump__(io, key)
14
45
  io << Types::MIN_KEY
15
46
  io << key.to_bson_cstring