Class: Moped::BSON::ObjectId
- Includes:
- Comparable
- Defined in:
- lib/moped/bson/object_id.rb
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
71 72 73 |
# File 'lib/moped/bson/object_id.rb', line 71 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
19 20 21 |
# File 'lib/moped/bson/object_id.rb', line 19 def legal?(str) /\A\h{24}\Z/ === str end |
Instance Method Details
#<=>(other) ⇒ Object
43 44 45 |
# File 'lib/moped/bson/object_id.rb', line 43 def <=>(other) data <=> other.data end |
#==(other) ⇒ Object Also known as: eql?
38 39 40 |
# File 'lib/moped/bson/object_id.rb', line 38 def ==(other) BSON::ObjectId === other && data == other.data end |
#__bson_dump__(io, key) ⇒ Object
76 77 78 79 80 |
# File 'lib/moped/bson/object_id.rb', line 76 def __bson_dump__(io, key) io << Types::OBJECT_ID io << key.to_bson_cstring io << data end |
#data ⇒ Object
30 31 32 33 34 35 36 |
# File 'lib/moped/bson/object_id.rb', line 30 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.
66 67 68 |
# File 'lib/moped/bson/object_id.rb', line 66 def generation_time Time.at(data.unpack("N")[0]).utc end |
#hash ⇒ Object
47 48 49 |
# File 'lib/moped/bson/object_id.rb', line 47 def hash data.hash end |
#inspect ⇒ Object
55 56 57 |
# File 'lib/moped/bson/object_id.rb', line 55 def inspect to_s.inspect end |
#marshal_dump ⇒ Object
115 116 117 |
# File 'lib/moped/bson/object_id.rb', line 115 def marshal_dump data end |
#marshal_load(data) ⇒ Object
120 121 122 |
# File 'lib/moped/bson/object_id.rb', line 120 def marshal_load(data) self.data = data end |
#to_json(*args) ⇒ Object
59 60 61 |
# File 'lib/moped/bson/object_id.rb', line 59 def to_json(*args) "{\"$oid\": \"#{to_s}\"}" end |
#to_s ⇒ Object
51 52 53 |
# File 'lib/moped/bson/object_id.rb', line 51 def to_s data.unpack("H*")[0] end |