Module: Stannum::Entities::PrimaryKey

Included in:
Stannum::Entity
Defined in:
lib/stannum/entities/primary_key.rb

Overview

Methods for defining and accessing an entity's primary key attribute.

Defined Under Namespace

Modules: ClassMethods Classes: PrimaryKeyAlreadyExists, PrimaryKeyMissing

Instance Method Summary collapse

Instance Method Details

#primary_key?Boolean

Returns true if the entity class defines a primary key and if the entity has a non-empty value for that attribute; otherwise false.

Returns:

  • true if the entity class defines a primary key and if the entity has a non-empty value for that attribute; otherwise false.



101
102
103
104
105
106
107
108
109
# File 'lib/stannum/entities/primary_key.rb', line 101

def primary_key?
  return false unless self.class.primary_key?

  value = attributes[self.class.primary_key_name]

  return false if value.nil? || (value.respond_to?(:empty?) && value.empty?)

  true
end

#primary_key_nameString

Returns the name of the primary key attribute.

Returns:

  • the name of the primary key attribute.

Raises:

  • if the entity does not define a primary key.



115
116
117
118
119
120
121
# File 'lib/stannum/entities/primary_key.rb', line 115

def primary_key_name
  unless self.class.primary_key?
    raise PrimaryKeyMissing, "#{self.class} does not define a primary key"
  end

  self.class.primary_key_name
end

#primary_key_typeClass

Returns the type of the primary key attribute.

Returns:

  • the type of the primary key attribute.

Raises:

  • if the entity does not define a primary key.



127
128
129
130
131
132
133
# File 'lib/stannum/entities/primary_key.rb', line 127

def primary_key_type
  unless self.class.primary_key?
    raise PrimaryKeyMissing, "#{self.class} does not define a primary key"
  end

  self.class.primary_key_type
end

#primary_key_valueObject Also known as: primary_key

Returns the current value of the primary key attribute.

Returns:

  • the current value of the primary key attribute.

Raises:

  • if the entity does not define a primary key.



139
140
141
142
143
144
145
# File 'lib/stannum/entities/primary_key.rb', line 139

def primary_key_value
  unless self.class.primary_key?
    raise PrimaryKeyMissing, "#{self.class} does not define a primary key"
  end

  attributes[self.class.primary_key_name]
end