Class: HashPath

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash = {}) ⇒ HashPath

Returns a new instance of HashPath.



23
24
25
26
# File 'lib/hash-path.rb', line 23

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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



33
34
35
36
37
38
39
# File 'lib/hash-path.rb', line 33

def method_missing(name, *args, &block)
  if args.empty?
    self[ self.class.paths[name.to_s] || name ]
  else
    super
  end
end

Class Method Details

.path(key, path) ⇒ Object



7
8
9
# File 'lib/hash-path.rb', line 7

def self.path(key, path)
  paths[key.to_s] = path
end

.pathsObject



3
4
5
# File 'lib/hash-path.rb', line 3

def self.paths
  @paths ||= {}
end

Instance Method Details

#[](key) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/hash-path.rb', line 11

def [](key)
  key = key.to_s

  # first, check exact value
  return super if has_key?(key)

  # second, browse children
  return key.split("/").inject(self) {|hash, key| hash[key]}
rescue Exception => e
  return rescue_hierarchical_access(key, e)
end