Class: OpenHash

Inherits:
ActiveSupport::HashWithIndifferentAccess
  • Object
show all
Defined in:
lib/open_hash.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash = {}) ⇒ OpenHash

Returns a new instance of OpenHash.



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

def initialize(hash = {})
  super
  self.default = hash.default
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object (protected)



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/open_hash.rb', line 32

def method_missing(name, *args, &block)
  method = name.to_s
  
  case method
    when %r{.=$}
      super unless args.length == 1
      self[method[0...-1]] = args.first
    
    when %r{.\?$}
      super unless args.empty?
      self.key?(method[0...-1].to_sym)
      
    when %r{^_.}
      ret = self.send(method[1..-1], *args, &block)
      (ret.is_a?(Hash) and meth != :_default) ? OpenHash[ret] : ret 
      
    else
      if key?(method) or !respond_to?(method)
        self[method]
      else
        super
      end
  end
end

Class Method Details

.[](hash = {}) ⇒ Object



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

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

Instance Method Details

#dupObject



13
14
15
# File 'lib/open_hash.rb', line 13

def dup
  self.class.new(self)
end