Module: Html2rss::HashUtil

Defined in:
lib/html2rss/hash_util.rb

Overview

Shared helpers for hash normalization and structural operations.

Class Method Summary collapse

Class Method Details

.assert_string_keys!(value, context: 'hash', deep: true) ⇒ void

This method returns an undefined value.

Validates that hash keys are strings.

Parameters:

  • value (Object)

    candidate hash container whose keys must be strings

  • context (String) (defaults to: 'hash')

    error context

  • deep (Boolean) (defaults to: true)

    whether nested hashes should also be validated



80
81
82
83
84
85
86
87
88
89
# File 'lib/html2rss/hash_util.rb', line 80

def assert_string_keys!(value, context: 'hash', deep: true)
  return unless value in Hash

  unless value.each_key.all?(String)
    invalid_key = value.keys.find { _1.class != String }
    raise ArgumentError, "#{context} must use string keys (found #{invalid_key.inspect})"
  end

  value.each_value { assert_string_keys!(_1, context:, deep:) } if deep
end

.assert_symbol_keys!(value, context: 'hash', deep: true) ⇒ void

This method returns an undefined value.

Validates that hash keys are symbols.

Parameters:

  • value (Object)

    candidate hash container whose keys must be symbols

  • context (String) (defaults to: 'hash')

    error context

  • deep (Boolean) (defaults to: true)

    whether nested hashes should also be validated



63
64
65
66
67
68
69
70
71
72
# File 'lib/html2rss/hash_util.rb', line 63

def assert_symbol_keys!(value, context: 'hash', deep: true)
  return unless value in Hash

  unless value.each_key.all?(Symbol)
    invalid_key = value.keys.find { _1.class != Symbol }
    raise ArgumentError, "#{context} must use symbol keys (found #{invalid_key.inspect})"
  end

  value.each_value { assert_symbol_keys!(_1, context:, deep:) } if deep
end

.deep_dup(object) ⇒ Object

Deeply duplicates nested arrays and hashes.

Parameters:

  • object (Object)

    nested value from configuration or runtime state

Returns:

  • (Object)

    deep duplicated object



12
13
14
15
16
17
18
19
20
21
# File 'lib/html2rss/hash_util.rb', line 12

def deep_dup(object)
  case object
  in Hash
    object.transform_values { deep_dup(_1) }
  in Array
    object.map { deep_dup(_1) }
  else
    object.dup rescue StandardError # rubocop:disable Style/RescueModifier
  end
end

.deep_merge(base, override) ⇒ Hash

Deeply merges nested hashes while replacing non-hash values from override.

Parameters:

  • base (Hash)

    base hash

  • override (Hash)

    override hash

Returns:

  • (Hash)

    merged hash



28
29
30
31
32
33
34
35
36
37
# File 'lib/html2rss/hash_util.rb', line 28

def deep_merge(base, override)
  base.merge(override) do |_key, old_val, new_val|
    case [old_val, new_val]
    in [Hash, Hash]
      deep_merge(old_val, new_val)
    else
      new_val
    end
  end
end

.deep_symbolize_keys(object, context: 'hash') ⇒ Object

Converts string-keyed hashes to symbol-keyed hashes recursively.

Parameters:

  • object (Object)

    value to normalize

  • context (String) (defaults to: 'hash')

    error context

Returns:

  • (Object)

    normalized value



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/html2rss/hash_util.rb', line 44

def deep_symbolize_keys(object, context: 'hash')
  case object
  in Hash
    object.each_with_object({}) do |(k, v), memo|
      memo[symbol_key(k, context:)] = deep_symbolize_keys(v, context:)
    end
  in Array
    object.map { deep_symbolize_keys(_1, context:) }
  else
    object
  end
end