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
-
#primary_key? ⇒ Boolean
True if the entity class defines a primary key and if the entity has a non-empty value for that attribute; otherwise false.
-
#primary_key_name ⇒ String
The name of the primary key attribute.
-
#primary_key_type ⇒ Class
The type of the primary key attribute.
-
#primary_key_value ⇒ Object
(also: #primary_key)
The current value of the primary key attribute.
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.
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_name ⇒ String
Returns the name of the primary key attribute.
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_type ⇒ Class
Returns the type of the primary key attribute.
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_value ⇒ Object Also known as: primary_key
Returns the current value of the primary key attribute.
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 |