Class: InventoryRefresh::InventoryObjectLazy

Inherits:
Object
  • Object
show all
Defined in:
lib/inventory_refresh/inventory_object_lazy.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(inventory_collection, index_data, ref: :manager_ref, key: nil, default: nil, transform_nested_lazy_finds: false) ⇒ 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

#defaultObject (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_collectionObject (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

#keyObject (readonly)

Returns the value of attribute key.



5
6
7
# File 'lib/inventory_refresh/inventory_object_lazy.rb', line 5

def key
  @key
end

#referenceObject

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_findsObject (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



73
74
75
76
77
78
79
80
# File 'lib/inventory_refresh/inventory_object_lazy.rb', line 73

def association?(key)
  # TODO(lsmola) remove this if there will be better dependency scan, probably with transitive dependencies filled
  # in a second pass, then we can get rid of this hardcoded symbols. Right now we are not able to introspect these.
  return true if [:parent, :genealogy_parent].include?(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


57
58
59
60
61
# File 'lib/inventory_refresh/inventory_object_lazy.rb', line 57

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

#inspectString



38
39
40
41
42
43
# File 'lib/inventory_refresh/inventory_object_lazy.rb', line 38

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



49
50
51
52
53
# File 'lib/inventory_refresh/inventory_object_lazy.rb', line 49

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_sString



32
33
34
35
# File 'lib/inventory_refresh/inventory_object_lazy.rb', line 32

def to_s
  # TODO(lsmola) do we need this method?
  stringified_reference
end

#transform_nested_secondary_indexes!(depth = 0) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/inventory_refresh/inventory_object_lazy.rb', line 82

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.


65
66
67
68
69
70
# File 'lib/inventory_refresh/inventory_object_lazy.rb', line 65

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