Class: BSON::ObjectId
- Inherits:
-
Object
- Object
- BSON::ObjectId
- Defined in:
- lib/bson/types/object_id.rb
Overview
Generates MongoDB object ids.
Instance Attribute Summary collapse
-
#data ⇒ Object
Returns the value of attribute data.
Class Method Summary collapse
-
.create_pk(doc) ⇒ BSON::ObjectId, Object
Adds a primary key to the given document if needed.
-
.from_string(str) ⇒ BSON::ObjectId
Given a string representation of an ObjectId, return a new ObjectId with that value.
-
.from_time(time, opts = {}) ⇒ BSON::ObjectId
Create an object id from the given time.
-
.legal?(str) ⇒ Boolean
Determine if the supplied string is legal.
- .machine_id ⇒ Object
Instance Method Summary collapse
-
#as_json(options = {}) ⇒ Hash
Create the JSON hash structure convert to MongoDB extended format.
-
#eql?(object_id) ⇒ Boolean
(also: #==)
Check equality of this object id with another.
-
#generation_time ⇒ Time
Return the UTC time at which this ObjectId was generated.
-
#hash ⇒ Integer
Get a unique hashcode for this object.
-
#initialize(data = nil, time = nil) ⇒ ObjectId
constructor
Create a new object id.
- #inspect ⇒ Object
-
#to_a ⇒ Array
Get an array representation of the object id.
-
#to_json(*a) ⇒ String
Convert to MongoDB extended JSON format.
-
#to_s ⇒ String
Get a string representation of this object id.
Constructor Details
#initialize(data = nil, time = nil) ⇒ ObjectId
Create a new object id. If no parameter is given, an id corresponding to the ObjectId BSON data type will be created. This is a 12-byte value consisting of a 4-byte timestamp, a 3-byte machine id, a 2-byte process id, and a 3-byte counter.
29 30 31 |
# File 'lib/bson/types/object_id.rb', line 29 def initialize(data=nil, time=nil) @data = data || generate(time) end |
Instance Attribute Details
#data ⇒ Object
Returns the value of attribute data.
15 16 17 |
# File 'lib/bson/types/object_id.rb', line 15 def data @data end |
Class Method Details
.create_pk(doc) ⇒ BSON::ObjectId, Object
Adds a primary key to the given document if needed.
74 75 76 |
# File 'lib/bson/types/object_id.rb', line 74 def self.create_pk(doc) doc.has_key?(:_id) || doc.has_key?('_id') ? doc : doc.merge!(:_id => self.new) end |
.from_string(str) ⇒ BSON::ObjectId
Given a string representation of an ObjectId, return a new ObjectId with that value.
107 108 109 110 111 112 113 114 |
# File 'lib/bson/types/object_id.rb', line 107 def self.from_string(str) raise InvalidObjectId, "illegal ObjectId format: #{str}" unless legal?(str) data = [] 12.times do |i| data[i] = str[i * 2, 2].to_i(16) end self.new(data) end |
.from_time(time, opts = {}) ⇒ BSON::ObjectId
Create an object id from the given time. This is useful for doing range queries; it works because MongoDB’s object ids begin with a timestamp.
59 60 61 62 63 64 65 66 |
# File 'lib/bson/types/object_id.rb', line 59 def self.from_time(time, opts={}) unique = opts.fetch(:unique, false) if unique self.new(nil, time) else self.new([time.to_i,0,0].pack("NNN").unpack("C12")) end end |
.legal?(str) ⇒ Boolean
Determine if the supplied string is legal. Legal strings will consist of 24 hexadecimal characters.
39 40 41 |
# File 'lib/bson/types/object_id.rb', line 39 def self.legal?(str) str =~ /^[0-9a-f]{24}$/i ? true : false end |
.machine_id ⇒ Object
152 153 154 |
# File 'lib/bson/types/object_id.rb', line 152 def self.machine_id @@machine_id end |
Instance Method Details
#as_json(options = {}) ⇒ Hash
Create the JSON hash structure convert to MongoDB extended format. Rails 2.3.3 introduced as_json to create the needed hash structure to encode objects into JSON.
139 140 141 |
# File 'lib/bson/types/object_id.rb', line 139 def as_json( ={}) {"$oid" => to_s} end |
#eql?(object_id) ⇒ Boolean Also known as: ==
Check equality of this object id with another.
81 82 83 |
# File 'lib/bson/types/object_id.rb', line 81 def eql?(object_id) object_id.kind_of?(BSON::ObjectId) and self.data == object_id.data end |
#generation_time ⇒ Time
Return the UTC time at which this ObjectId was generated. This may be used in lieu of a created_at timestamp since this information is always encoded in the object id.
148 149 150 |
# File 'lib/bson/types/object_id.rb', line 148 def generation_time Time.at(@data.pack("C4").unpack("N")[0]).utc end |
#hash ⇒ Integer
Get a unique hashcode for this object. This is required since we’ve defined an #eql? method.
90 91 92 |
# File 'lib/bson/types/object_id.rb', line 90 def hash @data.hash end |
#inspect ⇒ Object
123 124 125 |
# File 'lib/bson/types/object_id.rb', line 123 def inspect "BSON::ObjectId('#{to_s}')" end |
#to_a ⇒ Array
Get an array representation of the object id.
97 98 99 |
# File 'lib/bson/types/object_id.rb', line 97 def to_a @data.dup end |
#to_json(*a) ⇒ String
Convert to MongoDB extended JSON format. Since JSON includes type information, but lacks an ObjectId type, this JSON format encodes the type using an $oid key.
131 132 133 |
# File 'lib/bson/types/object_id.rb', line 131 def to_json(*a) "{\"$oid\": \"#{to_s}\"}" end |
#to_s ⇒ String
Get a string representation of this object id.
119 120 121 |
# File 'lib/bson/types/object_id.rb', line 119 def to_s @data.map {|e| v=e.to_s(16); v.size == 1 ? "0#{v}" : v }.join end |