Module: DataMapper::Aspects::BSONID

Defined in:
lib/datamapper/aspects/bson_id.rb

Overview

Public: Provides a valid BSON Object ID key property. Also validates that BSON IDs are valid object ids.

Examples

my_obj.id.id_is_valid?
# => true

my_obj.id.id_generation_time
# => 2014-05-09 06:07:00 UTC

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/datamapper/aspects/bson_id.rb', line 16

def self.included(base)
  # Public: Provides the default aspect's attributes.
  base.property :id, String, length: 24, key: true, default: Moped::BSON::ObjectId.new.to_s

  # Internal: Validates that the BSON ID is a valid object id.
  base.validates_with_method :id, method: :id_is_valid?

  # Public: Retrieves the generation time of the BSON Object ID.
  #
  # Examples
  #
  #   id_generation_time()
  #   # => 2014-05-09 06:07:00 UTC
  #
  # Returns the generation time of the BSON Object ID as a String.
  def id_generation_time
    Moped::BSON::ObjectId.from_string(@id).generation_time
  end

  # Public: Checks if the ID is a valid BSON Object ID.
  #
  # Examples
  #
  #   id_is_valid?()
  #   # => true
  #
  # Returns true if ID is valid, false if not.
  def id_is_valid?
    Moped::BSON::ObjectId.legal?(@id)
  end
end

Instance Method Details

#id_generation_timeObject

Public: Retrieves the generation time of the BSON Object ID.

Examples

id_generation_time()
# => 2014-05-09 06:07:00 UTC

Returns the generation time of the BSON Object ID as a String.



31
32
33
# File 'lib/datamapper/aspects/bson_id.rb', line 31

def id_generation_time
  Moped::BSON::ObjectId.from_string(@id).generation_time
end

#id_is_valid?Boolean

Public: Checks if the ID is a valid BSON Object ID.

Examples

id_is_valid?()
# => true

Returns true if ID is valid, false if not.

Returns:

  • (Boolean)


43
44
45
# File 'lib/datamapper/aspects/bson_id.rb', line 43

def id_is_valid?
  Moped::BSON::ObjectId.legal?(@id)
end