Class: Hashery::CastingHash
- 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
Class Method Summary collapse
-
.[](hash) ⇒ Object
Like ‘#new` but can take a priming Hash or Array-pairs.
Instance Method Summary collapse
-
#cast(hash) ⇒ Object
private
Cast a given
hash
according to the ‘#key_proc` and `#value_proc`. -
#cast_pair(key, value) ⇒ Object
private
If ‘cast_proc` is defined then use it to process key-value pair, otherwise return them as is.
-
#cast_proc(&proc) ⇒ Object
The cast procedure.
-
#cast_proc=(proc) ⇒ Object
Set ‘cast_proc`.
-
#initialize(default = nil, &cast_proc) ⇒ CastingHash
constructor
Unlike traditional Hash a CastingHash’s block argument coerces key/value pairs when #store is called.
-
#recast! ⇒ Object
Recast all entries via the cast procedure.
-
#replace(other) ⇒ Object
Replace current entries with those from another Hash, or Hash-like object.
-
#store(key, value) ⇒ Object
CRUD method for create and update.
-
#to_hash ⇒ Object
(also: #to_h)
Convert the CastingHash to a regular Hash.
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.
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.
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`.
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.
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.
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.
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
Isn’t this the same as ‘#rehash`?
Recast all entries via the cast procedure.
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.
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.
74 75 76 |
# File 'lib/hashery/casting_hash.rb', line 74 def store(key, value) super(*cast_pair(key, value)) end |
#to_hash ⇒ Object Also known as: to_h
Convert the CastingHash to a regular Hash.
96 97 98 |
# File 'lib/hashery/casting_hash.rb', line 96 def to_hash h = {}; each{ |k,v| h[k] = v }; h end |