Sha256: c11194743e3aa403911bdc3f91fb854ec4fecfe15886b55c57e2a59a10b0fc52

Contents?: true

Size: 887 Bytes

Versions: 8

Compression:

Stored size: 887 Bytes

Contents

class ExtendHash < Hash
  class << self
    def [](hash)
      case hash
      when ExtendHash
        hash
      when Hash
        eh = self.new
        eh.replace deep_convert(hash)
      else
        raise ArgumentError, "must be a Hash or ExtendHash"
      end
    end

  private
    # convert string key to symbol key.
    # I'm rescurive
    def deep_convert(hash)
      ret = {}
      hash.each { |k,v|
        ret[k.to_sym] = Hash===v ? deep_convert(v) : v
      }
      ret
    end
  end

  def []=(key, value)
    key = key.to_sym if String === key
    super(key, value)
  end

  def [](key)
    key = key.to_sym if String === key
    super(key)
  end

  def method_missing(name, *args)
    if name =~ /=$/
      store(name[0...-1].to_sym, args[0])
    elsif has_key?(name)
      fetch(name)
    else
      raise NoMethodError, "-- :#{name}, #{args.inspect}"
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
tagen-1.1.7 lib/tagen/core/extend_hash.rb
tagen-1.1.6 lib/tagen/core/extend_hash.rb
tagen-1.1.5 lib/tagen/core/extend_hash.rb
tagen-1.1.4 lib/tagen/core/extend_hash.rb
tagen-1.1.3 lib/tagen/core/extend_hash.rb
tagen-1.1.2 lib/tagen/core/extend_hash.rb
tagen-1.1.1 lib/tagen/core/extend_hash.rb
tagen-1.1.0 lib/tagen/core/extend_hash.rb