Class: BSON::ObjectId

Inherits:
Object show all
Defined in:
lib/jmongo/mongo/bson.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.create_pk(doc) ⇒ Object



19
20
21
# File 'lib/jmongo/mongo/bson.rb', line 19

def self.create_pk(doc)
  doc.has_key?(:_id) || doc.has_key?('_id') ? doc : doc.merge!('_id' => self.new)
end

.from_string(str) ⇒ Object



13
14
15
16
17
# File 'lib/jmongo/mongo/bson.rb', line 13

def self.from_string(str)
  v = is_valid?(str.to_s)
  raise BSON::InvalidObjectId, "illegal ObjectID format" unless v
  new(str.to_s)
end

.from_time(time, opts = {}) ⇒ Object



23
24
25
26
27
28
29
30
# File 'lib/jmongo/mongo/bson.rb', line 23

def self.from_time(time, opts={})
  unique = opts.fetch(:unique, false)
  if unique
    self.new(time)
  else
    self.new([time.to_i,0,0].pack("NNN").to_java_bytes)
  end
end

Instance Method Details

#cloneObject



38
39
40
# File 'lib/jmongo/mongo/bson.rb', line 38

def clone
  self.class.new(self.to_byte_array)
end

#dataObject

“data=”, “decode64”, “encode64”, “decode_b”, “b64encode” - shout out if these methods are needed



34
35
36
# File 'lib/jmongo/mongo/bson.rb', line 34

def data
  self.to_byte_array.to_a.map{|x| x & 0xFF}
end

#generation_timeObject



46
47
48
# File 'lib/jmongo/mongo/bson.rb', line 46

def generation_time
  Time.at(self.get_time/1000).utc
end

#inspectObject



42
43
44
# File 'lib/jmongo/mongo/bson.rb', line 42

def inspect
  "BSON::ObjectID('#{self.to_s}')"
end