Sha256: dac61e112f0294476593481a430b134979458d72eeca4090d3b889f1c776d8bd

Contents?: true

Size: 1.56 KB

Versions: 1

Compression:

Stored size: 1.56 KB

Contents

# encoding: utf-8

module DataMapper
  module Aspects
    # 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
    module BSONID
      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
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
dm-aspects-0.1.0 lib/datamapper/aspects/bson_id.rb