Class: Kiba::Extend::Utils::LookupHash

Inherits:
Object
  • Object
show all
Defined in:
lib/kiba/extend/utils/lookup_hash.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(keycolumn:) ⇒ LookupHash

Returns a new instance of LookupHash.

Parameters:

  • keycolumn (Symbol)

    field name on which rows are grouped/looked up



11
12
13
14
# File 'lib/kiba/extend/utils/lookup_hash.rb', line 11

def initialize(keycolumn:)
  @keycolumn = keycolumn
  @hash = {}
end

Instance Attribute Details

#hashObject (readonly)

Returns the value of attribute hash.



7
8
9
# File 'lib/kiba/extend/utils/lookup_hash.rb', line 7

def hash
  @hash
end

Instance Method Details

#add_record(record) ⇒ Object

Parameters:

  • record (Hash{Symbol => String})


17
18
19
20
# File 'lib/kiba/extend/utils/lookup_hash.rb', line 17

def add_record(record)
  key = record.fetch(keycolumn, nil)
  hash.key?(key) ? hash[key] << record : hash[key] = [record]
end