module DataMapper::Aspects::BSONID
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
Public Class Methods
included(base)
click to toggle source
# 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: BSON::ObjectId.new.to_s base.property :id, String, length: 24, key: true, unique: true, default: proc { 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 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? if BSON::ObjectId.legal?(@id) true else [false, 'Id must be a valid BSON ObjectId'] end end end
Public Instance Methods
id_generation_time()
click to toggle source
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.
# File lib/datamapper/aspects/bson_id.rb, line 36 def id_generation_time BSON::ObjectId.from_string(@id).generation_time end
id_is_valid?()
click to toggle source
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.
# File lib/datamapper/aspects/bson_id.rb, line 48 def id_is_valid? if BSON::ObjectId.legal?(@id) true else [false, 'Id must be a valid BSON ObjectId'] end end