Parent

Files

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/hashery/castinghash.rb, line 15
def self.[](hash)
  s = new
  hash.each{ |k,v| s[k] = v }
  s
end
new(hash={}, value_cast=nil, &key_cast) click to toggle source
# File lib/hashery/castinghash.rb, line 22
def initialize(hash={}, value_cast=nil, &key_cast)
  @key_proc   = (key_cast   || KEY_PROC)
  @value_proc = (value_cast || VAL_PROC).to_proc
  hash.each{ |k,v| self[k] = v }
end

Public Instance Methods

<<(other) click to toggle source
# File lib/hashery/castinghash.rb, line 59
def <<(other)
  case other
  when Hash
    super(cast(other))
  when Array
    self[other[0]] = other[1]
  else
    raise ArgumentError
  end
end
[](k) click to toggle source
# File lib/hashery/castinghash.rb, line 49
def [](k)
  super(key_proc[k])
end
[]=(k,v) click to toggle source
# File lib/hashery/castinghash.rb, line 54
def []=(k,v)
  super(key_proc[k], value_proc[v])
end
delete(k) click to toggle source
# File lib/hashery/castinghash.rb, line 118
def delete(k)
  super(key_proc[k])
end
fetch(k) click to toggle source
# File lib/hashery/castinghash.rb, line 70
def fetch(k)
  super(key_proc[k])
end
has_key?(k) click to toggle source
# File lib/hashery/castinghash.rb, line 85
def has_key?(k)
  super(key_proc[k])
end
key?(k) click to toggle source
# File lib/hashery/castinghash.rb, line 80
def key?(k)
  super(key_proc[k])
end
key_proc() click to toggle source
# File lib/hashery/castinghash.rb, line 29
def key_proc
  @key_proc
end
key_proc=(proc) click to toggle source
# File lib/hashery/castinghash.rb, line 34
def key_proc=(proc)
  @key_proc = proc.to_proc
end
merge!(other) click to toggle source

Same as update.

# File lib/hashery/castinghash.rb, line 128
def merge!(other)
  super(cast(other))
end
rekey(*args, &block) click to toggle source
# File lib/hashery/castinghash.rb, line 113
def rekey(*args, &block)
  dup.rekey!(*args, &block)
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/hashery/castinghash.rb, line 95
def rekey!(*args, &block)
  # for backward comptability (DEPRECATE?).
  block = args.pop.to_sym.to_proc if args.size == 1
  if args.empty?
    block = lambda{|k| k} unless block
    keys.each do |k|
      nk = block[k]
      self[nk] = delete(k) #if nk
    end
  else
    raise ArgumentError, "3 for 2" if block
    to, from = *args
    self[to] = delete(from) if has_key?(from)
  end
  self
end
replace(other) click to toggle source
# File lib/hashery/castinghash.rb, line 133
def replace(other)
  super(cast(other))
end
store(k, v) click to toggle source
# File lib/hashery/castinghash.rb, line 75
def store(k, v)
  super(key_proc[k], value_proc[v])
end
to_h() click to toggle source
Alias for: to_hash
to_hash() click to toggle source
# File lib/hashery/castinghash.rb, line 143
def to_hash
  h = {}; each{ |k,v| h[k] = v }; h
end
Also aliased as: to_h
update(other) click to toggle source
# File lib/hashery/castinghash.rb, line 123
def update(other)
  super(cast(other))
end
value_proc() click to toggle source
# File lib/hashery/castinghash.rb, line 39
def value_proc
  @value_proc
end
value_proc=(proc) click to toggle source
# File lib/hashery/castinghash.rb, line 44
def value_proc=(proc)
  @value_proc = proc.to_proc
end
values_at(*keys) click to toggle source
# File lib/hashery/castinghash.rb, line 138
def values_at(*keys)
  super(keys.map(&key_proc))
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.