Sha256: 4b332f44239bb3f34a641d99bb038451ebc52823c3af3d2f0a4e5de59caf781f

Contents?: true

Size: 780 Bytes

Versions: 1

Compression:

Stored size: 780 Bytes

Contents

module Repia

  module Support

    ##
    # This module is a mixin that allows the model to use UUIDs instead of
    # normal IDs. By including this module, the model class declares that the
    # primary key is called "uuid" and an UUID is generated right before
    # save(). You may assign an UUID prior to save, in which case, no new UUID
    # will be generated.
    #
    module UUIDModel

      ##
      # Triggered when this module is included.
      #
      def self.included(klass)
        klass.primary_key = "uuid"
        klass.before_create :generate_uuid
      end

      ##
      # Generates an UUID for the model object.
      #
      def generate_uuid()
        self.uuid = UUIDTools::UUID.timestamp_create().to_s if self.uuid.nil?
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
repia-0.3.0 lib/repia/support/uuid_model.rb