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

Returns a new instance of InventoryObjectLazy.

Parameters:

  • inventory_collection (InventoryRefresh::InventoryCollection)

    InventoryCollection object owning the InventoryObject

  • index_data (Hash)

    data of the InventoryObject object

  • ref (Symbol) (defaults to: :manager_ref)

    reference name

  • key (Symbol) (defaults to: nil)

    key name, will be used to fetch attribute from resolved InventoryObject

  • default (Object) (defaults to: nil)

    a default value used if the :key will resolve to nil

  • transform_nested_lazy_finds (Boolean) (defaults to: false)

    True if we want to convert all lazy objects in InventoryObject objects and reset the Reference. TODO(lsmola) we should be able to do this automatically, then we can remove this option



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

Returns true if the key is an association on inventory_collection_scope model class.

Returns:

  • (Boolean)

    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

Returns:

  • (Boolean)


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

#inspectString

Returns string format for nice logging.

Returns:

  • (String)

    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.

Parameters:

  • inventory_object (InventoryRefresh::InventoryObject) (defaults to: nil)

    InventoryObject object owning this relation

  • inventory_object_key (Symbol) (defaults to: nil)

    InventoryObject object’s attribute pointing to this relation

Returns:



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_sString

Returns stringified reference.

Returns:

  • (String)

    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.

Returns:

  • (Boolean)


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