Class: Datadog::Core::Utils::Hash::CaseInsensitiveWrapper

Inherits:
Object
  • Object
show all
Defined in:
lib/datadog/core/utils/hash.rb

Overview

A minimal Datadog::Core::Utils::Hash wrapper that provides case-insensitive access to hash keys, without the overhead of copying the original hash.

This class should be used when the original hash is short lived and each hash key is only accesses a few times. For other cases, create a copy of the original hash with the keys normalized adequate to your use case.

Instance Method Summary collapse

Constructor Details

#initialize(hash) ⇒ CaseInsensitiveWrapper

Returns a new instance of CaseInsensitiveWrapper.

Raises:

  • (ArgumentError)


15
16
17
18
19
# File 'lib/datadog/core/utils/hash.rb', line 15

def initialize(hash)
  raise ArgumentError, "must be a hash, but was #{hash.class}: #{hash.inspect}" unless hash.is_a?(::Hash)

  @hash = hash
end

Instance Method Details

#[](key) ⇒ Object



21
22
23
24
25
26
27
28
29
# File 'lib/datadog/core/utils/hash.rb', line 21

def [](key)
  return nil unless key.is_a?(::String)

  @hash.each do |k, value|
    return value if key.casecmp(k) == 0
  end

  nil
end

#empty? ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/datadog/core/utils/hash.rb', line 41

def empty?
  @hash.empty?
end

#key?(key) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
34
35
36
37
38
39
# File 'lib/datadog/core/utils/hash.rb', line 31

def key?(key)
  return false unless key.is_a?(::String)

  @hash.each_key do |k|
    return true if key.casecmp(k) == 0
  end

  false
end

#length ⇒ Object



45
46
47
# File 'lib/datadog/core/utils/hash.rb', line 45

def length
  @hash.length
end

#original_hash ⇒ Object



49
50
51
# File 'lib/datadog/core/utils/hash.rb', line 49

def original_hash
  @hash
end