Class: InventoryRefresh::ApplicationRecordIterator
- Inherits:
-
Object
- Object
- InventoryRefresh::ApplicationRecordIterator
- Defined in:
- lib/inventory_refresh/application_record_iterator.rb
Instance Attribute Summary collapse
-
#inventory_collection ⇒ Object
readonly
Returns the value of attribute inventory_collection.
-
#iterator ⇒ Object
readonly
Returns the value of attribute iterator.
-
#manager_uuids_set ⇒ Object
readonly
Returns the value of attribute manager_uuids_set.
-
#query ⇒ Object
readonly
Returns the value of attribute query.
Instance Method Summary collapse
-
#find_each { ... } ⇒ Object
Iterator that mimics find_each of ActiveRecord::Relation using find_in_batches (see #find_in_batches).
-
#find_in_batches(batch_size: 1000) { ... } ⇒ Object
Iterator that mimics find_in_batches of ActiveRecord::Relation.
-
#initialize(inventory_collection: nil, manager_uuids_set: nil, iterator: nil, query: nil) ⇒ ApplicationRecordIterator
constructor
An iterator that can fetch batches of the AR objects based on a set of manager refs, or just mimics AR relation when given an iterator.
Constructor Details
#initialize(inventory_collection: nil, manager_uuids_set: nil, iterator: nil, query: nil) ⇒ ApplicationRecordIterator
An iterator that can fetch batches of the AR objects based on a set of manager refs, or just mimics AR relation when given an iterator. Or given query, acts as iterator by selecting batches.
13 14 15 16 17 18 |
# File 'lib/inventory_refresh/application_record_iterator.rb', line 13 def initialize(inventory_collection: nil, manager_uuids_set: nil, iterator: nil, query: nil) @inventory_collection = inventory_collection @manager_uuids_set = manager_uuids_set @iterator = iterator @query = query end |
Instance Attribute Details
#inventory_collection ⇒ Object (readonly)
Returns the value of attribute inventory_collection.
3 4 5 |
# File 'lib/inventory_refresh/application_record_iterator.rb', line 3 def inventory_collection @inventory_collection end |
#iterator ⇒ Object (readonly)
Returns the value of attribute iterator.
3 4 5 |
# File 'lib/inventory_refresh/application_record_iterator.rb', line 3 def iterator @iterator end |
#manager_uuids_set ⇒ Object (readonly)
Returns the value of attribute manager_uuids_set.
3 4 5 |
# File 'lib/inventory_refresh/application_record_iterator.rb', line 3 def manager_uuids_set @manager_uuids_set end |
#query ⇒ Object (readonly)
Returns the value of attribute query.
3 4 5 |
# File 'lib/inventory_refresh/application_record_iterator.rb', line 3 def query @query end |
Instance Method Details
#find_each { ... } ⇒ Object
Iterator that mimics find_each of ActiveRecord::Relation using find_in_batches (see #find_in_batches)
46 47 48 49 50 |
# File 'lib/inventory_refresh/application_record_iterator.rb', line 46 def find_each(&block) find_in_batches do |batch| batch.each(&block) end end |
#find_in_batches(batch_size: 1000) { ... } ⇒ Object
Iterator that mimics find_in_batches of ActiveRecord::Relation. This iterator serves for making more optimized query since e.g. having 1500 ids if objects we want to return. Doing relation.where(:id => 1500ids).find_each would always search for all 1500 ids, then return on limit 1000.
With this iterator we build queries using only batch of ids, so find_each will cause relation.where(:id => 1000ids) and relation.where(:id => 500ids)
29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/inventory_refresh/application_record_iterator.rb', line 29 def find_in_batches(batch_size: 1000, &block) if iterator iterator.call(&block) elsif query manager_uuids_set.each_slice(batch_size) do |batch| yield(query.where(inventory_collection.targeted_selection_for(batch))) end else manager_uuids_set.each_slice(batch_size) do |batch| yield(inventory_collection.db_collection_for_comparison_for(batch)) end end end |