Class: Hashery::CastingHash

Inherits:
CRUDHash show all
Defined in:
lib/hashery/casting_hash.rb

Overview

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

Constant Summary

Constants inherited from CRUDHash

Hashery::CRUDHash::NA

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from CRUDHash

#<<, #[], #[]=, auto, #cast_key, #default_proc, #delete, #each, #fetch, #key?, #key_proc, #key_proc=, #merge, #read, #retrieve, #update, #values_at

Methods inherited from Hash

create, #rekey, #rekey!, #retrieve, #to_stash

Constructor Details

#initialize(default = nil, &cast_proc) ⇒ CastingHash

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

Parameters:

  • default (defaults to: nil)

    Default value.

  • cast_proc

    Casting procedure.



34
35
36
37
# File 'lib/hashery/casting_hash.rb', line 34

def initialize(default=nil, &cast_proc)
  @cast_proc = cast_proc
  super(default, &nil)
end

Class Method Details

.[](hash) ⇒ Object

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

Examples:

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

Parameters:

  • hash

    Hash-like object.

Returns:

  • ‘CastingHash`.



21
22
23
24
25
# File 'lib/hashery/casting_hash.rb', line 21

def self.[](hash)
  s = new
  hash.each{ |k,v| s[k] = v }
  s
end

Instance Method Details

#cast(hash) ⇒ Object (private)

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

Parameters:

  • hash

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

Returns:

  • a recasted ‘Hash`.



142
143
144
145
146
147
148
149
# 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) ⇒ Object (private)

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

Parameters:

  • key

    Key of entry.

  • value

    Value of entry.

Returns:

  • ‘Array` of key-value pair.



127
128
129
130
131
132
133
# 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

#cast_proc(&proc) ⇒ Object

The cast procedure.

Parameters:

  • proc

    Casting procedure.

Returns:

  • ‘Proc` used for casting.



46
47
48
49
# File 'lib/hashery/casting_hash.rb', line 46

def cast_proc(&proc)
  @cast_proc = proc if proc
  @cast_proc
end

#cast_proc=(proc) ⇒ Object

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

Parameters:

  • proc

    Casting procedure.

Returns:

  • proc.

Raises:

  • (ArgumentError)


59
60
61
62
# 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!Object

TODO:

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

Recast all entries via the cast procedure.

Returns:

  • self.



112
113
114
# File 'lib/hashery/casting_hash.rb', line 112

def recast!
  replace self
end

#replace(other) ⇒ Object

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

Parameters:

  • other

    Hash-like object.

Returns:

  • self.



87
88
89
# File 'lib/hashery/casting_hash.rb', line 87

def replace(other)
  super cast(other)
end

#store(key, value) ⇒ Object

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.

Parameters:

  • key

    Key of entry.

  • value

    Value of entry.

Returns:

  • the value.



74
75
76
# File 'lib/hashery/casting_hash.rb', line 74

def store(key, value)
  super(*cast_pair(key, value))
end

#to_hashObject Also known as: to_h

Convert the CastingHash to a regular Hash.

Returns:

  • an ordinary ‘Hash`.



96
97
98
# File 'lib/hashery/casting_hash.rb', line 96

def to_hash
  h = {}; each{ |k,v| h[k] = v }; h
end