Class: Adva::Registry

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRegistry

Returns a new instance of Registry.



23
24
25
# File 'lib/adva/registry.rb', line 23

def initialize
  super &lambda { |hash, key| hash[key] = Registry.new }
end

Class Method Details

.clearObject



18
19
20
# File 'lib/adva/registry.rb', line 18

def clear
  instance.clear
end

.get(*args) ⇒ Object



10
11
12
# File 'lib/adva/registry.rb', line 10

def get(*args)
  instance.get(*args)
end

.instanceObject



6
7
8
# File 'lib/adva/registry.rb', line 6

def instance
  @@instance ||= new
end

.set(*args) ⇒ Object



14
15
16
# File 'lib/adva/registry.rb', line 14

def set(*args)
  instance.set(*args)
end

Instance Method Details

#get(*keys) ⇒ Object



42
43
44
45
46
47
# File 'lib/adva/registry.rb', line 42

def get(*keys)
  keys.map { |key| key.to_sym }.inject(self) do |result, key|
    return nil unless result.has_key?(key)
    result[key]
  end
end

#set(*args) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/adva/registry.rb', line 27

def set(*args)
  value, last_key = args.pop, args.pop.to_sym
  target = args.inject(self) { |result, key| result[key.to_sym] }

  case value
  when Hash
    target[last_key].merge!(to_registry(value))
  when Array
    target[last_key] = Array(target[last_key]) unless target[last_key].is_a?(Array)
    target[last_key] += value
  else
    target.merge!(last_key => value)
  end
end