Class: InventoryRefresh::InventoryObjectLazy
- Inherits:
-
Object
- Object
- InventoryRefresh::InventoryObjectLazy
- Defined in:
- lib/inventory_refresh/inventory_object_lazy.rb
Instance Attribute Summary collapse
-
#default ⇒ Object
readonly
Returns the value of attribute default.
-
#inventory_collection ⇒ Object
readonly
Returns the value of attribute inventory_collection.
-
#key ⇒ Object
readonly
Returns the value of attribute key.
-
#reference ⇒ Object
readonly
Returns the value of attribute reference.
-
#transform_nested_lazy_finds ⇒ Object
readonly
Returns the value of attribute transform_nested_lazy_finds.
Instance Method Summary collapse
-
#association?(key) ⇒ Boolean
True if the key is an association on inventory_collection_scope model class.
-
#dependency? ⇒ Boolean
return [Boolean] true if the Lazy object is causing a dependency, Lazy link is always a dependency if no :key is provider or if it’s transitive_dependency.
-
#initialize(inventory_collection, index_data, ref: :manager_ref, key: nil, default: nil, transform_nested_lazy_finds: false) ⇒ InventoryObjectLazy
constructor
A new instance of InventoryObjectLazy.
-
#inspect ⇒ String
String format for nice logging.
-
#load(inventory_object = nil, inventory_object_key = nil) ⇒ InventoryRefresh::InventoryObject, Object
InventoryRefresh::InventoryObject instance or an attribute on key.
-
#to_s ⇒ String
Stringified reference.
- #transform_nested_secondary_indexes!(depth = 0) ⇒ Object
-
#transitive_dependency? ⇒ Boolean
return [Boolean] true if the Lazy object is causing a transitive dependency, which happens if the :key points to an attribute that is causing a dependency.
Constructor Details
#initialize(inventory_collection, index_data, ref: :manager_ref, key: nil, default: nil, transform_nested_lazy_finds: false) ⇒ InventoryObjectLazy
Returns a new instance of InventoryObjectLazy.
18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/inventory_refresh/inventory_object_lazy.rb', line 18 def initialize(inventory_collection, index_data, ref: :manager_ref, key: nil, default: nil, transform_nested_lazy_finds: false) @inventory_collection = inventory_collection @reference = inventory_collection.build_reference(index_data, ref) @key = key @default = default @transform_nested_lazy_finds = transform_nested_lazy_finds # We do not support skeletal pre-create for :key, since :key will not be available, we want to use local_db_find # instead. skeletal_precreate! unless @key end |
Instance Attribute Details
#default ⇒ Object (readonly)
Returns the value of attribute default.
5 6 7 |
# File 'lib/inventory_refresh/inventory_object_lazy.rb', line 5 def default @default end |
#inventory_collection ⇒ Object (readonly)
Returns the value of attribute inventory_collection.
5 6 7 |
# File 'lib/inventory_refresh/inventory_object_lazy.rb', line 5 def inventory_collection @inventory_collection end |
#key ⇒ Object (readonly)
Returns the value of attribute key.
5 6 7 |
# File 'lib/inventory_refresh/inventory_object_lazy.rb', line 5 def key @key end |
#reference ⇒ Object
Returns the value of attribute reference.
5 6 7 |
# File 'lib/inventory_refresh/inventory_object_lazy.rb', line 5 def reference @reference end |
#transform_nested_lazy_finds ⇒ Object (readonly)
Returns the value of attribute transform_nested_lazy_finds.
5 6 7 |
# File 'lib/inventory_refresh/inventory_object_lazy.rb', line 5 def transform_nested_lazy_finds @transform_nested_lazy_finds end |
Instance Method Details
#association?(key) ⇒ Boolean
Returns true if the key is an association on inventory_collection_scope model class.
72 73 74 75 |
# File 'lib/inventory_refresh/inventory_object_lazy.rb', line 72 def association?(key) inventory_collection.dependency_attributes.key?(key) || !inventory_collection.association_to_foreign_key_mapping[key].nil? end |
#dependency? ⇒ Boolean
return [Boolean] true if the Lazy object is causing a dependency, Lazy link is always a dependency if no :key
is provider or if it's transitive_dependency
56 57 58 59 60 |
# File 'lib/inventory_refresh/inventory_object_lazy.rb', line 56 def dependency? # If key is not set, InventoryObjectLazy is a dependency, cause it points to the record itself. Otherwise # InventoryObjectLazy is a dependency only if it points to an attribute which is a dependency or a relation. !key || transitive_dependency? end |
#inspect ⇒ String
Returns string format for nice logging.
37 38 39 40 41 42 |
# File 'lib/inventory_refresh/inventory_object_lazy.rb', line 37 def inspect suffix = "" suffix += ", ref: #{ref}" if ref.present? suffix += ", key: #{key}" if key.present? "InventoryObjectLazy:('#{self}', #{inventory_collection}#{suffix})" end |
#load(inventory_object = nil, inventory_object_key = nil) ⇒ InventoryRefresh::InventoryObject, Object
Returns InventoryRefresh::InventoryObject instance or an attribute on key.
48 49 50 51 52 |
# File 'lib/inventory_refresh/inventory_object_lazy.rb', line 48 def load(inventory_object = nil, inventory_object_key = nil) transform_nested_secondary_indexes! if transform_nested_lazy_finds && nested_secondary_index? load_object(inventory_object, inventory_object_key) end |
#to_s ⇒ String
Returns stringified reference.
32 33 34 |
# File 'lib/inventory_refresh/inventory_object_lazy.rb', line 32 def to_s stringified_reference end |
#transform_nested_secondary_indexes!(depth = 0) ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/inventory_refresh/inventory_object_lazy.rb', line 77 def transform_nested_secondary_indexes!(depth = 0) raise "Nested references are too deep!" if depth > 20 keys.each do |x| attr = full_reference[x] next unless attr.kind_of?(InventoryRefresh::InventoryObjectLazy) next if attr.primary? if attr.nested_secondary_index? attr.transform_nested_secondary_indexes!(depth + 1) end full_reference[x] = full_reference[x].load end # Rebuild the reference to get the right value self.reference = inventory_collection.build_reference(full_reference, ref) end |
#transitive_dependency? ⇒ Boolean
return [Boolean] true if the Lazy object is causing a transitive dependency, which happens if the :key points
to an attribute that is causing a dependency.
64 65 66 67 68 69 |
# File 'lib/inventory_refresh/inventory_object_lazy.rb', line 64 def transitive_dependency? # If the dependency is inventory_collection.lazy_find(:ems_ref, :key => :stack) # and a :stack is a relation to another object, in the InventoryObject object, # then this relation is considered transitive. key && association?(key) end |