Class: Hash::NormalizeKeys

Inherits:
Object
  • Object
show all
Defined in:
lib/hash/normalizekeys.rb

Overview

Hash::NormalizeKeys

Direct Known Subclasses

String

Constant Summary collapse

VERSION =

version

'1.3'

Instance Method Summary collapse

Constructor Details

#initialize(*opts, &block) ⇒ NormalizeKeys

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



9
10
11
# File 'lib/hash/normalizekeys.rb', line 9

def initialize(*opts, &block)
  @hsh = Hash.new(*opts, &block)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args, &block) ⇒ Object (private)

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



115
116
117
118
119
120
121
122
# 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

Instance Method Details

#invertObject

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



83
84
85
86
87
88
89
90
91
# 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) ⇒ Object

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



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# 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) ⇒ Object Also known as: update

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



38
39
40
41
42
43
44
45
46
# 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

#replace(other) ⇒ Object

Works like Hash#replace, with keys being normalized.



52
53
54
55
56
57
58
59
60
61
62
63
# 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) ⇒ Boolean

Returns the underlying hash’s respond_to? method.

Returns:

  • (Boolean)


14
15
16
# File 'lib/hash/normalizekeys.rb', line 14

def respond_to?(m)
  return @hsh.respond_to?(m)
end

#slice(*org_keys) ⇒ Object

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



66
67
68
69
70
71
72
73
74
# 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_sObject

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



77
78
79
# File 'lib/hash/normalizekeys.rb', line 77

def to_s
  return @hsh.to_s
end

#values_at(*keys) ⇒ Object

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



94
95
96
97
98
99
100
101
102
# File 'lib/hash/normalizekeys.rb', line 94

def values_at(*keys)
  rv = []
  
  keys.each do |key|
    rv.push self[key]
  end
  
  return rv
end