Class: Moped::BSON::ObjectId
- Includes:
- Comparable
- Defined in:
- lib/moped/bson/object_id.rb
Defined Under Namespace
Classes: Generator
Class Method Summary collapse
- .__bson_load__(io) ⇒ Object
-
.from_data(data) ⇒ ObjectId
Create a new object id from some raw data.
-
.from_string(string) ⇒ ObjectId
Create a new object id from a string.
-
.from_time(time, options = nil) ⇒ ObjectId
Create a new object id from a time.
-
.legal?(string) ⇒ true, false
Determine if the string is a legal object id.
Instance Method Summary collapse
-
#<=>(other) ⇒ Integer
Compare this object with another object, used in sorting.
-
#==(other) ⇒ true, false
(also: #eql?)
Check equality on the object.
-
#===(other) ⇒ true, false
Check equality on the object.
-
#__bson_dump__(io, key) ⇒ Object
Serialize the object id to its raw bytes.
-
#data ⇒ String
Get the raw data (bytes) for the object id.
-
#generation_time ⇒ Time
Return the UTC time at which this ObjectId was generated.
-
#hash ⇒ Fixnum
Gets the hash code for the object.
-
#inspect ⇒ String
Gets the string inspection for the object.
-
#marshal_dump ⇒ String
Dump the object for use in a marshal dump.
-
#marshal_load(data) ⇒ Object
Load the object from the marshal dump.
-
#to_json(*args) ⇒ String
Convert the object to a JSON string.
-
#to_s ⇒ String
(also: #to_str)
Get the string representation of the object.
Class Method Details
.__bson_load__(io) ⇒ Object
195 196 197 |
# File 'lib/moped/bson/object_id.rb', line 195 def __bson_load__(io) from_data(io.read(12)) end |
.from_data(data) ⇒ ObjectId
Create a new object id from some raw data.
260 261 262 263 264 |
# File 'lib/moped/bson/object_id.rb', line 260 def from_data(data) id = allocate id.send(:data=, data) id end |
.from_string(string) ⇒ ObjectId
Create a new object id from a string.
209 210 211 212 |
# File 'lib/moped/bson/object_id.rb', line 209 def from_string(string) raise Errors::InvalidObjectId.new(string) unless legal?(string) from_data [string].pack("H*") end |
.from_time(time, options = nil) ⇒ ObjectId
Create a new object id from a time.
231 232 233 234 |
# File 'lib/moped/bson/object_id.rb', line 231 def from_time(time, = nil) unique = ( || {})[:unique] from_data(unique ? @@generator.next(time.to_i) : [ time.to_i ].pack("Nx8")) end |
.legal?(string) ⇒ true, false
Determine if the string is a legal object id.
246 247 248 |
# File 'lib/moped/bson/object_id.rb', line 246 def legal?(string) /\A\h{24}\Z/ === string.to_s end |
Instance Method Details
#<=>(other) ⇒ Integer
Compare this object with another object, used in sorting.
64 65 66 |
# File 'lib/moped/bson/object_id.rb', line 64 def <=>(other) data <=> other.data end |
#==(other) ⇒ true, false Also known as: eql?
Check equality on the object.
49 50 51 |
# File 'lib/moped/bson/object_id.rb', line 49 def ==(other) BSON::ObjectId === other && data == other.data end |
#===(other) ⇒ true, false
Check equality on the object.
34 35 36 37 |
# File 'lib/moped/bson/object_id.rb', line 34 def ===(other) return to_str === other.to_str if other.respond_to?(:to_str) super end |
#__bson_dump__(io, key) ⇒ Object
Serialize the object id to its raw bytes.
18 19 20 21 22 |
# File 'lib/moped/bson/object_id.rb', line 18 def __bson_dump__(io, key) io << Types::OBJECT_ID io << key.to_bson_cstring io << data end |
#data ⇒ String
Get the raw data (bytes) for the object id.
76 77 78 79 80 81 |
# File 'lib/moped/bson/object_id.rb', line 76 def data # If @data is defined, then we know we've been loaded in some # non-standard way, so we attempt to repair the data. repair! @data if defined? @data @raw_data ||= @@generator.next end |
#generation_time ⇒ Time
Return the UTC time at which this ObjectId was generated. This may be used instread of a created_at timestamp since this information is always encoded in the object id.
93 94 95 |
# File 'lib/moped/bson/object_id.rb', line 93 def generation_time Time.at(data.unpack("N")[0]).utc end |
#hash ⇒ Fixnum
Gets the hash code for the object.
105 106 107 |
# File 'lib/moped/bson/object_id.rb', line 105 def hash data.hash end |
#inspect ⇒ String
Gets the string inspection for the object.
117 118 119 |
# File 'lib/moped/bson/object_id.rb', line 117 def inspect to_s.inspect end |
#marshal_dump ⇒ String
Dump the object for use in a marshal dump.
129 130 131 |
# File 'lib/moped/bson/object_id.rb', line 129 def marshal_dump data end |
#marshal_load(data) ⇒ Object
Load the object from the marshal dump.
141 142 143 |
# File 'lib/moped/bson/object_id.rb', line 141 def marshal_load(data) self.data = data end |
#to_json(*args) ⇒ String
Convert the object to a JSON string.
153 154 155 |
# File 'lib/moped/bson/object_id.rb', line 153 def to_json(*args) "{\"$oid\": \"#{to_s}\"}" end |
#to_s ⇒ String Also known as: to_str
Get the string representation of the object.
165 166 167 |
# File 'lib/moped/bson/object_id.rb', line 165 def to_s data.unpack("H*")[0].force_encoding(Moped::BSON::UTF8_ENCODING) end |