class BSON::Timestamp
Represents a timestamp type, which is predominately used for sharding.
@see bsonspec.org/#/specification
@since 2.0.0
Constants
Attributes
@!attribute seconds
@return [ Integer ] The number of seconds. @since 2.0.0
@!attribute increment
@return [ Integer ] The incrementing value. @since 2.0.0
@!attribute seconds
@return [ Integer ] The number of seconds. @since 2.0.0
@!attribute increment
@return [ Integer ] The incrementing value. @since 2.0.0
Public Class Methods
Source
# File lib/bson/timestamp.rb, line 143 def self.from_bson(buffer, **options) increment = buffer.get_uint32 seconds = buffer.get_uint32 new(seconds, increment) end
Deserialize timestamp from BSON
.
@param [ ByteBuffer
] buffer The byte buffer.
@option options [ nil | :bson ] :mode Decoding mode to use.
@return [ Timestamp
] The decoded timestamp.
@see bsonspec.org/#/specification
@since 2.0.0
Source
# File lib/bson/timestamp.rb, line 113 def initialize(seconds, increment) @seconds, @increment = seconds, increment end
Instantiate the new timestamp.
@example Instantiate the timestamp.
BSON::Timestamp.new(5, 30)
@param [ Integer
] seconds The number of seconds. @param [ Integer
] increment The increment value.
@since 2.0.0
Public Instance Methods
Source
# File lib/bson/timestamp.rb, line 72 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
Determine if this timestamp is greater or less than another object.
@example Compare the timestamp.
timestamp < other
@param [ Object
] other The object to compare against.
@return [ true, false ] The result of the comparison.
@since 4.3.0
Source
# File lib/bson/timestamp.rb, line 57 def ==(other) return false unless other.is_a?(Timestamp) seconds == other.seconds && increment == other.increment end
Determine if this timestamp is equal to another object.
@example Check the timestamp equality.
timestamp == other
@param [ Object
] other The object to compare against.
@return [ true, false ] If the objects are equal.
@since 2.0.0
Source
# File lib/bson/timestamp.rb, line 100 def as_extended_json(**options) { "$timestamp" => { "t" => seconds, "i" => increment } } end
Converts this object to a representation directly serializable to Extended JSON
(github.com/mongodb/specifications/blob/master/source/extended-json.rst).
@option opts [ nil | :relaxed | :legacy ] :mode Serialization mode
(default is canonical extended JSON)
@return [ Hash
] The extended json representation.
Source
# File lib/bson/timestamp.rb, line 89 def as_json(*args) as_extended_json end
Get the timestamp as JSON
hash data.
@example Get the timestamp as a JSON
hash.
timestamp.as_json
@return [ Hash
] The timestamp as a JSON
hash.
@since 2.0.0 @deprecated Use as_extended_json
instead.
Source
# File lib/bson/timestamp.rb, line 127 def to_bson(buffer = ByteBuffer.new, validating_keys = Config.validating_keys?) buffer.put_uint32(increment) buffer.put_uint32(seconds) end
Get the timestamp as its encoded raw BSON
bytes.
@example Get the timestamp as BSON
.
timestamp.to_bson
@return [ BSON::ByteBuffer
] The buffer with the encoded object.
@see bsonspec.org/#/specification
@since 2.0.0