Class: ActiveModelPersistence::Index
- Inherits:
-
Object
- Object
- ActiveModelPersistence::Index
- Defined in:
- lib/active_model_persistence/index.rb
Overview
An Index keeps a map from a key to zero of more objects
Instance Attribute Summary collapse
-
#base_class ⇒ Class
readonly
Identifies the base class this index applies to.
-
#key_value_source ⇒ Symbol, Proc
readonly
Defines how the object's key value is calculated.
-
#name ⇒ String
readonly
The name of the index.
-
#unique ⇒ Boolean
(also: #unique?)
readonly
Determines if a key value can index more than one object.
Instance Method Summary collapse
-
#add_or_update(object) ⇒ Index
(also: #<<)
Adds an object to the index.
-
#include?(key) ⇒ Boolean
Returns true if the index contains an object with the given key.
-
#initialize(name:, base_class:, key_value_source: nil, unique: false) ⇒ Index
constructor
Create an Index.
-
#objects(key) ⇒ Array<Object>
Returns the objects that match the key.
-
#remove(object, key = nil) ⇒ void
Removes an object from the index.
-
#remove_all ⇒ void
Removes all objects from the index.
Constructor Details
#initialize(name:, base_class:, key_value_source: nil, unique: false) ⇒ Index
Create an Index
89 90 91 92 93 94 95 |
# File 'lib/active_model_persistence/index.rb', line 89 def initialize(name:, base_class:, key_value_source: nil, unique: false) @name = name.to_s @base_class = base_class @key_value_source = determine_key_value_source(name, key_value_source) @unique = unique @key_to_objects_map = {} end |
Instance Attribute Details
#base_class ⇒ Class (readonly)
Identifies the base class this index applies to
Objects that are not of this class or one of its subclasses will not be added to the index.
28 29 30 |
# File 'lib/active_model_persistence/index.rb', line 28 def base_class @base_class end |
#key_value_source ⇒ Symbol, Proc (readonly)
Defines how the object's key value is calculated
If a proc is provided, it will be called with the object as an argument to get the key value.
If a symbol is provided, it will identify the method to call on the object to get the key value.
42 43 44 |
# File 'lib/active_model_persistence/index.rb', line 42 def key_value_source @key_value_source end |
#name ⇒ String (readonly)
The name of the index
16 17 18 |
# File 'lib/active_model_persistence/index.rb', line 16 def name @name end |
#unique ⇒ Boolean (readonly) Also known as: unique?
Determines if a key value can index more than one object
The default value is false.
If true, if two objects have the same key, a UniqueContraintError will be raised when trying to add the second object.
57 58 59 |
# File 'lib/active_model_persistence/index.rb', line 57 def unique @unique end |
Instance Method Details
#add_or_update(object) ⇒ Index Also known as: <<
Adds an object to the index
If the object was already in the index using a different key, remote the object from the index using the previous key before adding it again.
156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/active_model_persistence/index.rb', line 156 def add_or_update(object) previous_key = object.previous_index_key(name) key = key_value_for(object) return if previous_key == key remove(object, previous_key) unless previous_key.nil? add(object, key) unless key.nil? self end |
#include?(key) ⇒ Boolean
Returns true if the index contains an object with the given key
130 131 132 |
# File 'lib/active_model_persistence/index.rb', line 130 def include?(key) key_to_objects_map.key?(key) end |
#objects(key) ⇒ Array<Object>
Returns the objects that match the key
A unique index will return an Array containing zero or one objects. A non-unique index will return an array containing zero or more objects.
113 114 115 |
# File 'lib/active_model_persistence/index.rb', line 113 def objects(key) key_to_objects_map[key] || [] end |
#remove(object, key = nil) ⇒ void
This method returns an undefined value.
Removes an object from the index
190 191 192 193 194 195 196 197 198 |
# File 'lib/active_model_persistence/index.rb', line 190 def remove(object, key = nil) key ||= object.previous_index_key(name) raise ActiveModelPersistence::ObjectNotInIndexError if key.nil? remove_object_from_index(object, key) nil end |
#remove_all ⇒ void
This method returns an undefined value.
Removes all objects from the index
215 216 217 218 219 220 221 222 223 |
# File 'lib/active_model_persistence/index.rb', line 215 def remove_all @key_to_objects_map.each_pair do |_key, objects| objects.each do |object| object.clear_index_key(name) end end @key_to_objects_map = {} nil end |