Sha256: 417c8efd874223007806beb67f9b73b64b37cfa41872bb89f47a14034be685cb

Contents?: true

Size: 1.24 KB

Versions: 3

Compression:

Stored size: 1.24 KB

Contents

module Gumdrop::Util

  class HashObject < Hash

    # All keys are symbols, internally
    def [](key)
      super(key.to_sym)
    end
    def []=(key, value)
      super(key.to_sym, value)
    end
  
    def method_missing(sym, *args, &block)
      if self.has_key? sym.to_s or self.has_key? sym
        self[sym]
      elsif sym.to_s.ends_with? '='
        key= sym.to_s.chop
        self[key]= args[0]
      else
        # super sym, *args, &block # ???
        nil
      end
    end

    def get(key)
      self[key]
    end
    def set(key,value=nil)
      if key.is_a? Hash
        key.each do |k,v|
          self[k]= v
        end
      else
        self[key]= value
      end
    end

    def has_key?(key)
      super key.to_sym
    end

    def store(key,value)
      super(key.to_sym, value)
    end

    def merge(other_hash=nil, &block)
      unless other_hash.nil?
        super(other_hash.to_symbolized_hash, &block)
      else
        super(other_hash, &block)
      end
    end

    def merge!(other_hash=nil, &block)
      unless other_hash.nil?
        super(other_hash.to_symbolized_hash, &block)
      else
        super(other_hash, &block)
      end
    end

    def self.from(hash={})
      h= new
      h.merge!(hash)
      h
    end
  
  end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
gumdrop-1.0.2 lib/gumdrop/util/hash_object.rb
gumdrop-1.0.1 lib/gumdrop/util/hash_object.rb
gumdrop-1.0.0 lib/gumdrop/util/hash_object.rb