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

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.

Parameters:

  • array (Array)

    The array of key-value pairs to be converted.

Returns:

  • (Hash)

    A hash with keys from the array and compacted values.



363
364
365
366
367
# File 'lib/hash_delegator.rb', line 363

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`.

Parameters:

  • hash (Hash)

    The hash to be converted and compacted.

Returns:

  • (Hash)

    A hash with indexed keys and the compacted original values.



391
392
393
394
395
# File 'lib/hash_delegator.rb', line 391

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.

Parameters:

  • hash (Hash)

    The hash to be compacted.

Returns:

  • (Hash)

    A compacted version of the input hash.



376
377
378
379
380
381
382
# File 'lib/hash_delegator.rb', line 376

def compact_hash(hash)
  hash.map do |key, value|
    next if value_ineligible?(value) || key == :random

    [key, value]
  end.compact.to_h
end