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.



341
342
343
344
345
# File 'lib/hash_delegator.rb', line 341

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.



366
367
368
369
370
# File 'lib/hash_delegator.rb', line 366

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.



353
354
355
356
357
358
359
# File 'lib/hash_delegator.rb', line 353

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

    [key, value]
  end.compact.to_h
end