class Hash::NormalizeKeys

Hash::NormalizeKeys

Constants

COERCES

a list of methods that need the first param normalized

VERSION

version

Public Class Methods

new(*opts, &block) click to toggle source

new() takes the same params as Hash.new.

# File lib/hash/normalizekeys.rb, line 9
def initialize(*opts, &block)
        @hsh = Hash.new(*opts, &block)
end

Private Class Methods

normalize_key(key) click to toggle source

normalize_key

# File lib/hash/normalizekeys.rb, line 125
def self.normalize_key(key)
        raise 'override-normalize_key'
end

Public Instance Methods

invert() click to toggle source

Works like Hash#invert, except the new object is a NormalizeKey object and the keys have been normalized.

# File lib/hash/normalizekeys.rb, line 83
def invert
        rv = self.class.new
        
        self.each do |k, v|
                rv[v] = k
        end
        
        return rv
end
merge(other) click to toggle source

Returns a new NormalizeKeys object by combining this object and the other object, with keys being normalized.

# File lib/hash/normalizekeys.rb, line 20
def merge(other)
        rv = self.class.new
        
        # initialize with own elements
        self.each do |k, v|
                rv[k] = v
        end
        
        # merge in other's elements
        other.each do |k, v|
                rv[k] = v
        end
        
        # return
        return rv
end
merge!(other) click to toggle source

Merges other into the NormalizeKey object, with other's keys being normalized.

# File lib/hash/normalizekeys.rb, line 38
def merge!(other)
        # merge in other's elements
        other.each do |k, v|
                self[k] = v
        end
        
        # return
        return self
end
Also aliased as: update
replace(other) click to toggle source

Works like Hash#replace, with keys being normalized.

# File lib/hash/normalizekeys.rb, line 52
def replace(other)
        # reset @hsh
        @hsh.clear
        
        # merge in other's elements
        other.each do |k, v|
                self[k] = v
        end
        
        # return
        return self
end
respond_to?(m) click to toggle source

Returns the underlying hash's respond_to? method.

# File lib/hash/normalizekeys.rb, line 14
def respond_to?(m)
        return @hsh.respond_to?(m)
end
slice(*org_keys) click to toggle source

Works just like Hash#slice, except the key values are normalized.

# File lib/hash/normalizekeys.rb, line 66
def slice(*org_keys)
        use_keys = {}
        
        org_keys.each do |org_key|
                use_keys[self.class.normalize_key(org_key)] = nil
        end
        
        return @hsh.slice(*use_keys.keys)
end
to_s() click to toggle source

Returns the results of the underlying hash's to_s method.

# File lib/hash/normalizekeys.rb, line 77
def to_s
        return @hsh.to_s
end
update(other)

works just like merge!

Alias for: merge!
values_at(*keys) click to toggle source

Works like Hash#values_at, except the key values are normalized.

# File lib/hash/normalizekeys.rb, line 94
def values_at(*keys)
        rv = []
        
        keys.each do |key|
                rv.push self[key]
        end
        
        return rv
end

Private Instance Methods

method_missing(m, *args, &block) click to toggle source

All methods not already defined are passed to @hsh. If the method is listed in COERCES then the first arg is normalized.

# File lib/hash/normalizekeys.rb, line 115
def method_missing(m, *args, &block)
        if COERCES.include?(m.to_s) and (args.length > 0)
                args = *args
                args[0] = self.class.normalize_key(args[0])
        end
        
        return @hsh.send(m, *args, &block)
end