Sha256: 66ac88783800634bdae3129fe42b7b81b744b4e28100c86abeb5dfe5f6ff3bac

Contents?: true

Size: 712 Bytes

Versions: 1

Compression:

Stored size: 712 Bytes

Contents

module Repia

  ##
  # 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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
repia-0.2.0 lib/repia/uuid_model.rb