Sha256: 08a92315a8fb9e2e7718189ffdfa490a71db7be51bad6dd13f946d126c91ac30

Contents?: true

Size: 1.04 KB

Versions: 8

Compression:

Stored size: 1.04 KB

Contents

module Pushwagner
  class HashWithIndifferentAccess < ::Hash
    def initialize(hash={})
      super()
      hash.each do |key, value|
        self[convert_key(key)] = value
      end
    end

    def [](key)
      super(convert_key(key))
    end

    def []=(key, value)
      super(convert_key(key), value)
    end

    def delete(key)
      super(convert_key(key))
    end

    def values_at(*indices)
      indices.collect { |key| self[convert_key(key)] }
    end

    def merge(other)
      dup.merge!(other)
    end

    def merge!(other)
      other.each do |key, value|
        self[convert_key(key)] = value
      end
      self
    end

    def to_hash
      Hash.new(default).merge!(self)
    end

    protected
    def convert_key(key)
      key.is_a?(Symbol) ? key.to_s : key
    end

    def method_missing(method, *args, &block)
      method = method.to_s
      if method =~ /^(\w+)\?$/
        if args.empty?
          !!self[$1]
        else
          self[$1] == args.first
        end
      else
        self[method]
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
pushwagner-0.0.1.7 lib/pushwagner/ext.rb
pushwagner-0.0.1.6 lib/pushwagner/ext.rb
pushwagner-0.0.1.5 lib/pushwagner/ext.rb
pushwagner-0.0.1.4 lib/pushwagner/ext.rb
pushwagner-0.0.1.3 lib/pushwagner/ext.rb
pushwagner-0.0.1.2 lib/pushwagner/ext.rb
pushwagner-0.0.1.1 lib/pushwagner/ext.rb
pushwagner-0.0.1 lib/pushwagner/ext.rb