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.
Instance Method Summary collapse
-
#find_each(attributes_index: {}) { ... } ⇒ Object
Iterator that mimics find_each of ActiveRecord::Relation using find_in_batches (see #find_in_batches).
-
#find_in_batches(batch_size: 1000, attributes_index: {}) { ... } ⇒ Object
Iterator that mimics find_in_batches of ActiveRecord::Relation.
-
#initialize(inventory_collection: nil) ⇒ ApplicationRecordIterator
constructor
An iterator that can fetch batches of the AR objects based on a set of attribute_indexes.
Constructor Details
#initialize(inventory_collection: nil) ⇒ ApplicationRecordIterator
An iterator that can fetch batches of the AR objects based on a set of attribute_indexes
8 9 10 |
# File 'lib/inventory_refresh/application_record_iterator.rb', line 8 def initialize(inventory_collection: nil) @inventory_collection = inventory_collection 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 |
Instance Method Details
#find_each(attributes_index: {}) { ... } ⇒ Object
Iterator that mimics find_each of ActiveRecord::Relation using find_in_batches (see #find_in_batches)
31 32 33 34 35 36 37 |
# File 'lib/inventory_refresh/application_record_iterator.rb', line 31 def find_each(attributes_index: {}) find_in_batches(:attributes_index => attributes_index) do |batch| batch.each do |item| yield(item) end end end |
#find_in_batches(batch_size: 1000, attributes_index: {}) { ... } ⇒ 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)
22 23 24 25 26 |
# File 'lib/inventory_refresh/application_record_iterator.rb', line 22 def find_in_batches(batch_size: 1000, attributes_index: {}) attributes_index.each_slice(batch_size) do |batch| yield(inventory_collection.db_collection_for_comparison_for(batch)) end end |