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
-
#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:, 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:, key_value_source: nil, unique: false) ⇒ Index
Create an Index
77 78 79 80 81 82 |
# File 'lib/active_model_persistence/index.rb', line 77 def initialize(name:, key_value_source: nil, unique: false) @name = name.to_s @key_value_source = determine_key_value_source(name, key_value_source) @unique = unique @key_to_objects_map = {} end |
Instance Attribute Details
#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.
30 31 32 |
# File 'lib/active_model_persistence/index.rb', line 30 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.
45 46 47 |
# File 'lib/active_model_persistence/index.rb', line 45 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.
143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/active_model_persistence/index.rb', line 143 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
117 118 119 |
# File 'lib/active_model_persistence/index.rb', line 117 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.
100 101 102 |
# File 'lib/active_model_persistence/index.rb', line 100 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
177 178 179 180 181 182 183 184 185 |
# File 'lib/active_model_persistence/index.rb', line 177 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
202 203 204 205 206 207 208 209 210 |
# File 'lib/active_model_persistence/index.rb', line 202 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 |