Module: SqliteCrypto::ModelExtensions::ClassMethods

Defined in:
lib/sqlite_crypto/model_extensions.rb

Instance Method Summary collapse

Instance Method Details

#_sqlite_crypto_pk_typeObject

Detect if primary key is UUID or ULID based on column schema. Cached per class. Returns :uuid, :ulid, or nil.



48
49
50
51
# File 'lib/sqlite_crypto/model_extensions.rb', line 48

def _sqlite_crypto_pk_type
  return @_sqlite_crypto_pk_type if defined?(@_sqlite_crypto_pk_type)
  @_sqlite_crypto_pk_type = _detect_sqlite_crypto_pk_type
end

#generates_ulid(attribute, unique: false) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/sqlite_crypto/model_extensions.rb', line 38

def generates_ulid(attribute, unique: false)
  before_create do
    self[attribute] ||= ULID.generate.to_s
  end

  validates attribute, uniqueness: true if unique
end

#generates_uuid(attribute, unique: false) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/sqlite_crypto/model_extensions.rb', line 30

def generates_uuid(attribute, unique: false)
  before_create do
    self[attribute] ||= SqliteCrypto::Generators::Uuid.generate
  end

  validates attribute, uniqueness: true if unique
end