Class: ChillDB::IndifferentHash

Inherits:
Hash
  • Object
show all
Defined in:
lib/chill.rb

Overview

A simple version of Hash which converts keys to strings - so symbols and strings can be used interchangably as keys. Works pretty much like a Hash. and also provides method getters and setters via #method_missing

Direct Known Subclasses

Document

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ IndifferentHash

:nodoc:



303
304
305
306
307
# File 'lib/chill.rb', line 303

def initialize *args # :nodoc:
  super(*args) do |hash, key| # indifferent access
    hash[key.to_s] if Symbol === key
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object

getters and setters for hash items



310
311
312
313
314
# File 'lib/chill.rb', line 310

def method_missing name, *args
  return self[name.to_s] if self.key? name.to_s
  return self[name.to_s[0...-1]] = args.first if name.to_s.end_with? '=' and args.length == 1
  super
end

Instance Method Details

#[]=(key, value) ⇒ Object

:nodoc:



331
332
333
334
# File 'lib/chill.rb', line 331

def []= key, value # :nodoc:
  key = key.to_s if key.is_a? Symbol
  super(key, normalize(value))
end

#to_hashObject

Convert to a regular ruby Hash



337
338
339
# File 'lib/chill.rb', line 337

def to_hash
  Hash.new.replace self
end