Module: CompactionHelpers
- Included in:
- MarkdownExec::HashDelegatorParent
- Defined in:
- lib/hash_delegator.rb
Overview
This module provides methods for compacting and converting data structures.
Instance Method Summary collapse
-
#compact_and_convert_array_to_hash(array) ⇒ Hash
Converts an array of key-value pairs into a hash, applying compaction to the values.
-
#compact_and_index_hash(hash) ⇒ Hash
Converts a hash into another hash with indexed keys, applying compaction to the values.
-
#compact_hash(hash) ⇒ Hash
Compacts a hash by removing ineligible elements.
Instance Method Details
#compact_and_convert_array_to_hash(array) ⇒ Hash
Converts an array of key-value pairs into a hash,
compaction to the values.
Each value is processed by ‘compact_hash` to remove ineligible elements.
471 472 473 474 475 |
# File 'lib/hash_delegator.rb', line 471 def compact_and_convert_array_to_hash(array) array.transform_values do |value| compact_hash(value) end end |
#compact_and_index_hash(hash) ⇒ Hash
Converts a hash into another hash with indexed keys,
compaction to the values.
The keys are indexed, and the values are
compacted using `compact_and_convert_array_to_hash`.
499 500 501 502 503 |
# File 'lib/hash_delegator.rb', line 499 def compact_and_index_hash(hash) compact_and_convert_array_to_hash(hash.map.with_index do |value, index| [index, value] end.to_h) end |
#compact_hash(hash) ⇒ Hash
Compacts a hash by removing ineligible elements. It filters out nil, empty arrays, empty hashes,
and empty strings from its values.
It also removes entries with :random as the key.
484 485 486 487 488 489 490 |
# File 'lib/hash_delegator.rb', line 484 def compact_hash(hash) hash.map do |key, value| next if value_ineligible?(value) || key == :random [key, value] end.compact.to_h end |