Class: IndifferentAccessHash

Inherits:
Object
  • Object
show all
Defined in:
lib/brightbox-cli/indifferent_access_hash.rb

Overview

A simpler wrapper to allows either String or Symbol keys to be used when accessing attributes since fog applies a change on the top level resulting in a mix of both which has introduced issues.

Instance Method Summary collapse

Constructor Details

#initialize(hash) ⇒ IndifferentAccessHash

Returns a new instance of IndifferentAccessHash.



5
6
7
# File 'lib/brightbox-cli/indifferent_access_hash.rb', line 5

def initialize(hash)
  @hash = hash
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



29
30
31
# File 'lib/brightbox-cli/indifferent_access_hash.rb', line 29

def method_missing(method, *args, &block)
  @hash.send(method, *args, &block)
end

Instance Method Details

#==(other) ⇒ Object

Returns the result of the comparison.

Parameters:

  • other (Object)

    the object to compare

Returns:

  • (Object)

    the result of the comparison



25
26
27
# File 'lib/brightbox-cli/indifferent_access_hash.rb', line 25

def ==(other)
  @hash == (other.is_a?(IndifferentAccessHash) ? other.to_h : other)
end

#[](key) ⇒ Object

Returns the value of the key.

Parameters:

  • key (String, Symbol)

    the key to look up

Returns:

  • (Object)

    the value of the key



11
12
13
14
# File 'lib/brightbox-cli/indifferent_access_hash.rb', line 11

def [](key)
  value = @hash[key.to_s] || @hash[key.to_sym]
  wrap(value)
end

#[]=(key, value) ⇒ Object

Returns the value of the key.

Parameters:

  • key (String, Symbol)

    the key to set

  • value (Object)

    the value to set

Returns:

  • (Object)

    the value of the key



19
20
21
# File 'lib/brightbox-cli/indifferent_access_hash.rb', line 19

def []=(key, value)
  @hash[key.to_sym] = value
end

#to_hObject



33
34
35
# File 'lib/brightbox-cli/indifferent_access_hash.rb', line 33

def to_h
  @hash
end