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)


38
39
40
41
42
# File 'lib/datadog/core/utils/hash.rb', line 38

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



44
45
46
47
48
49
50
51
52
# File 'lib/datadog/core/utils/hash.rb', line 44

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)


64
65
66
# File 'lib/datadog/core/utils/hash.rb', line 64

def empty?
  @hash.empty?
end

#key?(key) ⇒ Boolean

Returns:

  • (Boolean)


54
55
56
57
58
59
60
61
62
# File 'lib/datadog/core/utils/hash.rb', line 54

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

#lengthObject



68
69
70
# File 'lib/datadog/core/utils/hash.rb', line 68

def length
  @hash.length
end

#original_hashObject



72
73
74
# File 'lib/datadog/core/utils/hash.rb', line 72

def original_hash
  @hash
end