Sha256: 4b5b2829ff101c7bdb65405886d6a0da3056f6559fafafcc2ebd115a53663980

Contents?: true

Size: 914 Bytes

Versions: 7

Compression:

Stored size: 914 Bytes

Contents

class Registry < Hash
  class << self
    def instance
      @@instance ||= new
    end
    
    def get(*args)
      instance.get *args
    end
    
    def set(*args)
      instance.set *args
    end
    
    def clear
      instance.clear
    end
  end

  def initialize
    blk = lambda {|h,k| h[k] = Registry.new(&blk)}
    super &blk
  end

  def set(*args)
    value, last_key = args.pop, args.pop
    target = args.inject(self){|result, key| result[key] }
    value = to_registry(value) if value.is_a?(Hash)
    target[last_key] = value
  end

  def get(*keys)
    keys.inject self do |result, key| 
      return nil unless result.has_key?(key)
      result[key]
    end
  end
  
  protected
  
    def to_registry(hash)
      registry = Registry.new
      hash.each do |key, value|
        value = to_registry(value) if value.is_a?(Hash)
        registry[key] = value
      end
      registry
    end
end

Version data entries

7 entries across 7 versions & 2 rubygems

Version Path
adva-0.1.4 lib/registry.rb
adva-0.1.3 lib/registry.rb
adva-0.1.2 lib/registry.rb
adva-0.1.1 lib/registry.rb
adva-0.1.0 lib/registry.rb
adva_cms-0.0.1 lib/registry.rb
adva-0.0.1 adva_cms/lib/registry.rb