Class: Moped::BSON::ObjectId
Defined Under Namespace
Classes: Generator
Constant Summary collapse
Class Method Summary collapse
- .__bson_load__(io) ⇒ Object
- .from_data(data) ⇒ Object
- .from_string(string) ⇒ Object
- .from_time(time) ⇒ Object
- .legal?(str) ⇒ Boolean
Instance Method Summary collapse
- #<=>(other) ⇒ Object
- #==(other) ⇒ Object (also: #eql?)
- #__bson_dump__(io, key) ⇒ Object
- #data ⇒ Object
-
#generation_time ⇒ Object
Return the UTC time at which this ObjectId was generated.
- #hash ⇒ Object
- #inspect ⇒ Object
- #marshal_dump ⇒ Object
- #marshal_load(data) ⇒ Object
- #to_json(*args) ⇒ Object
- #to_s ⇒ Object
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
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 |
#data ⇒ Object
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_time ⇒ Object
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 |
#hash ⇒ Object
46 47 48 |
# File 'lib/moped/bson/object_id.rb', line 46 def hash data.hash end |
#inspect ⇒ Object
54 55 56 |
# File 'lib/moped/bson/object_id.rb', line 54 def inspect to_s.inspect end |
#marshal_dump ⇒ Object
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_s ⇒ Object
50 51 52 |
# File 'lib/moped/bson/object_id.rb', line 50 def to_s data.unpack("H*")[0] end |