Module: Sequent::Core::Helpers::UniqueKeys
- Included in:
- AggregateRoot
- Defined in:
- lib/sequent/core/helpers/unique_keys.rb
Overview
Some aggregates represent a unique external entity (e.g. a user’s email address or login name) and this uniqueness needs to be enforced. For each unique key the returned object should have an entry where the key of the entry describes the scope of the constraint (e.g. ‘user_email` or `login_name`) and the value represents the unique value. Values can be any JSON value (string, object, array, etc). Note that uniqueness is enforced across all aggregate types if the same scope is used.
An ‘AggregateKeyNotUniqueError` is raised if a unique constrained is violated when committing the events to the database.
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
Instance Method Summary collapse
-
#unique_keys ⇒ Object
Returns the unique keys for the current instance based on the ‘unique_key` defintions.
Class Method Details
.included(host_class) ⇒ Object
75 76 77 |
# File 'lib/sequent/core/helpers/unique_keys.rb', line 75 def self.included(host_class) host_class.extend(ClassMethods) end |
Instance Method Details
#unique_keys ⇒ Object
Returns the unique keys for the current instance based on the ‘unique_key` defintions. You can also override it if you need more compicated logic.
Example return value:
“‘ {
user_email: { email: '[email protected]' }
} “‘
65 66 67 68 69 70 71 72 73 |
# File 'lib/sequent/core/helpers/unique_keys.rb', line 65 def unique_keys return {} if self.class.unique_key_definitions.nil? self.class.unique_key_definitions &.transform_values do |attributes| attributes.transform_values { |block| instance_exec(&block) }.compact end &.delete_if { |_, value| value.empty? } end |