Class: InventoryRefresh::InventoryCollection::Reference

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, ref, keys) ⇒ Reference

Returns a new instance of Reference.

Parameters:

  • data (Hash, String)

    Data needed for creating the reference

  • ref (String)

    Name of the reference (and of the index associated)

  • keys (Array<Symbol>)

    Attribute/column names of the reference, that are used as indexes of the passed data hash



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/inventory_refresh/inventory_collection/reference.rb', line 14

def initialize(data, ref, keys)
  @full_reference = build_full_reference(data, keys)
  @ref            = ref
  @keys           = keys

  @nested_secondary_index = keys.select { |key| full_reference[key].kind_of?(InventoryRefresh::InventoryObjectLazy) }.any? do |key|
    !full_reference[key].primary?
  end

  @stringified_reference = self.class.build_stringified_reference(full_reference, keys)
end

Instance Attribute Details

#full_referenceObject (readonly)

Returns the value of attribute full_reference.



6
7
8
# File 'lib/inventory_refresh/inventory_collection/reference.rb', line 6

def full_reference
  @full_reference
end

#keysObject (readonly)

Returns the value of attribute keys.



6
7
8
# File 'lib/inventory_refresh/inventory_collection/reference.rb', line 6

def keys
  @keys
end

#refObject (readonly)

Returns the value of attribute ref.



6
7
8
# File 'lib/inventory_refresh/inventory_collection/reference.rb', line 6

def ref
  @ref
end

#stringified_referenceObject (readonly)

Returns the value of attribute stringified_reference.



6
7
8
# File 'lib/inventory_refresh/inventory_collection/reference.rb', line 6

def stringified_reference
  @stringified_reference
end

Class Method Details

.build_stringified_reference(hash, keys) ⇒ String

Builds string uuid from passed Hash and keys

Parameters:

  • hash (Hash)

    Hash data

  • keys (Array<Symbol>)

    Indexes into the Hash data

Returns:

  • (String)

    Concatenated values on keys from data



48
49
50
# File 'lib/inventory_refresh/inventory_collection/reference.rb', line 48

def build_stringified_reference(hash, keys)
  stringify_reference(keys.map { |attribute| hash[attribute].to_s })
end

.build_stringified_reference_for_record(record, keys) ⇒ String

Builds string uuid from passed Object and keys

Parameters:

  • record (ApplicationRecord)

    ActiveRecord record

  • keys (Array<Symbol>)

    Indexes into the Hash data

Returns:

  • (String)

    Concatenated values on keys from data



57
58
59
# File 'lib/inventory_refresh/inventory_collection/reference.rb', line 57

def build_stringified_reference_for_record(record, keys)
  stringify_reference(keys.map { |attribute| record.public_send(attribute).to_s })
end

.stringify_reference(reference) ⇒ String

Returns passed array joined by stringify_joiner

Parameters:

  • reference (Array<String>)

Returns:

  • (String)

    Passed array joined by stringify_joiner



65
66
67
# File 'lib/inventory_refresh/inventory_collection/reference.rb', line 65

def stringify_reference(reference)
  reference.join(stringify_joiner)
end

Instance Method Details

#loadable?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/inventory_refresh/inventory_collection/reference.rb', line 38

def loadable?
  keys.any? { |attribute| full_reference[attribute].present? }
end

#nested_secondary_index?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/inventory_refresh/inventory_collection/reference.rb', line 34

def nested_secondary_index?
  nested_secondary_index
end

#primary?Boolean

Return true if reference is to primary index, false otherwise. Reference is primary, only if all the nested references are primary.

Returns:

  • (Boolean)

    true if reference is to primary index, false otherwise



30
31
32
# File 'lib/inventory_refresh/inventory_collection/reference.rb', line 30

def primary?
  ref == :manager_ref && !nested_secondary_index
end