Class: Moped::BSON::ObjectId

Inherits:
Object
  • Object
show all
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



70
71
72
# File 'lib/moped/bson/object_id.rb', line 70

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

.from_data(data) ⇒ Object



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

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

.from_string(string) ⇒ Object



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

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

.from_time(time) ⇒ Object



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

def from_time(time)
  from_data @@generator.generate(time.to_i)
end

.legal?(str) ⇒ Boolean

Returns:

  • (Boolean)


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

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

Instance Method Details

#<=>(other) ⇒ Object



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

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

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



37
38
39
# File 'lib/moped/bson/object_id.rb', line 37

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

#__bson_dump__(io, key) ⇒ Object



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

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

#dataObject



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

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.



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

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

#hashObject



46
47
48
# File 'lib/moped/bson/object_id.rb', line 46

def hash
  data.hash
end

#inspectObject



54
55
56
# File 'lib/moped/bson/object_id.rb', line 54

def inspect
  to_s.inspect
end

#marshal_dumpObject



114
115
116
# File 'lib/moped/bson/object_id.rb', line 114

def marshal_dump
  data
end

#marshal_load(data) ⇒ Object



119
120
121
# File 'lib/moped/bson/object_id.rb', line 119

def marshal_load(data)
  self.data = data
end

#to_json(*args) ⇒ Object



58
59
60
# File 'lib/moped/bson/object_id.rb', line 58

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

#to_sObject



50
51
52
# File 'lib/moped/bson/object_id.rb', line 50

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