Class: IndifferentVariableHash::Hash

Inherits:
Hash
  • Object
show all
Defined in:
lib/indifferent-variable-hash.rb

Overview

IndifferentVariableHash::Hash extends Hash to be ‘indifferent’

‘Indifferent’ has a few different meanings and needs to be better defined.

This is custom in 2 major ways:

* All keys will be stringified (so foo[:hi] and foo['hi'] are the same)
* Sending messages are translated into keys, eg. foo.hi returns foo['hi']

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/indifferent-variable-hash.rb', line 36

def method_missing name, *args
  if name.to_s =~ /=$/ # eg. @hash.foo = 'bar'
    self[$`.to_s] = args.first
  else
    if args.empty?
      self[name.to_s] # eg. @hash.foo
    else
      super # anything else ... fall back to super
    end
  end
end

Instance Method Details

#[](key) ⇒ Object



32
33
34
# File 'lib/indifferent-variable-hash.rb', line 32

def [] key
  super key.to_s
end

#[]=(key, value) ⇒ Object



28
29
30
# File 'lib/indifferent-variable-hash.rb', line 28

def []= key, value
  super key.to_s, value
end