Class: BSON::Timestamp
- Inherits:
-
Object
- Object
- BSON::Timestamp
- Includes:
- JSON, Comparable
- Defined in:
- lib/bson/timestamp.rb
Overview
Represents a timestamp type, which is predominately used for sharding.
Constant Summary collapse
- BSON_TYPE =
A timestamp is type 0x11 in the BSON spec.
::String.new(17.chr, encoding: BINARY).freeze
- COMPARISON_ERROR_MESSAGE =
Error message if an object other than a Timestamp is compared with this object.
'comparison of %s with Timestamp failed'
Instance Attribute Summary collapse
- #increment ⇒ Object
-
#seconds ⇒ Integer
The number of seconds.
Class Method Summary collapse
-
.from_bson(buffer, **options) ⇒ Timestamp
Deserialize timestamp from BSON.
Instance Method Summary collapse
-
#<=>(other) ⇒ true, false
Determine if this timestamp is greater or less than another object.
-
#==(other) ⇒ true, false
Determine if this timestamp is equal to another object.
-
#as_extended_json(**options) ⇒ Hash
Converts this object to a representation directly serializable to Extended JSON (github.com/mongodb/specifications/blob/master/source/extended-json/extended-json.md).
-
#as_json(*args) ⇒ Hash
deprecated
Deprecated.
Use as_extended_json instead.
-
#initialize(seconds, increment) ⇒ Timestamp
constructor
Instantiate the new timestamp.
-
#to_bson(buffer = ByteBuffer.new) ⇒ BSON::ByteBuffer
Get the timestamp as its encoded raw BSON bytes.
Methods included from JSON
Constructor Details
#initialize(seconds, increment) ⇒ Timestamp
Instantiate the new timestamp.
114 115 116 |
# File 'lib/bson/timestamp.rb', line 114 def initialize(seconds, increment) @seconds, @increment = seconds, increment end |
Instance Attribute Details
#seconds ⇒ Integer
Returns The number of seconds.
46 47 48 |
# File 'lib/bson/timestamp.rb', line 46 def seconds @seconds end |
Class Method Details
.from_bson(buffer, **options) ⇒ Timestamp
Deserialize timestamp from BSON.
144 145 146 147 148 |
# File 'lib/bson/timestamp.rb', line 144 def self.from_bson(buffer, **) increment = buffer.get_uint32 seconds = buffer.get_uint32 new(seconds, increment) end |
Instance Method Details
#<=>(other) ⇒ true, false
Determine if this timestamp is greater or less than another object.
73 74 75 76 77 78 79 |
# File 'lib/bson/timestamp.rb', line 73 def <=>(other) raise ArgumentError.new(COMPARISON_ERROR_MESSAGE % other.class) unless other.is_a?(Timestamp) return 0 if self == other a = [ seconds, increment ] b = [ other.seconds, other.increment ] [ a, b ].sort[0] == a ? -1 : 1 end |
#==(other) ⇒ true, false
Determine if this timestamp is equal to another object.
58 59 60 61 |
# File 'lib/bson/timestamp.rb', line 58 def ==(other) return false unless other.is_a?(Timestamp) seconds == other.seconds && increment == other.increment end |
#as_extended_json(**options) ⇒ Hash
Converts this object to a representation directly serializable to Extended JSON (github.com/mongodb/specifications/blob/master/source/extended-json/extended-json.md).
101 102 103 |
# File 'lib/bson/timestamp.rb', line 101 def as_extended_json(**) { "$timestamp" => { "t" => seconds, "i" => increment } } end |
#as_json(*args) ⇒ Hash
Use as_extended_json instead.
Get the timestamp as JSON hash data.
90 91 92 |
# File 'lib/bson/timestamp.rb', line 90 def as_json(*args) as_extended_json end |
#to_bson(buffer = ByteBuffer.new) ⇒ BSON::ByteBuffer
Get the timestamp as its encoded raw BSON bytes.
128 129 130 131 |
# File 'lib/bson/timestamp.rb', line 128 def to_bson(buffer = ByteBuffer.new) buffer.put_uint32(increment) buffer.put_uint32(seconds) end |