Module: NoBrainer::Document::PrimaryKey::ClassMethods

Defined in:
lib/no_brainer/document/primary_key.rb

Instance Method Summary collapse

Instance Method Details

#define_default_pkObject



41
42
43
44
45
46
47
48
49
# File 'lib/no_brainer/document/primary_key.rb', line 41

def define_default_pk
  class_variable_set(:@@pk_name, nil)

  # TODO Maybe we should let the user configure the pk generator
  pk_generator = NoBrainer::Document::PrimaryKey::Generator

  field NoBrainer::Document::PrimaryKey::DEFAULT_PK_NAME, :primary_key => true,
    :type => pk_generator.field_type, :default => ->{ pk_generator.generate }
end

#field(attr, options = {}) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/no_brainer/document/primary_key.rb', line 51

def field(attr, options={})
  return super unless options[:primary_key]

  if attr != pk_name
    remove_field(pk_name, :set_default_pk => false) if pk_name
    class_variable_set(:@@pk_name, attr)
  end

  options[:index] = true
  options[:readonly] = true
  super
end

#pk_nameObject



37
38
39
# File 'lib/no_brainer/document/primary_key.rb', line 37

def pk_name
  class_variable_get(:@@pk_name)
end

#remove_field(attr, options = {}) ⇒ Object



64
65
66
67
68
69
# File 'lib/no_brainer/document/primary_key.rb', line 64

def remove_field(attr, options={})
  if fields[attr][:primary_key] && options[:set_default_pk] != false
    define_default_pk
  end
  super
end