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) ⇒ 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

Returns:

  • (Hash)


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

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

#enum_to_hash(enum:, keycolumn:) ⇒ Hash

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

Returns:

  • (Hash)


51
52
53
54
55
# File 'lib/kiba/extend/utils/lookup.rb', line 51

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

#from_job(jobkey:, lookup_on: nil, csvopt: Kiba::Extend.csvopts) ⇒ Hash

Creates hash from a registered job key outside of the context of job files setup

Parameters:

  • jobkey (Symbol)

    registry entry key for job with namespace

  • lookup_on (nil, Symbol) (defaults to: nil)

    field name on which rows are grouped/looked up; will use value defined for registry entry if not provided

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

    options for reading/parsing CSV

Returns:

  • (Hash)


33
34
35
36
37
38
39
40
41
42
43
# File 'lib/kiba/extend/utils/lookup.rb', line 33

def from_job(jobkey:, lookup_on: nil, csvopt: Kiba::Extend.csvopts)
  entry = Kiba::Extend::Registry.entry_for(jobkey)
  path = entry.path
  lkup = lookup_on || entry.lookup_on
  unless lkup
    fail Kiba::Extend::NoLookupOnError.new(jobkey, "Lookup.from_job")
  end
  return {} unless Kiba::Extend::Job.output?(jobkey)

  csv_to_hash(file: path, keycolumn: lkup, csvopt: csvopt)
end