Module: ArrayCollection::ArrayComponents::KeyFiltering

Included in:
CollectionArray
Defined in:
lib/array_collection/array_components/key_filtering.rb

Overview

rubocop:disable Style/Documentation

Instance Method Summary collapse

Instance Method Details

#except(array, *keys) ⇒ Object

Raises:

  • (ArgumentError)


10
11
12
13
14
# File 'lib/array_collection/array_components/key_filtering.rb', line 10

def except(array, *keys)
  raise ArgumentError, "Empty key list" if keys.empty?

  array.map { |hash| hash.except(*keys) }
end

#key_by(records, key) ⇒ Object



6
7
8
# File 'lib/array_collection/array_components/key_filtering.rb', line 6

def key_by(records, key)
  records.to_h { |record| [record[key].to_s, yield(record)] }
end

#only(array, *keys) ⇒ Object

Raises:

  • (ArgumentError)


16
17
18
19
20
# File 'lib/array_collection/array_components/key_filtering.rb', line 16

def only(array, *keys)
  raise ArgumentError, "Empty key list" if keys.empty?

  array.map { |hash| hash.select { |k, _| keys.include?(k) } }
end