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
-
#csv_to_hash(file:, keycolumn:, csvopt: Kiba::Extend.csvopts) ⇒ Object
(also: #csv_to_multi_hash)
creates hash with keycolumn value as key and array of csv-rows-as-hashes as the value.
-
#csv_to_hash_deprecated(file:, keycolumn:, csvopt: {}) ⇒ Object
deprecated
Deprecated.
in 2.2.0. The original
csv_to_multi_hash
now has the namecsv_to_hash
.csv_to_multi_hash
is now aliased tocsv_to_hash
. Since creating these methods, I never once needed to use the originalcsv_to_hash
method. Any need for it can be met by the multi-hash implementation -
#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.
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
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.(file), **csvopt) do |row| lookup.add_record(row.to_h) end lookup.hash end |
#csv_to_hash_deprecated(file:, keycolumn:, csvopt: {}) ⇒ Object
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
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.(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
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 |