Class: AWS::Core::IndifferentHash

Inherits:
Hash
  • Object
show all
Defined in:
lib/aws/core/indifferent_hash.rb

Overview

A utility class to provide indifferent access to hash data.

Inspired by ActiveSupport’s HashWithIndifferentAccess, this class has a few notable differences:

  • ALL keys are converted to strings (via #to_s)

  • It does not deep merge/convert hashes indifferently, good for fla

  • It will not perserve default value behaviours

These features were omitted because our primary use for this class is to wrap a 1-level hash as a return value, but we want the user to access the values with string or symbol keys.

Direct Known Subclasses

Record::Errors

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ IndifferentHash

Returns a new instance of IndifferentHash.



33
34
35
36
37
38
39
40
# File 'lib/aws/core/indifferent_hash.rb', line 33

def initialize *args
  if args.first.is_a?(Hash)
    super()
    merge!(*args)
  else
    super(*args)
  end
end

Instance Method Details

#[](key) ⇒ Object



50
51
52
# File 'lib/aws/core/indifferent_hash.rb', line 50

def [] key
  _getter(_convert_key(key))
end

#[]=(key, value) ⇒ Object Also known as: store



45
46
47
# File 'lib/aws/core/indifferent_hash.rb', line 45

def []=(key, value)
  _setter(_convert_key(key), value)
end

#_getterObject



42
# File 'lib/aws/core/indifferent_hash.rb', line 42

alias_method :_getter, :[]

#_setterObject



43
# File 'lib/aws/core/indifferent_hash.rb', line 43

alias_method :_setter, :[]=

#delete(key) ⇒ Object



77
78
79
# File 'lib/aws/core/indifferent_hash.rb', line 77

def delete key
  super(_convert_key(key))
end

#fetch(key, *extras, &block) ⇒ Object



73
74
75
# File 'lib/aws/core/indifferent_hash.rb', line 73

def fetch key, *extras, &block
  super(_convert_key(key), *extras, &block)
end

#has_key?(key) ⇒ Boolean Also known as: key?, member?, include?

Returns:

  • (Boolean)


66
67
68
# File 'lib/aws/core/indifferent_hash.rb', line 66

def has_key? key
  super(_convert_key(key))
end

#merge(hash) ⇒ Object



62
63
64
# File 'lib/aws/core/indifferent_hash.rb', line 62

def merge hash
  self.dup.merge!(hash)
end

#merge!(hash) ⇒ Object Also known as: update



54
55
56
57
58
59
# File 'lib/aws/core/indifferent_hash.rb', line 54

def merge! hash
  hash.each_pair do |key,value|
    self[key] = value 
  end
  self
end