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, applying compaction to the values. Each value is processed by ‘compact_hash` to remove ineligible elements.
309 310 311 312 313 |
# File 'lib/hash_delegator.rb', line 309 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, applying compaction to the values. The keys are indexed, and the values are compacted using ‘compact_and_convert_array_to_hash`.
334 335 336 337 338 |
# File 'lib/hash_delegator.rb', line 334 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.
321 322 323 324 325 326 327 |
# File 'lib/hash_delegator.rb', line 321 def compact_hash(hash) hash.map do |key, value| next if value_ineligible?(value) || key == :random [key, value] end.compact.to_h end |