Class: Moped::BSON::ObjectId

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/moped/bson/object_id.rb

Defined Under Namespace

Classes: Generator

Constant Summary collapse

@@generator =
Generator.new

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.__bson_load__(io) ⇒ Object



77
78
79
# File 'lib/moped/bson/object_id.rb', line 77

def __bson_load__(io)
  from_data(io.read(12))
end

.from_data(data) ⇒ Object



23
24
25
26
27
# File 'lib/moped/bson/object_id.rb', line 23

def from_data(data)
  id = allocate
  id.send(:data=, data)
  id
end

.from_string(string) ⇒ Object



10
11
12
13
# File 'lib/moped/bson/object_id.rb', line 10

def from_string(string)
  raise Errors::InvalidObjectId.new(string) unless legal?(string)
  from_data [string].pack("H*")
end

.from_time(time) ⇒ Object



15
16
17
# File 'lib/moped/bson/object_id.rb', line 15

def from_time(time)
  from_data [time.to_i].pack("Nx8")
end

.legal?(str) ⇒ Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/moped/bson/object_id.rb', line 19

def legal?(str)
  /\A\h{24}\Z/ === str.to_s
end

Instance Method Details

#<=>(other) ⇒ Object



48
49
50
# File 'lib/moped/bson/object_id.rb', line 48

def <=>(other)
  data <=> other.data
end

#==(other) ⇒ Object Also known as: eql?



43
44
45
# File 'lib/moped/bson/object_id.rb', line 43

def ==(other)
  BSON::ObjectId === other && data == other.data
end

#===(other) ⇒ Object



30
31
32
33
# File 'lib/moped/bson/object_id.rb', line 30

def ===(other)
  return to_str === other.to_str if other.respond_to?(:to_str)
  super
end

#__bson_dump__(io, key) ⇒ Object



82
83
84
85
86
# File 'lib/moped/bson/object_id.rb', line 82

def __bson_dump__(io, key)
  io << Types::OBJECT_ID
  io << key.to_bson_cstring
  io << data
end

#dataObject



35
36
37
38
39
40
41
# File 'lib/moped/bson/object_id.rb', line 35

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_timeObject

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.



72
73
74
# File 'lib/moped/bson/object_id.rb', line 72

def generation_time
  Time.at(data.unpack("N")[0]).utc
end

#hashObject



52
53
54
# File 'lib/moped/bson/object_id.rb', line 52

def hash
  data.hash
end

#inspectObject



61
62
63
# File 'lib/moped/bson/object_id.rb', line 61

def inspect
  to_s.inspect
end

#marshal_dumpObject



122
123
124
# File 'lib/moped/bson/object_id.rb', line 122

def marshal_dump
  data
end

#marshal_load(data) ⇒ Object



127
128
129
# File 'lib/moped/bson/object_id.rb', line 127

def marshal_load(data)
  self.data = data
end

#to_json(*args) ⇒ Object



65
66
67
# File 'lib/moped/bson/object_id.rb', line 65

def to_json(*args)
  "{\"$oid\": \"#{to_s}\"}"
end

#to_sObject Also known as: to_str



56
57
58
# File 'lib/moped/bson/object_id.rb', line 56

def to_s
  data.unpack("H*")[0]
end