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.
17.chr.force_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'.freeze
Instance Attribute Summary collapse
- #increment ⇒ Object
-
#seconds ⇒ Integer
The number of seconds.
Class Method Summary collapse
-
.from_bson(buffer) ⇒ 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_json(*args) ⇒ Hash
Get the timestamp as JSON hash data.
-
#initialize(seconds, increment) ⇒ Timestamp
constructor
Instantiate the new timestamp.
-
#to_bson(buffer = ByteBuffer.new, validating_keys = Config.validating_keys?) ⇒ 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.
100 101 102 |
# File 'lib/bson/timestamp.rb', line 100 def initialize(seconds, increment) @seconds, @increment = seconds, increment end |
Instance Attribute Details
#seconds ⇒ Integer
Returns The number of seconds.
44 45 46 |
# File 'lib/bson/timestamp.rb', line 44 def seconds @seconds end |
Class Method Details
.from_bson(buffer) ⇒ Timestamp
Deserialize timestamp from BSON.
128 129 130 131 132 |
# File 'lib/bson/timestamp.rb', line 128 def self.from_bson(buffer) increment = buffer.get_int32 seconds = buffer.get_int32 new(seconds, increment) end |
Instance Method Details
#<=>(other) ⇒ true, false
Determine if this timestamp is greater or less than another object.
71 72 73 74 75 76 77 |
# File 'lib/bson/timestamp.rb', line 71 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.
56 57 58 59 |
# File 'lib/bson/timestamp.rb', line 56 def ==(other) return false unless other.is_a?(Timestamp) seconds == other.seconds && increment == other.increment end |
#as_json(*args) ⇒ Hash
Get the timestamp as JSON hash data.
87 88 89 |
# File 'lib/bson/timestamp.rb', line 87 def as_json(*args) { "$timestamp" => { "t" => seconds, "i" => increment } } end |
#to_bson(buffer = ByteBuffer.new, validating_keys = Config.validating_keys?) ⇒ BSON::ByteBuffer
Get the timestamp as its encoded raw BSON bytes.
114 115 116 117 |
# File 'lib/bson/timestamp.rb', line 114 def to_bson(buffer = ByteBuffer.new, validating_keys = Config.validating_keys?) buffer.put_int32(increment) buffer.put_int32(seconds) end |