Parent

CastingHash

CastingHash is just like Hash, except that all keys and values are passed through casting procedures.

Constants

KEY_PROC

Default key conversion procedure.

VAL_PROC

Default value conversion procedure.

Public Class Methods

[](hash) click to toggle source
    # File lib/more/facets/casting_hash.rb, line 15
15:   def self.[](hash)
16:     s = new
17:     hash.each{ |k,v| s[k] = v }
18:     s
19:   end
new(hash, value_cast=nil, &key_cast) click to toggle source
    # File lib/more/facets/casting_hash.rb, line 22
22:   def initialize(hash, value_cast=nil, &key_cast)
23:     @key_proc   = key_cast           || KEY_PROC
24:     @value_proc = value_cast.to_proc || VAL_PROC
25:     hash.each{ |k,v| self[k] = v }
26:   end

Public Instance Methods

<<(other) click to toggle source
    # File lib/more/facets/casting_hash.rb, line 59
59:   def <<(other)
60:     case other
61:     when Hash
62:       super(cast(other))
63:     when Array
64:       self[other[0]] = other[1]
65:     else
66:       raise ArgumentError
67:     end
68:   end
[](k) click to toggle source
    # File lib/more/facets/casting_hash.rb, line 49
49:   def [](k)
50:     super(key_proc[k])
51:   end
[]=(k,v) click to toggle source
    # File lib/more/facets/casting_hash.rb, line 54
54:   def []=(k,v)
55:     super(key_proc[k], value_proc[v])
56:   end
delete(k) click to toggle source
     # File lib/more/facets/casting_hash.rb, line 118
118:   def delete(k)
119:     super(key_proc[k])
120:   end
fetch(k) click to toggle source
    # File lib/more/facets/casting_hash.rb, line 70
70:   def fetch(k)
71:     super(key_proc[k])
72:   end
has_key?(k) click to toggle source
    # File lib/more/facets/casting_hash.rb, line 85
85:   def has_key?(k)
86:     super(key_proc[k])
87:   end
key?(k) click to toggle source
    # File lib/more/facets/casting_hash.rb, line 80
80:   def key?(k)
81:     super(key_proc[k])
82:   end
key_proc() click to toggle source
    # File lib/more/facets/casting_hash.rb, line 29
29:   def key_proc
30:     @key_proc
31:   end
key_proc=(proc) click to toggle source
    # File lib/more/facets/casting_hash.rb, line 34
34:   def key_proc=(proc)
35:     @key_proc = proc.to_proc
36:   end
merge!(other) click to toggle source

Same as #.

     # File lib/more/facets/casting_hash.rb, line 128
128:   def merge!(other)
129:     super(cast(other))
130:   end
rekey(*args, &block) click to toggle source
     # File lib/more/facets/casting_hash.rb, line 113
113:   def rekey(*args, &block)
114:     dup.rekey!(*args, &block)
115:   end
rekey!(*args, &block) click to toggle source

Synonym for Hash#rekey, but modifies the receiver in place (and returns it).

  foo = { :name=>'Gavin', :wife=>:Lisa }.to_stash
  foo.rekey!{ |k| k.upcase }  #=>  { "NAME"=>"Gavin", "WIFE"=>:Lisa }
  foo.inspect                 #=>  { "NAME"=>"Gavin", "WIFE"=>:Lisa }
     # File lib/more/facets/casting_hash.rb, line 95
 95:   def rekey!(*args, &block)
 96:     # for backward comptability (DEPRECATE?).
 97:     block = args.pop.to_sym.to_proc if args.size == 1
 98:     if args.empty?
 99:       block = lambda{|k| k} unless block
100:       keys.each do |k|
101:         nk = block[k]
102:         self[nk] = delete(k) #if nk
103:       end
104:     else
105:       raise ArgumentError, "3 for 2" if block
106:       to, from = *args
107:       self[to] = delete(from) if has_key?(from)
108:     end
109:     self
110:   end
replace(other) click to toggle source
     # File lib/more/facets/casting_hash.rb, line 133
133:   def replace(other)
134:     super(cast(other))
135:   end
store(k, v) click to toggle source
    # File lib/more/facets/casting_hash.rb, line 75
75:   def store(k, v)
76:     super(key_proc[k], value_proc[v])
77:   end
to_h() click to toggle source
Alias for: to_hash
to_hash() click to toggle source
     # File lib/more/facets/casting_hash.rb, line 143
143:   def to_hash
144:     h = {}; each{ |k,v| h[k] = v }; h
145:   end
Also aliased as: to_h
update(other) click to toggle source
     # File lib/more/facets/casting_hash.rb, line 123
123:   def update(other)
124:     super(cast(other))
125:   end
value_proc() click to toggle source
    # File lib/more/facets/casting_hash.rb, line 39
39:   def value_proc
40:     @value_proc
41:   end
value_proc=(proc) click to toggle source
    # File lib/more/facets/casting_hash.rb, line 44
44:   def value_proc=(proc)
45:     @value_proc = proc.to_proc
46:   end
values_at(*keys) click to toggle source
     # File lib/more/facets/casting_hash.rb, line 138
138:   def values_at(*keys)
139:     super(keys.map(&key_proc))
140:   end

Private Instance Methods

cast(hash) click to toggle source
     # File lib/more/facets/casting_hash.rb, line 153
153:     def cast(hash)
154:       h
155:       hash.each do |k,v| 
156:         h[key_proc[k]] = value_proc[v]
157:       end
158:       h
159:     end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.