Class: InventoryRefresh::InventoryCollection
- Inherits:
-
Object
- Object
- InventoryRefresh::InventoryCollection
- Defined in:
- lib/inventory_refresh/inventory_collection.rb,
lib/inventory_refresh/inventory_collection/graph.rb,
lib/inventory_refresh/inventory_collection/scanner.rb,
lib/inventory_refresh/inventory_collection/reference.rb,
lib/inventory_refresh/inventory_collection/index/proxy.rb,
lib/inventory_refresh/inventory_collection/data_storage.rb,
lib/inventory_refresh/inventory_collection/serialization.rb,
lib/inventory_refresh/inventory_collection/index/type/base.rb,
lib/inventory_refresh/inventory_collection/index/type/data.rb,
lib/inventory_refresh/inventory_collection/references_storage.rb,
lib/inventory_refresh/inventory_collection/index/type/local_db.rb,
lib/inventory_refresh/inventory_collection/index/type/skeletal.rb
Overview
For more usage examples please follow spec examples in
-
spec/models/inventory_refresh/save_inventory/single_inventory_collection_spec.rb
-
spec/models/inventory_refresh/save_inventory/acyclic_graph_of_inventory_collections_spec.rb
-
spec/models/inventory_refresh/save_inventory/graph_of_inventory_collections_spec.rb
-
spec/models/inventory_refresh/save_inventory/graph_of_inventory_collections_targeted_refresh_spec.rb
-
spec/models/inventory_refresh/save_inventory/strategies_and_references_spec.rb
Defined Under Namespace
Modules: Index Classes: DataStorage, Graph, Reference, ReferencesStorage, Scanner, Serialization
Instance Attribute Summary collapse
-
#all_manager_uuids ⇒ Array?
If present, InventoryCollection switches into delete_complement mode, where it will delete every record from the DB, that is not present in this list.
-
#arel ⇒ Object
readonly
Returns the value of attribute arel.
-
#assert_graph_integrity ⇒ Object
readonly
Returns the value of attribute assert_graph_integrity.
-
#association ⇒ Object
readonly
Returns the value of attribute association.
-
#attributes_blacklist ⇒ Object
Returns the value of attribute attributes_blacklist.
-
#attributes_whitelist ⇒ Object
Returns the value of attribute attributes_whitelist.
-
#batch_extra_attributes ⇒ Object
readonly
Returns the value of attribute batch_extra_attributes.
-
#check_changed ⇒ Object
readonly
Returns the value of attribute check_changed.
-
#complete ⇒ Object
readonly
Returns the value of attribute complete.
-
#create_only ⇒ Object
readonly
Returns the value of attribute create_only.
-
#created_records ⇒ Object
readonly
Returns the value of attribute created_records.
-
#custom_reconnect_block ⇒ Object
readonly
Returns the value of attribute custom_reconnect_block.
-
#custom_save_block ⇒ Object
readonly
Returns the value of attribute custom_save_block.
-
#data_collection_finalized ⇒ Boolean
A true value marks that we collected all the data of the InventoryCollection, meaning we also collected all the references.
-
#data_storage ⇒ InventoryRefresh::InventoryCollection::DataStorage
An InventoryCollection encapsulating all data with indexes.
-
#default_values ⇒ Object
readonly
Returns the value of attribute default_values.
-
#delete_method ⇒ Object
readonly
Returns the value of attribute delete_method.
-
#deleted_records ⇒ Object
readonly
Returns the value of attribute deleted_records.
-
#dependees ⇒ Set
A set of InventoryCollection objects that depends on this InventoryCollection object.
-
#dependency_attributes ⇒ Object
readonly
Returns the value of attribute dependency_attributes.
-
#internal_attributes ⇒ Object
readonly
Returns the value of attribute internal_attributes.
-
#inventory_object_attributes ⇒ Object
readonly
Returns the value of attribute inventory_object_attributes.
-
#manager_ref ⇒ Object
readonly
Returns the value of attribute manager_ref.
-
#manager_ref_allowed_nil ⇒ Object
readonly
Returns the value of attribute manager_ref_allowed_nil.
-
#model_class ⇒ Object
readonly
Returns the value of attribute model_class.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
-
#parent_inventory_collections ⇒ Array<Symbol>
parameters.
-
#references_storage ⇒ Object
readonly
Returns the value of attribute references_storage.
-
#saved ⇒ Boolean
True if this collection is already saved into the DB.
-
#saver_strategy ⇒ Object
readonly
Returns the value of attribute saver_strategy.
-
#strategy ⇒ Object
readonly
Returns the value of attribute strategy.
-
#targeted ⇒ Object
readonly
Returns the value of attribute targeted.
-
#targeted_arel ⇒ Object
readonly
Returns the value of attribute targeted_arel.
-
#targeted_scope ⇒ Object
readonly
Returns the value of attribute targeted_scope.
-
#transitive_dependency_attributes ⇒ Object
readonly
Returns the value of attribute transitive_dependency_attributes.
-
#update_only ⇒ Object
readonly
Returns the value of attribute update_only.
-
#updated_records ⇒ Object
readonly
Returns the value of attribute updated_records.
-
#use_ar_object ⇒ Object
readonly
Returns the value of attribute use_ar_object.
Instance Method Summary collapse
-
#association_to_base_class_mapping ⇒ Hash{Symbol => String}
Hash with association name mapped to base class of the association.
-
#association_to_foreign_key_mapping ⇒ Hash{Symbol => String}
Hash with association name mapped to foreign key column name.
-
#association_to_foreign_type_mapping ⇒ Hash{Symbol => String}
Hash with association name mapped to polymorphic foreign key type column name.
-
#base_class_name ⇒ String
Base class name of the model_class of this InventoryCollection.
- #base_columns ⇒ Object
-
#batch_size ⇒ Integer
Default batch size for talking to the DB.
-
#batch_size_pure_sql ⇒ Integer
Default batch size for talking to the DB if not using ApplicationRecord objects.
-
#belongs_to_associations ⇒ Array<ActiveRecord::Reflection::BelongsToReflection">
All belongs_to associations.
-
#blacklist_attributes!(attributes) ⇒ Array<Symbol>
Add passed attributes to blacklist.
-
#build_multi_selection_condition(hashes, keys = manager_ref) ⇒ String
Builds a multiselection conditions like (table1.a = a1 AND table2.b = b1) OR (table1.a = a2 AND table2.b = b2).
-
#check_changed? ⇒ Boolean
True means we want to call .changed? on every ActiveRecord object before saving it.
-
#clone ⇒ InventoryCollection
A shallow copy of InventoryCollection, the copy will share data_storage of the original collection, otherwise we would be copying a lot of records in memory.
-
#complete? ⇒ Boolean
True means the data is not complete, leading to only creating and updating data.
-
#create_allowed? ⇒ Boolean
True means we will delete/soft-delete data.
-
#create_only? ⇒ Boolean
True means that only create of new data is allowed.
-
#data_collection_finalized? ⇒ Boolean
True if no more data will be added to this InventoryCollection object, that usually happens after the parsing step is finished.
-
#db_collection_for_comparison ⇒ ActiveRecord::Relation
A relation that can fetch all data of this InventoryCollection from the DB.
-
#db_collection_for_comparison_for(references) ⇒ ActiveRecord::Relation
Builds an ActiveRecord::Relation that can fetch all the references from the DB.
-
#db_collection_for_comparison_for_complement_of(manager_uuids_set) ⇒ ActiveRecord::Relation
Builds an ActiveRecord::Relation that can fetch complement of all the references from the DB.
-
#delete_allowed? ⇒ Boolean
True means we will delete/soft-delete data.
-
#dependencies ⇒ Array<InventoryRefresh::InventoryCollection>
All unique non saved dependencies.
-
#dependency_attributes_for(inventory_collections) ⇒ Array<InventoryRefresh::InventoryCollection>
Returns what attributes are causing a dependencies to certain InventoryCollection objects.
-
#filtered_dependency_attributes ⇒ Hash{Symbol => Set}
List attributes causing a dependency and filters them by attributes_blacklist and attributes_whitelist.
-
#fixed_attributes ⇒ Array<Symbol>
Attributes that are needed to be able to save the record, i.e.
-
#fixed_dependencies ⇒ Object
Returns fixed dependencies, which are the ones we can’t move, because we wouldn’t be able to save the data.
-
#fixed_foreign_keys ⇒ Array<Symbol>
List of all column names that are foreign keys and cannot removed, otherwise we couldn’t save the record.
-
#foreign_key_to_association_mapping ⇒ Hash{String => Hash}
Hash with foreign_key column name mapped to association name.
-
#foreign_keys ⇒ Array<Symbol>
List of all column names that are foreign keys.
-
#foreign_type_to_association_mapping ⇒ Hash{Symbol => String}
Hash with polymorphic foreign key type column name mapped to association name.
-
#full_collection_for_comparison ⇒ ActiveRecord::Relation
Relation that can fetch all the references from the DB.
-
#initialize(model_class: nil, manager_ref: nil, association: nil, parent: nil, strategy: nil, custom_save_block: nil, delete_method: nil, dependency_attributes: nil, attributes_blacklist: nil, attributes_whitelist: nil, complete: nil, update_only: nil, check_changed: nil, arel: nil, default_values: {}, create_only: nil, inventory_object_attributes: nil, name: nil, saver_strategy: nil, parent_inventory_collections: nil, manager_uuids: [], all_manager_uuids: nil, targeted_arel: nil, targeted: nil, manager_ref_allowed_nil: nil, secondary_refs: {}, use_ar_object: nil, custom_reconnect_block: nil, batch_extra_attributes: [], assert_graph_integrity: nil) ⇒ InventoryCollection
constructor
A new instance of InventoryCollection.
-
#inspect ⇒ String
A concise form of the InventoryCollection for easy logging.
- #internal_columns ⇒ Object
- #internal_timestamp_columns ⇒ Object
-
#inventory_object?(value) ⇒ Boolean
True is value is kind of InventoryRefresh::InventoryObject.
-
#inventory_object_lazy?(value) ⇒ Boolean
True is value is kind of InventoryRefresh::InventoryObjectLazy.
-
#manager_ref_to_cols ⇒ Array<String>
Convert manager_ref list of attributes to list of DB columns.
-
#manager_uuids ⇒ Array<String>
Returns a list of stringified uuids of all scoped InventoryObjects, which is used for scoping in targeted mode.
-
#new_inventory_object(hash) ⇒ InventoryRefresh::InventoryObject
Creates InventoryRefresh::InventoryObject object from passed hash data.
-
#noop? ⇒ Boolean
True if processing of this InventoryCollection object would lead to no operations.
-
#object_index_with_keys(keys, record) ⇒ String
Builds string uuid from passed Object and keys.
-
#parallel_safe? ⇒ Boolean
True if we are using a saver strategy that allows saving in parallel processes.
-
#process_saver_strategy(saver_strategy) ⇒ Symbol
Processes passed saver strategy.
-
#process_strategy(strategy_name) ⇒ Symbol
Processes passed strategy, modifies :data_collection_finalized and :saved attributes for db only strategies.
-
#saveable? ⇒ Boolean
True if all dependencies have all data persisted.
-
#saved? ⇒ Boolean
True if the whole InventoryCollection object has all data persisted.
-
#store_created_records(records) ⇒ Object
Caches what records were created, for later use, e.g.
-
#store_deleted_records(records) ⇒ Object
Caches what records were deleted/soft-deleted, for later use, e.g.
-
#store_updated_records(records) ⇒ Object
Caches what records were updated, for later use, e.g.
-
#supports_created_at? ⇒ Boolean
True if the model_class has created_at column.
-
#supports_created_on? ⇒ Boolean
True if the model_class has created_on column.
-
#supports_resource_timestamp? ⇒ Boolean
True if the model_class has resource_timestamp column.
-
#supports_resource_timestamps? ⇒ Boolean
True if the model_class has resource_timestamps column.
-
#supports_resource_timestamps_max? ⇒ Boolean
True if the model_class has resource_timestamps_max column.
-
#supports_resource_version? ⇒ Boolean
True if the model_class has resource_version column.
-
#supports_resource_versions? ⇒ Boolean
True if the model_class has resource_versions column.
-
#supports_resource_versions_max? ⇒ Boolean
True if the model_class has resource_versions_max column.
-
#supports_sti? ⇒ Boolean
True if the model_class supports STI.
-
#supports_updated_at? ⇒ Boolean
True if the model_class has updated_at column.
-
#supports_updated_on? ⇒ Boolean
True if the model_class has updated_on column.
-
#targeted? ⇒ Boolean
True is processing of this InventoryCollection will be in targeted mode.
-
#targeted_arel_default ⇒ InventoryRefresh::ApplicationRecordIterator
Builds targeted query limiting the results by the :references defined in parent_inventory_collections.
-
#targeted_iterator_for(references, query = nil) ⇒ InventoryRefresh::ApplicationRecordIterator
Returns iterator for the passed references and a query.
-
#targeted_selection_for(references) ⇒ String
Builds a multiselection conditions like (table1.a = a1 AND table2.b = b1) OR (table1.a = a2 AND table2.b = b2) for passed references.
-
#to_s ⇒ String
A concise form of the inventoryCollection for easy logging.
-
#transform_references_to_hashes(references) ⇒ Array<Hash>
Gets targeted references and transforms them into list of hashes.
-
#unique_index_columns ⇒ Array<Symbol>
All columns that are part of the best fit unique index.
-
#unique_index_for(keys) ⇒ ActiveRecord::ConnectionAdapters::IndexDefinition
Finds an index that fits the list of columns (keys) the best.
- #unique_index_keys ⇒ Object
-
#unique_indexes ⇒ Array<ActiveRecord::ConnectionAdapters::IndexDefinition>
Array of all unique indexes known to model.
-
#update_only? ⇒ Boolean
True means we want to only update data.
-
#use_ar_object? ⇒ Boolean
True means we want to use ActiveRecord object for writing attributes and we want to perform casting on all columns.
-
#whitelist_attributes!(attributes) ⇒ Array<Symbol>
Add passed attributes to whitelist.
Constructor Details
#initialize(model_class: nil, manager_ref: nil, association: nil, parent: nil, strategy: nil, custom_save_block: nil, delete_method: nil, dependency_attributes: nil, attributes_blacklist: nil, attributes_whitelist: nil, complete: nil, update_only: nil, check_changed: nil, arel: nil, default_values: {}, create_only: nil, inventory_object_attributes: nil, name: nil, saver_strategy: nil, parent_inventory_collections: nil, manager_uuids: [], all_manager_uuids: nil, targeted_arel: nil, targeted: nil, manager_ref_allowed_nil: nil, secondary_refs: {}, use_ar_object: nil, custom_reconnect_block: nil, batch_extra_attributes: [], assert_graph_integrity: nil) ⇒ InventoryCollection
Returns a new instance of InventoryCollection.
415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 415 def initialize(model_class: nil, manager_ref: nil, association: nil, parent: nil, strategy: nil, custom_save_block: nil, delete_method: nil, dependency_attributes: nil, attributes_blacklist: nil, attributes_whitelist: nil, complete: nil, update_only: nil, check_changed: nil, arel: nil, default_values: {}, create_only: nil, inventory_object_attributes: nil, name: nil, saver_strategy: nil, parent_inventory_collections: nil, manager_uuids: [], all_manager_uuids: nil, targeted_arel: nil, targeted: nil, manager_ref_allowed_nil: nil, secondary_refs: {}, use_ar_object: nil, custom_reconnect_block: nil, batch_extra_attributes: [], assert_graph_integrity: nil) @model_class = model_class @manager_ref = manager_ref || [:ems_ref] @secondary_refs = secondary_refs @association = association @parent = parent || nil @arel = arel @dependency_attributes = dependency_attributes || {} @strategy = process_strategy(strategy) @delete_method = delete_method || :destroy @custom_save_block = custom_save_block @custom_reconnect_block = custom_reconnect_block @check_changed = check_changed.nil? ? true : check_changed @internal_attributes = i(__feedback_edge_set_parent __parent_inventory_collections) @complete = complete.nil? ? true : complete @update_only = update_only.nil? ? false : update_only @create_only = create_only.nil? ? false : create_only @default_values = default_values @name = name || association || model_class.to_s.demodulize.tableize @saver_strategy = process_saver_strategy(saver_strategy) @use_ar_object = use_ar_object || false @batch_extra_attributes = batch_extra_attributes @assert_graph_integrity = assert_graph_integrity.nil? ? false : assert_graph_integrity @manager_ref_allowed_nil = manager_ref_allowed_nil || [] # Targeted mode related attributes # TODO(lsmola) Should we refactor this to use references too? @all_manager_uuids = all_manager_uuids @parent_inventory_collections = parent_inventory_collections @targeted_arel = targeted_arel @targeted = !!targeted @inventory_object_attributes = inventory_object_attributes @saved ||= false @attributes_blacklist = Set.new @attributes_whitelist = Set.new @transitive_dependency_attributes = Set.new @dependees = Set.new @data_storage = ::InventoryRefresh::InventoryCollection::DataStorage.new(self, secondary_refs) @references_storage = ::InventoryRefresh::InventoryCollection::ReferencesStorage.new(index_proxy) @targeted_scope = ::InventoryRefresh::InventoryCollection::ReferencesStorage.new(index_proxy).merge!(manager_uuids) @created_records = [] @updated_records = [] @deleted_records = [] blacklist_attributes!(attributes_blacklist) if attributes_blacklist.present? whitelist_attributes!(attributes_whitelist) if attributes_whitelist.present? end |
Instance Attribute Details
#all_manager_uuids ⇒ Array?
If present, InventoryCollection switches into delete_complement mode, where it will delete every record from the DB, that is not present in this list. This is used for the batch processing, where we don’t know which InventoryObject should be deleted, but we know all manager_uuids of all InventoryObject objects that exists in the provider.
75 76 77 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 75 def all_manager_uuids @all_manager_uuids end |
#arel ⇒ Object (readonly)
Returns the value of attribute arel.
84 85 86 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 84 def arel @arel end |
#assert_graph_integrity ⇒ Object (readonly)
Returns the value of attribute assert_graph_integrity.
84 85 86 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 84 def assert_graph_integrity @assert_graph_integrity end |
#association ⇒ Object (readonly)
Returns the value of attribute association.
84 85 86 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 84 def association @association end |
#attributes_blacklist ⇒ Object
Returns the value of attribute attributes_blacklist.
84 85 86 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 84 def attributes_blacklist @attributes_blacklist end |
#attributes_whitelist ⇒ Object
Returns the value of attribute attributes_whitelist.
84 85 86 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 84 def attributes_whitelist @attributes_whitelist end |
#batch_extra_attributes ⇒ Object (readonly)
Returns the value of attribute batch_extra_attributes.
84 85 86 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 84 def batch_extra_attributes @batch_extra_attributes end |
#check_changed ⇒ Object (readonly)
Returns the value of attribute check_changed.
84 85 86 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 84 def check_changed @check_changed end |
#complete ⇒ Object (readonly)
Returns the value of attribute complete.
84 85 86 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 84 def complete @complete end |
#create_only ⇒ Object (readonly)
Returns the value of attribute create_only.
84 85 86 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 84 def create_only @create_only end |
#created_records ⇒ Object (readonly)
Returns the value of attribute created_records.
84 85 86 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 84 def created_records @created_records end |
#custom_reconnect_block ⇒ Object (readonly)
Returns the value of attribute custom_reconnect_block.
84 85 86 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 84 def custom_reconnect_block @custom_reconnect_block end |
#custom_save_block ⇒ Object (readonly)
Returns the value of attribute custom_save_block.
84 85 86 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 84 def custom_save_block @custom_save_block end |
#data_collection_finalized ⇒ Boolean
Returns A true value marks that we collected all the data of the InventoryCollection, meaning we also collected all the references.
58 59 60 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 58 def data_collection_finalized @data_collection_finalized end |
#data_storage ⇒ InventoryRefresh::InventoryCollection::DataStorage
Returns An InventoryCollection encapsulating all data with indexes.
62 63 64 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 62 def data_storage @data_storage end |
#default_values ⇒ Object (readonly)
Returns the value of attribute default_values.
84 85 86 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 84 def default_values @default_values end |
#delete_method ⇒ Object (readonly)
Returns the value of attribute delete_method.
84 85 86 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 84 def delete_method @delete_method end |
#deleted_records ⇒ Object (readonly)
Returns the value of attribute deleted_records.
84 85 86 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 84 def deleted_records @deleted_records end |
#dependees ⇒ Set
Returns A set of InventoryCollection objects that depends on this InventoryCollection object.
78 79 80 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 78 def dependees @dependees end |
#dependency_attributes ⇒ Object (readonly)
Returns the value of attribute dependency_attributes.
84 85 86 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 84 def dependency_attributes @dependency_attributes end |
#internal_attributes ⇒ Object (readonly)
Returns the value of attribute internal_attributes.
84 85 86 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 84 def internal_attributes @internal_attributes end |
#inventory_object_attributes ⇒ Object (readonly)
Returns the value of attribute inventory_object_attributes.
84 85 86 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 84 def inventory_object_attributes @inventory_object_attributes end |
#manager_ref ⇒ Object (readonly)
Returns the value of attribute manager_ref.
84 85 86 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 84 def manager_ref @manager_ref end |
#manager_ref_allowed_nil ⇒ Object (readonly)
Returns the value of attribute manager_ref_allowed_nil.
84 85 86 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 84 def manager_ref_allowed_nil @manager_ref_allowed_nil end |
#model_class ⇒ Object (readonly)
Returns the value of attribute model_class.
84 85 86 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 84 def model_class @model_class end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
84 85 86 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 84 def name @name end |
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
84 85 86 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 84 def parent @parent end |
#parent_inventory_collections ⇒ Array<Symbol>
Returns @see #parent_inventory_collections documentation of InventoryCollection.new kwargs parameters.
82 83 84 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 82 def parent_inventory_collections @parent_inventory_collections end |
#references_storage ⇒ Object (readonly)
Returns the value of attribute references_storage.
84 85 86 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 84 def references_storage @references_storage end |
#saved ⇒ Boolean
Returns true if this collection is already saved into the DB. E.g. InventoryCollections with DB only strategy are marked as saved. This causes InventoryCollection not being a dependency for any other InventoryCollection, since it is already persisted into the DB.
67 68 69 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 67 def saved @saved end |
#saver_strategy ⇒ Object (readonly)
Returns the value of attribute saver_strategy.
84 85 86 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 84 def saver_strategy @saver_strategy end |
#strategy ⇒ Object (readonly)
Returns the value of attribute strategy.
84 85 86 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 84 def strategy @strategy end |
#targeted ⇒ Object (readonly)
Returns the value of attribute targeted.
84 85 86 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 84 def targeted @targeted end |
#targeted_arel ⇒ Object (readonly)
Returns the value of attribute targeted_arel.
84 85 86 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 84 def targeted_arel @targeted_arel end |
#targeted_scope ⇒ Object (readonly)
Returns the value of attribute targeted_scope.
84 85 86 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 84 def targeted_scope @targeted_scope end |
#transitive_dependency_attributes ⇒ Object (readonly)
Returns the value of attribute transitive_dependency_attributes.
84 85 86 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 84 def transitive_dependency_attributes @transitive_dependency_attributes end |
#update_only ⇒ Object (readonly)
Returns the value of attribute update_only.
84 85 86 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 84 def update_only @update_only end |
#updated_records ⇒ Object (readonly)
Returns the value of attribute updated_records.
84 85 86 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 84 def updated_records @updated_records end |
#use_ar_object ⇒ Object (readonly)
Returns the value of attribute use_ar_object.
84 85 86 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 84 def use_ar_object @use_ar_object end |
Instance Method Details
#association_to_base_class_mapping ⇒ Hash{Symbol => String}
Returns Hash with association name mapped to base class of the association.
946 947 948 949 950 951 952 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 946 def association_to_base_class_mapping return {} unless model_class @association_to_base_class_mapping ||= model_class.reflect_on_all_associations.each_with_object({}) do |x, obj| obj[x.name] = x.klass.base_class.name unless x.polymorphic? end end |
#association_to_foreign_key_mapping ⇒ Hash{Symbol => String}
Returns Hash with association name mapped to foreign key column name.
910 911 912 913 914 915 916 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 910 def association_to_foreign_key_mapping return {} unless model_class @association_to_foreign_key_mapping ||= belongs_to_associations.each_with_object({}) do |x, obj| obj[x.name] = x.foreign_key end end |
#association_to_foreign_type_mapping ⇒ Hash{Symbol => String}
Returns Hash with association name mapped to polymorphic foreign key type column name.
928 929 930 931 932 933 934 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 928 def association_to_foreign_type_mapping return {} unless model_class @association_to_foreign_type_mapping ||= model_class.reflect_on_all_associations.each_with_object({}) do |x, obj| obj[x.name] = x.foreign_type if x.polymorphic? end end |
#base_class_name ⇒ String
Returns Base class name of the model_class of this InventoryCollection.
976 977 978 979 980 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 976 def base_class_name return "" unless model_class @base_class_name ||= model_class.base_class.name end |
#base_columns ⇒ Object
727 728 729 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 727 def base_columns @base_columns ||= unique_index_columns + internal_columns end |
#batch_size ⇒ Integer
Returns default batch size for talking to the DB.
1000 1001 1002 1003 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 1000 def batch_size # TODO(lsmola) mode to the settings 1000 end |
#batch_size_pure_sql ⇒ Integer
Returns default batch size for talking to the DB if not using ApplicationRecord objects.
1006 1007 1008 1009 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 1006 def batch_size_pure_sql # TODO(lsmola) mode to the settings 10_000 end |
#belongs_to_associations ⇒ Array<ActiveRecord::Reflection::BelongsToReflection">
Returns All belongs_to associations.
905 906 907 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 905 def belongs_to_associations model_class.reflect_on_all_associations.select { |x| x.kind_of?(ActiveRecord::Reflection::BelongsToReflection) } end |
#blacklist_attributes!(attributes) ⇒ Array<Symbol>
Add passed attributes to blacklist. The manager_ref attributes cannot be blacklisted, otherwise we will not be able to identify the inventory_object. We do not automatically remove attributes causing fixed dependencies, so beware that without them, you won’t be able to create the record.
869 870 871 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 869 def blacklist_attributes!(attributes) self.attributes_blacklist += attributes - (fixed_attributes + internal_attributes) end |
#build_multi_selection_condition(hashes, keys = manager_ref) ⇒ String
Builds a multiselection conditions like (table1.a = a1 AND table2.b = b1) OR (table1.a = a2 AND table2.b = b2)
1026 1027 1028 1029 1030 1031 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 1026 def build_multi_selection_condition(hashes, keys = manager_ref) arel_table = model_class.arel_table # We do pure SQL OR, since Arel is nesting every .or into another parentheses, otherwise this would be just # inject(:or) instead of to_sql with .join(" OR ") hashes.map { |hash| "(#{keys.map { |key| arel_table[key].eq(hash[key]) }.inject(:and).to_sql})" }.join(" OR ") end |
#check_changed? ⇒ Boolean
Returns true means we want to call .changed? on every ActiveRecord object before saving it.
537 538 539 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 537 def check_changed? check_changed end |
#clone ⇒ InventoryCollection
Returns a shallow copy of InventoryCollection, the copy will share data_storage of the original collection, otherwise we would be copying a lot of records in memory.
885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 885 def clone cloned = self.class.new(:model_class => model_class, :manager_ref => manager_ref, :association => association, :parent => parent, :arel => arel, :strategy => strategy, :saver_strategy => saver_strategy, :custom_save_block => custom_save_block, # We want cloned IC to be update only, since this is used for cycle resolution :update_only => true, # Dependency attributes need to be a hard copy, since those will differ for each # InventoryCollection :dependency_attributes => dependency_attributes.clone) cloned.data_storage = data_storage cloned end |
#complete? ⇒ Boolean
Returns true means the data is not complete, leading to only creating and updating data.
548 549 550 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 548 def complete? complete end |
#create_allowed? ⇒ Boolean
Returns true means we will delete/soft-delete data.
563 564 565 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 563 def create_allowed? !update_only? end |
#create_only? ⇒ Boolean
Returns true means that only create of new data is allowed.
568 569 570 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 568 def create_only? create_only end |
#data_collection_finalized? ⇒ Boolean
Returns true if no more data will be added to this InventoryCollection object, that usually happens after the parsing step is finished.
733 734 735 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 733 def data_collection_finalized? data_collection_finalized end |
#db_collection_for_comparison ⇒ ActiveRecord::Relation
Returns A relation that can fetch all data of this InventoryCollection from the DB.
1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 1034 def db_collection_for_comparison if targeted? if targeted_arel.respond_to?(:call) targeted_arel.call(self) elsif parent_inventory_collections.present? targeted_arel_default else targeted_iterator_for(targeted_scope.primary_references) end else full_collection_for_comparison end end |
#db_collection_for_comparison_for(references) ⇒ ActiveRecord::Relation
Builds an ActiveRecord::Relation that can fetch all the references from the DB
1101 1102 1103 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 1101 def db_collection_for_comparison_for(references) full_collection_for_comparison.where(targeted_selection_for(references)) end |
#db_collection_for_comparison_for_complement_of(manager_uuids_set) ⇒ ActiveRecord::Relation
Builds an ActiveRecord::Relation that can fetch complement of all the references from the DB
1109 1110 1111 1112 1113 1114 1115 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 1109 def db_collection_for_comparison_for_complement_of(manager_uuids_set) # TODO(lsmola) this should have the build_multi_selection_condition, like in the method above # TODO(lsmola) this query will be highly ineffective, we will try approach with updating a timestamp of all # records, then we can get list of all records that were not update. That would be equivalent to result of this # more effective query and without need of all manager_uuids full_collection_for_comparison.where.not(manager_ref.first => manager_uuids_set) end |
#delete_allowed? ⇒ Boolean
Returns true means we will delete/soft-delete data.
558 559 560 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 558 def delete_allowed? complete? && !update_only? end |
#dependencies ⇒ Array<InventoryRefresh::InventoryCollection>
Returns all unique non saved dependencies.
846 847 848 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 846 def dependencies filtered_dependency_attributes.values.map(&:to_a).flatten.uniq.reject(&:saved?) end |
#dependency_attributes_for(inventory_collections) ⇒ Array<InventoryRefresh::InventoryCollection>
Returns what attributes are causing a dependencies to certain InventoryCollection objects.
855 856 857 858 859 860 861 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 855 def dependency_attributes_for(inventory_collections) attributes = Set.new inventory_collections.each do |inventory_collection| attributes += filtered_dependency_attributes.select { |_key, value| value.include?(inventory_collection) }.keys end attributes end |
#filtered_dependency_attributes ⇒ Hash{Symbol => Set}
List attributes causing a dependency and filters them by attributes_blacklist and attributes_whitelist
805 806 807 808 809 810 811 812 813 814 815 816 817 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 805 def filtered_dependency_attributes filtered_attributes = dependency_attributes if attributes_blacklist.present? filtered_attributes = filtered_attributes.reject { |key, _value| attributes_blacklist.include?(key) } end if attributes_whitelist.present? filtered_attributes = filtered_attributes.select { |key, _value| attributes_whitelist.include?(key) } end filtered_attributes end |
#fixed_attributes ⇒ Array<Symbol>
Attributes that are needed to be able to save the record, i.e. attributes that are part of the unique index and attributes with presence validation or NOT NULL constraint
823 824 825 826 827 828 829 830 831 832 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 823 def fixed_attributes if model_class presence_validators = model_class.validators.detect { |x| x.kind_of?(ActiveRecord::Validations::PresenceValidator) } end # Attributes that has to be always on the entity, so attributes making unique index of the record + attributes # that have presence validation fixed_attributes = manager_ref fixed_attributes += presence_validators.attributes if presence_validators.present? fixed_attributes end |
#fixed_dependencies ⇒ Object
Returns fixed dependencies, which are the ones we can’t move, because we wouldn’t be able to save the data
837 838 839 840 841 842 843 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 837 def fixed_dependencies fixed_attrs = fixed_attributes filtered_dependency_attributes.each_with_object(Set.new) do |(key, value), fixed_deps| fixed_deps.merge(value) if fixed_attrs.include?(key) end.reject(&:saved?) end |
#fixed_foreign_keys ⇒ Array<Symbol>
Returns List of all column names that are foreign keys and cannot removed, otherwise we couldn’t save the record.
963 964 965 966 967 968 969 970 971 972 973 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 963 def fixed_foreign_keys # Foreign keys that are part of a manager_ref must be present, otherwise the record would get lost. This is a # minimum check we can do to not break a referential integrity. return @fixed_foreign_keys_cache unless @fixed_foreign_keys_cache.nil? manager_ref_set = (manager_ref - manager_ref_allowed_nil) @fixed_foreign_keys_cache = manager_ref_set.map { |x| association_to_foreign_key_mapping[x] }.compact @fixed_foreign_keys_cache += foreign_keys & manager_ref @fixed_foreign_keys_cache.map!(&:to_sym) @fixed_foreign_keys_cache end |
#foreign_key_to_association_mapping ⇒ Hash{String => Hash}
Returns Hash with foreign_key column name mapped to association name.
919 920 921 922 923 924 925 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 919 def foreign_key_to_association_mapping return {} unless model_class @foreign_key_to_association_mapping ||= belongs_to_associations.each_with_object({}) do |x, obj| obj[x.foreign_key] = x.name end end |
#foreign_keys ⇒ Array<Symbol>
Returns List of all column names that are foreign keys.
955 956 957 958 959 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 955 def foreign_keys return [] unless model_class @foreign_keys_cache ||= belongs_to_associations.map(&:foreign_key).map!(&:to_sym) end |
#foreign_type_to_association_mapping ⇒ Hash{Symbol => String}
Returns Hash with polymorphic foreign key type column name mapped to association name.
937 938 939 940 941 942 943 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 937 def foreign_type_to_association_mapping return {} unless model_class @foreign_type_to_association_mapping ||= model_class.reflect_on_all_associations.each_with_object({}) do |x, obj| obj[x.foreign_type] = x.name if x.polymorphic? end end |
#full_collection_for_comparison ⇒ ActiveRecord::Relation
Returns relation that can fetch all the references from the DB.
1118 1119 1120 1121 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 1118 def full_collection_for_comparison return arel unless arel.nil? parent.send(association) end |
#inspect ⇒ String
Returns a concise form of the InventoryCollection for easy logging.
995 996 997 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 995 def inspect to_s end |
#internal_columns ⇒ Object
701 702 703 704 705 706 707 708 709 710 711 712 713 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 701 def internal_columns return @internal_columns if @internal_columns @internal_columns = [] + @internal_columns << :type if supports_sti? @internal_columns << :resource_timestamps_max if @internal_columns << :resource_timestamps if @internal_columns << :resource_timestamp if @internal_columns << :resource_versions_max if supports_resource_versions_max? @internal_columns << :resource_versions if supports_resource_versions? @internal_columns << :resource_version if supports_resource_version? @internal_columns end |
#internal_timestamp_columns ⇒ Object
715 716 717 718 719 720 721 722 723 724 725 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 715 def return if = [] << :created_on if supports_created_on? << :created_at if supports_created_at? << :updated_on if supports_updated_on? << :updated_at if supports_updated_at? end |
#inventory_object?(value) ⇒ Boolean
Returns true is value is kind of InventoryRefresh::InventoryObject.
739 740 741 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 739 def inventory_object?(value) value.kind_of?(::InventoryRefresh::InventoryObject) end |
#inventory_object_lazy?(value) ⇒ Boolean
Returns true is value is kind of InventoryRefresh::InventoryObjectLazy.
745 746 747 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 745 def inventory_object_lazy?(value) value.kind_of?(::InventoryRefresh::InventoryObjectLazy) end |
#manager_ref_to_cols ⇒ Array<String>
Convert manager_ref list of attributes to list of DB columns
794 795 796 797 798 799 800 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 794 def manager_ref_to_cols # TODO(lsmola) this should contain the polymorphic _type, otherwise the IC with polymorphic unique key will get # conflicts manager_ref.map do |ref| association_to_foreign_key_mapping[ref] || ref end end |
#manager_uuids ⇒ Array<String>
Returns a list of stringified uuids of all scoped InventoryObjects, which is used for scoping in targeted mode
1014 1015 1016 1017 1018 1019 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 1014 def manager_uuids # TODO(lsmola) LEGACY: this is still being used by :targetel_arel definitions and it expects array of strings raise "This works only for :manager_ref size 1" if manager_ref.size > 1 key = manager_ref.first transform_references_to_hashes(targeted_scope.primary_references).map { |x| x[key] } end |
#new_inventory_object(hash) ⇒ InventoryRefresh::InventoryObject
Creates InventoryRefresh::InventoryObject object from passed hash data
1127 1128 1129 1130 1131 1132 1133 1134 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 1127 def new_inventory_object(hash) manager_ref.each do |x| # TODO(lsmola) with some effort, we can do this, but it's complex raise "A lazy_find with a :key can't be a part of the manager_uuid" if inventory_object_lazy?(hash[x]) && hash[x].key end inventory_object_class.new(self, hash) end |
#noop? ⇒ Boolean
True if processing of this InventoryCollection object would lead to no operations. Then we use this marker to stop processing of the InventoryCollector object very soon, to avoid a lot of unnecessary Db queries, etc.
763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 763 def noop? # If this InventoryCollection doesn't do anything. it can easily happen for targeted/batched strategies. if targeted? if parent_inventory_collections.nil? && targeted_scope.primary_references.blank? && all_manager_uuids.nil? && parent_inventory_collections.blank? && custom_save_block.nil? && skeletal_primary_index.blank? # It's a noop Parent targeted InventoryCollection true elsif !parent_inventory_collections.nil? && parent_inventory_collections.all? { |x| x.targeted_scope.primary_references.blank? } && skeletal_primary_index.blank? # It's a noop Child targeted InventoryCollection true else false end elsif data.blank? && !delete_allowed? && skeletal_primary_index.blank? # If we have no data to save and delete is not allowed, we can just skip true else false end end |
#object_index_with_keys(keys, record) ⇒ String
Builds string uuid from passed Object and keys
754 755 756 757 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 754 def object_index_with_keys(keys, record) # TODO(lsmola) remove, last usage is in k8s reconnect logic build_stringified_reference_for_record(record, keys) end |
#parallel_safe? ⇒ Boolean
Returns true if we are using a saver strategy that allows saving in parallel processes.
583 584 585 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 583 def parallel_safe? @parallel_safe_cache ||= i(concurrent_safe concurrent_safe_batch).include?(saver_strategy) end |
#process_saver_strategy(saver_strategy) ⇒ Symbol
Processes passed saver strategy
499 500 501 502 503 504 505 506 507 508 509 510 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 499 def process_saver_strategy(saver_strategy) return :default unless saver_strategy saver_strategy = saver_strategy.to_sym case saver_strategy when :default, :batch, :concurrent_safe, :concurrent_safe_batch saver_strategy else raise "Unknown InventoryCollection saver strategy: :#{saver_strategy}, allowed strategies are "\ ":default, :batch, :concurrent_safe and :concurrent_safe_batch" end end |
#process_strategy(strategy_name) ⇒ Symbol
Processes passed strategy, modifies :data_collection_finalized and :saved attributes for db only strategies
516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 516 def process_strategy(strategy_name) self.data_collection_finalized = false return unless strategy_name strategy_name = strategy_name.to_sym case strategy_name when :local_db_cache_all self.data_collection_finalized = true self.saved = true when :local_db_find_references self.saved = true when :local_db_find_missing_references else raise "Unknown InventoryCollection strategy: :#{strategy_name}, allowed strategies are :local_db_cache_all, "\ ":local_db_find_references and :local_db_find_missing_references." end strategy_name end |
#saveable? ⇒ Boolean
Returns true if all dependencies have all data persisted.
578 579 580 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 578 def saveable? dependencies.all?(&:saved?) end |
#saved? ⇒ Boolean
Returns true if the whole InventoryCollection object has all data persisted.
573 574 575 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 573 def saved? saved end |
#store_created_records(records) ⇒ Object
Caches what records were created, for later use, e.g. post provision behavior
477 478 479 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 477 def store_created_records(records) @created_records.concat(records_identities(records)) end |
#store_deleted_records(records) ⇒ Object
Caches what records were deleted/soft-deleted, for later use, e.g. post provision behavior
491 492 493 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 491 def store_deleted_records(records) @deleted_records.concat(records_identities(records)) end |
#store_updated_records(records) ⇒ Object
Caches what records were updated, for later use, e.g. post provision behavior
484 485 486 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 484 def store_updated_records(records) @updated_records.concat(records_identities(records)) end |
#supports_created_at? ⇒ Boolean
Returns true if the model_class has created_at column.
610 611 612 613 614 615 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 610 def supports_created_at? if @supports_created_at_cache.nil? @supports_created_at_cache = (model_class.column_names.include?("created_at") && ActiveRecord::Base.) end @supports_created_at_cache end |
#supports_created_on? ⇒ Boolean
Returns true if the model_class has created_on column.
594 595 596 597 598 599 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 594 def supports_created_on? if @supports_created_on_cache.nil? @supports_created_on_cache = (model_class.column_names.include?("created_on") && ActiveRecord::Base.) end @supports_created_on_cache end |
#supports_resource_timestamp? ⇒ Boolean
Returns true if the model_class has resource_timestamp column.
636 637 638 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 636 def ||= model_class.column_names.include?("resource_timestamp") end |
#supports_resource_timestamps? ⇒ Boolean
Returns true if the model_class has resource_timestamps column.
631 632 633 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 631 def ||= model_class.column_names.include?("resource_timestamps") end |
#supports_resource_timestamps_max? ⇒ Boolean
Returns true if the model_class has resource_timestamps_max column.
626 627 628 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 626 def ||= model_class.column_names.include?("resource_timestamps_max") end |
#supports_resource_version? ⇒ Boolean
Returns true if the model_class has resource_version column.
651 652 653 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 651 def supports_resource_version? @supports_resource_version_cache ||= model_class.column_names.include?("resource_version") end |
#supports_resource_versions? ⇒ Boolean
Returns true if the model_class has resource_versions column.
646 647 648 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 646 def supports_resource_versions? @supports_resource_versions_cache ||= model_class.column_names.include?("resource_versions") end |
#supports_resource_versions_max? ⇒ Boolean
Returns true if the model_class has resource_versions_max column.
641 642 643 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 641 def supports_resource_versions_max? @supports_resource_versions_max_cache ||= model_class.column_names.include?("resource_versions_max") end |
#supports_sti? ⇒ Boolean
Returns true if the model_class supports STI.
588 589 590 591 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 588 def supports_sti? @supports_sti_cache = model_class.column_names.include?("type") if @supports_sti_cache.nil? @supports_sti_cache end |
#supports_updated_at? ⇒ Boolean
Returns true if the model_class has updated_at column.
618 619 620 621 622 623 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 618 def supports_updated_at? if @supports_updated_at_cache.nil? @supports_updated_at_cache = (model_class.column_names.include?("updated_at") && ActiveRecord::Base.) end @supports_updated_at_cache end |
#supports_updated_on? ⇒ Boolean
Returns true if the model_class has updated_on column.
602 603 604 605 606 607 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 602 def supports_updated_on? if @supports_updated_on_cache.nil? @supports_updated_on_cache = (model_class.column_names.include?("updated_on") && ActiveRecord::Base.) end @supports_updated_on_cache end |
#targeted? ⇒ Boolean
Returns true is processing of this InventoryCollection will be in targeted mode.
787 788 789 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 787 def targeted? targeted end |
#targeted_arel_default ⇒ InventoryRefresh::ApplicationRecordIterator
Builds targeted query limiting the results by the :references defined in parent_inventory_collections
1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 1051 def targeted_arel_default if parent_inventory_collections.collect { |x| x.model_class.base_class }.uniq.count > 1 raise "Multiple :parent_inventory_collections with different base class are not supported by default. Write "\ ":targeted_arel manually, or separate [#{self}] into 2 InventoryCollection objects." end parent_collection = parent_inventory_collections.first references = parent_inventory_collections.map { |x| x.targeted_scope.primary_references }.reduce({}, :merge!) parent_collection.targeted_iterator_for(references, full_collection_for_comparison) end |
#targeted_iterator_for(references, query = nil) ⇒ InventoryRefresh::ApplicationRecordIterator
Returns iterator for the passed references and a query
1089 1090 1091 1092 1093 1094 1095 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 1089 def targeted_iterator_for(references, query = nil) InventoryRefresh::ApplicationRecordIterator.new( :inventory_collection => self, :manager_uuids_set => references, :query => query ) end |
#targeted_selection_for(references) ⇒ String
Builds a multiselection conditions like (table1.a = a1 AND table2.b = b1) OR (table1.a = a2 AND table2.b = b2) for passed references
1080 1081 1082 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 1080 def targeted_selection_for(references) build_multi_selection_condition(transform_references_to_hashes(references)) end |
#to_s ⇒ String
Returns a concise form of the inventoryCollection for easy logging.
983 984 985 986 987 988 989 990 991 992 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 983 def to_s whitelist = ", whitelist: [#{attributes_whitelist.to_a.join(", ")}]" if attributes_whitelist.present? blacklist = ", blacklist: [#{attributes_blacklist.to_a.join(", ")}]" if attributes_blacklist.present? strategy_name = ", strategy: #{strategy}" if strategy name = model_class || association "InventoryCollection:<#{name}>#{whitelist}#{blacklist}#{strategy_name}" end |
#transform_references_to_hashes(references) ⇒ Array<Hash>
Gets targeted references and transforms them into list of hashes
1066 1067 1068 1069 1070 1071 1072 1073 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 1066 def transform_references_to_hashes(references) if references.kind_of?(Array) # Sliced InventoryRefresh::Inventorycollection::TargetedScope references.map { |x| x.second.full_reference } else references.values.map(&:full_reference) end end |
#unique_index_columns ⇒ Array<Symbol>
Returns all columns that are part of the best fit unique index.
656 657 658 659 660 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 656 def unique_index_columns return @unique_index_columns if @unique_index_columns @unique_index_columns = unique_index_for(unique_index_keys).columns.map(&:to_sym) end |
#unique_index_for(keys) ⇒ ActiveRecord::ConnectionAdapters::IndexDefinition
Finds an index that fits the list of columns (keys) the best
685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 685 def unique_index_for(keys) @unique_index_for_keys_cache ||= {} @unique_index_for_keys_cache[keys] if @unique_index_for_keys_cache[keys] # Find all uniq indexes that that are covering our keys uniq_key_candidates = unique_indexes.each_with_object([]) { |i, obj| obj << i if (keys - i.columns.map(&:to_sym)).empty? } if @unique_indexes_cache.blank? raise "#{self} and its table #{model_class.table_name} must have a unique index defined "\ "covering columns #{keys} to be able to use saver_strategy :concurrent_safe or :concurrent_safe_batch." end # Take the uniq key having the least number of columns @unique_index_for_keys_cache[keys] = uniq_key_candidates.min_by { |x| x.columns.count } end |
#unique_index_keys ⇒ Object
662 663 664 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 662 def unique_index_keys @unique_index_keys ||= manager_ref_to_cols.map(&:to_sym) end |
#unique_indexes ⇒ Array<ActiveRecord::ConnectionAdapters::IndexDefinition>
Returns array of all unique indexes known to model.
667 668 669 670 671 672 673 674 675 676 677 678 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 667 def unique_indexes @unique_indexes_cache if @unique_indexes_cache @unique_indexes_cache = model_class.connection.indexes(model_class.table_name).select(&:unique) if @unique_indexes_cache.blank? raise "#{self} and its table #{model_class.table_name} must have a unique index defined, to"\ " be able to use saver_strategy :concurrent_safe or :concurrent_safe_batch." end @unique_indexes_cache end |
#update_only? ⇒ Boolean
Returns true means we want to only update data.
553 554 555 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 553 def update_only? update_only end |
#use_ar_object? ⇒ Boolean
Returns true means we want to use ActiveRecord object for writing attributes and we want to perform casting on all columns.
543 544 545 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 543 def use_ar_object? use_ar_object end |
#whitelist_attributes!(attributes) ⇒ Array<Symbol>
Add passed attributes to whitelist. The manager_ref attributes always needs to be in the white list, otherwise we will not be able to identify theinventory_object. We do not automatically add attributes causing fixed dependencies, so beware that without them, you won’t be able to create the record.
879 880 881 |
# File 'lib/inventory_refresh/inventory_collection.rb', line 879 def whitelist_attributes!(attributes) self.attributes_whitelist += attributes + (fixed_attributes + internal_attributes) end |