Module: Kiba::Extend::Utils::Lookup

Extended by:
Lookup
Included in:
Lookup
Defined in:
lib/kiba/extend/utils/lookup.rb,
lib/kiba/extend/utils/lookup/row_sorter.rb,
lib/kiba/extend/utils/lookup/set_checker.rb,
lib/kiba/extend/utils/lookup/row_selector.rb,
lib/kiba/extend/utils/lookup/pair_equality.rb,
lib/kiba/extend/utils/lookup/multival_pairs.rb,
lib/kiba/extend/utils/lookup/pair_inclusion.rb,
lib/kiba/extend/utils/lookup/field_emptiness.rb,
lib/kiba/extend/utils/lookup/criteria_checker.rb,
lib/kiba/extend/utils/lookup/row_selector_by_hash.rb,
lib/kiba/extend/utils/lookup/row_selector_by_lambda.rb

Defined Under Namespace

Classes: CriteriaChecker, FieldEmptiness, MultivalPairs, PairEquality, PairInclusion, RowSelector, RowSelectorByHash, RowSelectorByLambda, RowSorter, SetChecker

Instance Method Summary collapse

Instance Method Details

#csv_to_hash(file:, keycolumn:, csvopt: Kiba::Extend.csvopts) ⇒ Object Also known as: csv_to_multi_hash

creates hash with keycolumn value as key and array of csv-rows-as-hashes as the value

Parameters:

  • file (String)

    path to CSV file

  • csvopt (Hash) (defaults to: Kiba::Extend.csvopts)

    options for reading/parsing CSV

  • keycolumn (Symbol)

    field name on which rows are grouped/looked up



38
39
40
41
42
43
44
# File 'lib/kiba/extend/utils/lookup.rb', line 38

def csv_to_hash(file:, keycolumn:, csvopt: Kiba::Extend.csvopts)
  lookup = Kiba::Extend::Utils::LookupHash.new(keycolumn: keycolumn)
  CSV.foreach(File.expand_path(file), **csvopt) do |row|
    lookup.add_record(row.to_h)
  end
  lookup.hash
end

#csv_to_hash_deprecated(file:, keycolumn:, csvopt: {}) ⇒ Object

Deprecated.

in 2.2.0. The original csv_to_multi_hash now has the name csv_to_hash. csv_to_multi_hash is now aliased to csv_to_hash. Since creating these methods, I never once needed to use the original csv_to_hash method. Any need for it can be met by the multi-hash implementation

TODO:

remove this entirely at some point



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

def csv_to_hash_deprecated(file:, keycolumn:, csvopt: {})
  CSV.foreach(File.expand_path(file),
    csvopt).each_with_object({}) do |r, memo|
    memo[r.fetch(keycolumn, nil)] = r.to_h
  end
end

#enum_to_hash(enum:, keycolumn:) ⇒ Object

Turns any Enumerable where each item is a record/row hash into an expected lookup hash via Utils::LookupHash

Parameters:

  • enum (#each<Hash>)

    rows/records to turn into the lookup source

  • keycolumn (Symbol)

    field name on which rows are grouped/looked up



28
29
30
31
32
# File 'lib/kiba/extend/utils/lookup.rb', line 28

def enum_to_hash(enum:, keycolumn:)
  lookup = Kiba::Extend::Utils::LookupHash.new(keycolumn: keycolumn)
  enum.each { |row| lookup.add_record(row.to_h) }
  lookup.hash
end