Class | Hashery::CRUDHash |
In: |
lib/hashery/crud_hash.rb
|
Parent: | ::Hash |
The CRUDHash is essentailly the same as the Hash class, but it reduces the the set of necessary methods to the fundametal CRUD requirements. All other methods route through these CRUD methods. This is a better general design, although it is, of course, a little bit slower. The utility of this class becomes appearent when subclassing or delegating, as only a handful of methods need to be changed for all other methods to work accordingly.
In addition to the CRUD features, CRUDHash supports a `key_proc`, akin to `default_proc`, that can be used to normalize keys.
The CRUD methods are:
In addition to these main methods, there are these supporting "CRUD" methods:
NA | = | Object.new | Dummy object for null arguments. |
Alternate to new which auto-creates sub-dictionaries as needed. By default the `default_proc` procuced a empty Hash and is self-referential so every such Hash also has the same `default_proc`.
args - Pass-thru arguments to `new`. block - Alternate internal procedure for default proc.
Examples
d = CRUDHash.auto d["a"]["b"]["c"] = "abc" #=> { "a"=>{"b"=>{"c"=>"abc"}}}
Returns `Hash`.
Operator for `store`.
key - The `Object` to act as indexing key. value - The `Object` to associate with key.
Returns value.
Allow `default_proc` to take a block.
block - The `Proc` object to set the `default_proc`.
Returns `Proc`, the `default_proc`.
CRUD method for read. This method gets the value for a given key. An error is raised if the key is not present, but an optional argument can be provided to be returned instead.
key - Hash key to lookup. default - Value to return if key is not present.
Raises KeyError when key is not found and default has not been given.
Returns the `Object` that is the Hash entry‘s value.
Set `key_proc`.
Examples
ch = CRUDHash.new ch.key_proc = Proc.new{ |key| key.to_sym }
Returns `Proc`.
Method for reading value. Returns `nil` if key is not present.
Note that this method used to be the CRUD method instead of retrieve. Complaints about read being indicative of an IO object (though in my opinion that is a bad asumption) have led to this method‘s deprecation.
key - Hash key to lookup.
Returns value of Hash entry.
Like fetch but returns the results of calling `default_proc`, if defined, otherwise `default`.
key - Hash key to lookup.
Returns value of Hash entry or `nil`.
CRUD method for create and update.
key - The `Object` to act as indexing key. value - The `Object` to associate with key.
Returns value.
Update the Hash with another hash.
other - Other hash or hash-like object to add to the hash.
Returns self.