class Hashery::CastingHash

CastingHash is just like CRUDHash, except that both keys and values can be passed through casting procedures.

Public Class Methods

[](hash) click to toggle source

Like ‘#new` but can take a priming Hash or Array-pairs.

hash - Hash-like object.

Examples

CastingHash[:a,1,:b,2]

Returns ‘CastingHash`.

# File lib/hashery/casting_hash.rb, line 21
def self.[](hash)
  s = new
  hash.each{ |k,v| s[k] = v }
  s
end
new(default=nil, &cast_proc) click to toggle source

Unlike traditional Hash a CastingHash’s block argument coerces key/value pairs when store is called.

default - Default value. cast_proc - Casting procedure.

Calls superclass method
# File lib/hashery/casting_hash.rb, line 34
def initialize(default=nil, &cast_proc)
  @cast_proc = cast_proc
  super(default, &nil)
end

Public Instance Methods

cast_proc(&proc) click to toggle source

The cast procedure.

proc - Casting procedure.

Returns ‘Proc` used for casting.

# File lib/hashery/casting_hash.rb, line 46
def cast_proc(&proc)
  @cast_proc = proc if proc
  @cast_proc
end
cast_proc=(proc) click to toggle source

Set ‘cast_proc`. This procedure must take two arguments (`key, value`) and return the same.

proc - Casting procedure.

Returns proc.

# File lib/hashery/casting_hash.rb, line 59
def cast_proc=(proc)
  raise ArgumentError unless Proc === proc or NilClass === proc
  @cast_proc = proc
end
recast!() click to toggle source

Recast all entries via the cast procedure.

TODO: Isn’t this the same as ‘#rehash`?

Returns self.

# File lib/hashery/casting_hash.rb, line 112
def recast!
  replace self
end
replace(other) click to toggle source

Replace current entries with those from another Hash, or Hash-like object. Each entry is run through the casting procedure as it is added.

other - Hash-like object.

Returns self.

Calls superclass method
# File lib/hashery/casting_hash.rb, line 87
def replace(other)
  super cast(other)
end
store(key, value) click to toggle source

CRUD method for create and update. Unlike the parent class the key, value pair are passed threw the cast_proc before being set in the underlying hash table.

key - Key of entry. value - Value of entry.

Returns the value.

Calls superclass method
# File lib/hashery/casting_hash.rb, line 74
def store(key, value)
  super(*cast_pair(key, value))
end
to_h()

Returns an ordinary ‘Hash`.

Alias for: to_hash
to_hash() click to toggle source

Convert the CastingHash to a regular Hash.

Returns an ordinary ‘Hash`.

# File lib/hashery/casting_hash.rb, line 96
def to_hash
  h = {}; each{ |k,v| h[k] = v }; h
end
Also aliased as: to_h

Private Instance Methods

cast(hash) click to toggle source

Cast a given hash according to the ‘#key_proc` and `#value_proc`.

hash - A ‘Hash` or anything the responds to `#each` like a hash.

Returns a recasted ‘Hash`.

# File lib/hashery/casting_hash.rb, line 142
def cast(hash)
  h = {}
  hash.each do |k,v|
    k, v = cast_pair(k, v)
    h[k] = v
  end
  h
end
cast_pair(key, value) click to toggle source

If ‘cast_proc` is defined then use it to process key-value pair, otherwise return them as is.

key - Key of entry. value - Value of entry.

Returns ‘Array` of key-value pair.

# File lib/hashery/casting_hash.rb, line 127
def cast_pair(key, value)
  if cast_proc
    return cast_proc.call(key, value)
  else
    return key, value
  end
end