Class: OpenHash

Inherits:
HashWithIndifferentAccess show all
Defined in:
lib/openhash/openhash.rb

Overview

Adapted from Ruby Facets

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from HashWithIndifferentAccess

#[]=, #default, #delete, #fetch, #key?, #merge, #regular_update, #regular_writer, #stringify_keys!, #symbolize_keys!, #to_hash, #to_options!, #update, #values_at

Methods inherited from Hash

#to_openhash

Constructor Details

#initialize(hash = {}) ⇒ OpenHash

Returns a new instance of OpenHash.



3
4
5
6
# File 'lib/openhash/openhash.rb', line 3

def initialize(hash = {})
  super()
  update(hash)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object

Route get and set calls.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/openhash/openhash.rb', line 13

def method_missing(name, *args)
  name = name.to_s
  # do nothing if missing method
  return if name == 'default' && !args.empty?
  k = name.sub(/[?!=]$/, '')
  if name =~ /=$/
    self[k] = args.first
  elsif args.empty?
    self[k]
  else
    # this should never be called?!
    super(name, *args)
  end
end

Class Method Details

.[](hash = {}) ⇒ Object



8
9
10
# File 'lib/openhash/openhash.rb', line 8

def self.[](hash = {})
  new(hash)
end

Instance Method Details

#dupObject



32
33
34
# File 'lib/openhash/openhash.rb', line 32

def dup
  self.class.new(self)
end

#with_indifferent_accessObject



28
29
30
# File 'lib/openhash/openhash.rb', line 28

def with_indifferent_access
  self
end