Class: TofuHash
- Inherits:
-
Hash
- Object
- Hash
- TofuHash
- Defined in:
- lib/tofuhash/tofuhash.rb
Overview
TofuHash
Links:
A varient of Hash which can change the lookup behavior of keys.
The default TofuHash will match Symbol and String without reguard to case. By subclassing TofuKey, this behavior can be changed.
License:
(The MIT License + Free Software Foundation Advertising Prohibition)
Copyright © 2007 Gregory N. Houston
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Except as contained in this notice, the name(s) of the above copyright holders shall not be used in advertising or otherwise to promote the sale, use or other dealings in this Software without prior written authorization.
Defined Under Namespace
Modules: Version
Class Method Summary collapse
-
.[](*args) ⇒ Object
see Hash.[].
Instance Method Summary collapse
-
#[](key) ⇒ Object
see Hash#[].
-
#[]=(key, value) ⇒ Object
see Hash#[]=.
-
#decode(key) ⇒ Object
see Hash#decode.
-
#delete_if(&block) ⇒ Object
see Hash#delete_if.
-
#delete_unless ⇒ Object
Deletes every key-value pair for which block evaluates to false.
-
#each(&block) ⇒ Object
see Hash#each.
-
#encode(key) ⇒ Object
see Hash#encode.
-
#has_key?(key) ⇒ Boolean
see Hash#has_key?.
-
#include?(key) ⇒ Boolean
see Hash#include?.
-
#initialize(default = nil) ⇒ TofuHash
constructor
see Hash.new.
-
#keys ⇒ Object
see Hash#keys.
- #regular_delete_if ⇒ Object
- #regular_each ⇒ Object
-
#store(key, value) ⇒ Object
see Hash#store.
-
#to_a ⇒ Object
see Hash#to_a.
-
#values_at(*args) ⇒ Object
see Hash#values_at.
Constructor Details
#initialize(default = nil) ⇒ TofuHash
see Hash.new
49 50 51 52 53 54 55 56 57 58 |
# File 'lib/tofuhash/tofuhash.rb', line 49 def initialize ( default = nil ) if block_given? if default raise ArgumentError, "Can't initialize Hash with both a default value and a block" end super() { |hash,key| yield(hash, decode(key)) } else super end end |
Class Method Details
.[](*args) ⇒ Object
see Hash.[]
72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/tofuhash/tofuhash.rb', line 72 def [](*args) if args.size == 1 && args[0].instance_of?( Object::Hash ) h = TofuHash.new args[0].each { |k,v| h.store( k,v ) } h elsif (args.size % 2 != 0) raise ArgumentError, "odd number of arguments for TofuHash" else h = TofuHash.new 1.step( args.size, 2 ) { |i| h.store(args[i-1],args[i]) } h end end |
Instance Method Details
#[](key) ⇒ Object
see Hash#[]
107 108 109 |
# File 'lib/tofuhash/tofuhash.rb', line 107 def [] key super( encode(key) ) end |
#[]=(key, value) ⇒ Object
see Hash#[]=
88 89 90 |
# File 'lib/tofuhash/tofuhash.rb', line 88 def []= key,value super( encode(key), value ) end |
#decode(key) ⇒ Object
see Hash#decode
66 67 68 |
# File 'lib/tofuhash/tofuhash.rb', line 66 def decode( key ) key.decode end |
#delete_if(&block) ⇒ Object
see Hash#delete_if
148 149 150 151 152 |
# File 'lib/tofuhash/tofuhash.rb', line 148 def delete_if(&block) self.regular_delete_if do |key, value| block.call( decode(key), value) end end |
#delete_unless ⇒ Object
Deletes every key-value pair for which block evaluates to false.
155 156 157 |
# File 'lib/tofuhash/tofuhash.rb', line 155 def delete_unless #:yield: delete_if{ |key, value| ! yield(key, value) } end |
#each(&block) ⇒ Object
see Hash#each
95 96 97 98 99 |
# File 'lib/tofuhash/tofuhash.rb', line 95 def each &block self.regular_each do |key, value| block.call( decode(key), value ) end end |
#encode(key) ⇒ Object
see Hash#encode
61 62 63 |
# File 'lib/tofuhash/tofuhash.rb', line 61 def encode( key ) TofuKey.new(key) end |
#has_key?(key) ⇒ Boolean
see Hash#has_key?
112 113 114 |
# File 'lib/tofuhash/tofuhash.rb', line 112 def has_key? key super( encode(key) ) end |
#include?(key) ⇒ Boolean
see Hash#include?
141 142 143 |
# File 'lib/tofuhash/tofuhash.rb', line 141 def include?(key) super(encode(key)) end |
#keys ⇒ Object
see Hash#keys
117 118 119 120 |
# File 'lib/tofuhash/tofuhash.rb', line 117 def keys # collect will call each which decodes the key self.collect { |k,v| k } end |
#regular_delete_if ⇒ Object
145 |
# File 'lib/tofuhash/tofuhash.rb', line 145 alias :regular_delete_if :delete_if |
#regular_each ⇒ Object
92 |
# File 'lib/tofuhash/tofuhash.rb', line 92 alias :regular_each :each |
#store(key, value) ⇒ Object
see Hash#store
102 103 104 |
# File 'lib/tofuhash/tofuhash.rb', line 102 def store key, value super( encode(key), value ) end |
#to_a ⇒ Object
see Hash#to_a
132 133 134 135 136 137 138 |
# File 'lib/tofuhash/tofuhash.rb', line 132 def to_a aux = [] self.each do |key,value| aux << [ key, value ] end aux end |
#values_at(*args) ⇒ Object
see Hash#values_at
125 126 127 128 129 |
# File 'lib/tofuhash/tofuhash.rb', line 125 def values_at( *args ) values = Array.new args.each { |key| values << self[key] } values end |