Class: InventoryRefresh::InventoryCollection::Index::Proxy

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/inventory_refresh/inventory_collection/index/proxy.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logging

#logger

Constructor Details

#initialize(inventory_collection, secondary_refs = {}) ⇒ Proxy

Returns a new instance of Proxy.

Parameters:

  • inventory_collection (InventoryRefresh::InventoryCollection)

    InventoryCollection object owning the proxy

  • secondary_refs (Hash) (defaults to: {})

    Secondary_refs in format => [:attribute1, :attribute2]



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/inventory_refresh/inventory_collection/index/proxy.rb', line 18

def initialize(inventory_collection, secondary_refs = {})
  @inventory_collection = inventory_collection

  @primary_ref    = {primary_index_ref => @inventory_collection.manager_ref}
  @secondary_refs = secondary_refs
  @all_refs       = @primary_ref.merge(@secondary_refs)

  @data_indexes     = {}
  @local_db_indexes = {}

  @all_refs.each do |index_name, attribute_names|
    @data_indexes[index_name] = InventoryRefresh::InventoryCollection::Index::Type::Data.new(
      inventory_collection,
      index_name,
      attribute_names
    )

    @local_db_indexes[index_name] = InventoryRefresh::InventoryCollection::Index::Type::LocalDb.new(
      inventory_collection,
      index_name,
      attribute_names,
      @data_indexes[index_name]
    )
  end

  @skeletal_primary_index = InventoryRefresh::InventoryCollection::Index::Type::Skeletal.new(
    inventory_collection,
    :skeletal_primary_index_ref,
    named_ref,
    primary_index
  )
end

Instance Attribute Details

#skeletal_primary_indexObject (readonly)

Returns the value of attribute skeletal_primary_index.



14
15
16
# File 'lib/inventory_refresh/inventory_collection/index/proxy.rb', line 14

def skeletal_primary_index
  @skeletal_primary_index
end

Instance Method Details

#build_primary_index_for(inventory_object) ⇒ InventoryRefresh::InventoryObject

Builds primary index for passed InventoryObject

Parameters:

Returns:



55
56
57
58
59
60
# File 'lib/inventory_refresh/inventory_collection/index/proxy.rb', line 55

def build_primary_index_for(inventory_object)
  # Building the object, we need to provide all keys of a primary index

  assert_index(inventory_object.data, primary_index_ref)
  primary_index.store_index_for(inventory_object)
end

#build_secondary_indexes_for(inventory_object) ⇒ Object



62
63
64
65
66
# File 'lib/inventory_refresh/inventory_collection/index/proxy.rb', line 62

def build_secondary_indexes_for(inventory_object)
  secondary_refs.each_key do |ref|
    data_index(ref).store_index_for(inventory_object)
  end
end

#find(reference, ref: primary_index_ref) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/inventory_refresh/inventory_collection/index/proxy.rb', line 80

def find(reference, ref: primary_index_ref)
  # TODO(lsmola) this method should return lazy too, the rest of the finders should be deprecated
  return if reference.nil?

  assert_index(reference, ref)

  reference = inventory_collection.build_reference(reference, ref)

  case strategy
  when :local_db_find_references, :local_db_cache_all
    local_db_index_find(reference)
  when :local_db_find_missing_references
    find_in_data_or_skeletal_index(reference) || local_db_index_find(reference)
  else
    find_in_data_or_skeletal_index(reference)
  end
end

#lazy_find(manager_uuid = nil, opts = {}, ref: primary_index_ref, key: nil, default: nil, transform_nested_lazy_finds: false, **manager_uuid_hash) ⇒ Object

Raises:

  • (ArgumentError)


98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/inventory_refresh/inventory_collection/index/proxy.rb', line 98

def lazy_find(manager_uuid = nil, opts = {}, ref: primary_index_ref, key: nil, default: nil, transform_nested_lazy_finds: false, **manager_uuid_hash)
  # TODO(lsmola) also, it should be enough to have only 1 find method, everything can be lazy, until we try to
  # access the data

  ref                         = opts[:ref] if opts.key?(:ref)
  key                         = opts[:key] if opts.key?(:key)
  default                     = opts[:default] if opts.key?(:default)
  transform_nested_lazy_finds = opts[:transform_nested_lazy_finds] if opts.key?(:transform_nested_lazy_finds)

  manager_uuid_hash.update(opts.except(:ref, :key, :default, :transform_nested_lazy_finds))

  # Skip if no manager_uuid is provided
  return if manager_uuid.nil? && manager_uuid_hash.blank?

  raise ArgumentError, "only one of manager_uuid or manager_uuid_hash must be passed" unless !!manager_uuid ^ !!manager_uuid_hash.present?

  ActiveSupport::Deprecation.warn("Passing a hash for options is deprecated and will be removed in an upcoming release.") if opts.present?

  manager_uuid ||= manager_uuid_hash

  assert_index(manager_uuid, ref)

  ::InventoryRefresh::InventoryObjectLazy.new(inventory_collection,
                                              manager_uuid,
                                              :ref                         => ref,
                                              :key                         => key,
                                              :default                     => default,
                                              :transform_nested_lazy_finds => transform_nested_lazy_finds)
end

#named_ref(ref = primary_index_ref) ⇒ Object



128
129
130
# File 'lib/inventory_refresh/inventory_collection/index/proxy.rb', line 128

def named_ref(ref = primary_index_ref)
  all_refs[ref]
end

#primary_indexObject



76
77
78
# File 'lib/inventory_refresh/inventory_collection/index/proxy.rb', line 76

def primary_index
  data_index(primary_index_ref)
end

#primary_index_refObject



132
133
134
# File 'lib/inventory_refresh/inventory_collection/index/proxy.rb', line 132

def primary_index_ref
  :manager_ref
end

#reindex_secondary_indexes!Object



68
69
70
71
72
73
74
# File 'lib/inventory_refresh/inventory_collection/index/proxy.rb', line 68

def reindex_secondary_indexes!
  data_indexes.each do |ref, index|
    next if ref == primary_index_ref

    index.reindex!
  end
end