Class: OpenHash

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

Instance Method Summary collapse

Methods inherited from Hash

#to_openhash, #to_post_fields, #to_querystring

Constructor Details

#initialize(hash = {}) ⇒ OpenHash

Returns a new instance of OpenHash.



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

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

Allow hash properties to be referenced by dot notation



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

def method_missing(name, *args)
  name = name.to_s
  k = name.sub(/[?!=]$/, '')

  if name =~ /=$/ && !args.empty?
    self[k] = args.first
  elsif self.include? k
    self[k]
  elsif self.include? k.to_sym
    self[k.to_sym]
  end
end

Instance Method Details

#[](key) ⇒ Object



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

def [](key)
  h = super(key)
  h.is_a?(Hash) ? OpenHash.new(h) : h
end