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.
592 593 594 595 596 |
# File 'lib/hash_delegator.rb', line 592 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`.
620 621 622 623 624 |
# File 'lib/hash_delegator.rb', line 620 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.
605 606 607 608 609 610 611 |
# File 'lib/hash_delegator.rb', line 605 def compact_hash(hash) hash.map do |key, value| next if value_ineligible?(value) || key == :random [key, value] end.compact.to_h end |