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 Classes: Missing
Class Method Summary collapse
-
.[](*args) ⇒ Object
see Hash.[].
-
.try_convert(obj) ⇒ Object
see Hash#try_convert (ruby 1.9.x +).
Instance Method Summary collapse
-
#==(obj) ⇒ Object
see Hash#== comparison will use TofuKey comparison to retain the desired behavior.
-
#[](key) ⇒ Object
see Hash#[].
-
#[]=(key, value) ⇒ Object
see Hash#[]=.
-
#decode(key) ⇒ Object
see Hash#decode.
-
#default(key = nil) ⇒ Object
see Hash#default.
-
#delete(key, &block) ⇒ Object
see Hash#delete.
-
#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.
-
#each_key(&block) ⇒ Object
see Hash#each_key.
-
#each_key_encoded ⇒ Object
executes the block on each key as it is stored with TofuHash (e.g. the TofuKey).
-
#each_pair(&block) ⇒ Object
see Hash#each_pair.
-
#encode(key) ⇒ Object
see Hash#encode.
-
#fetch(key, default = Missing) ⇒ Object
see Hash#fetch.
-
#has_key?(key) ⇒ Boolean
(also: #include?, #key?, #member?)
see Hash#has_key? also aliased as include?, key?, and member?.
-
#index(value) ⇒ Object
see Hash#index.
-
#initialize(default = nil) ⇒ TofuHash
constructor
see Hash.new.
-
#keys ⇒ Object
see Hash#keys.
-
#merge(other_hash, &block) ⇒ Object
see Hash#merge.
-
#merge!(other_hash) ⇒ Object
(also: #update)
see Hash#merge! see Hash#update.
- #regular_delete_if ⇒ Object
- #regular_each_pair ⇒ Object
- #regular_fetch ⇒ Object
- #regular_reject! ⇒ Object
-
#reject(&block) ⇒ Object
see Hash#reject.
-
#reject!(&block) ⇒ Object
see Hash#reject!.
-
#replace(other_hash) ⇒ Object
see Hash#replace.
-
#select ⇒ Object
see Hash#select.
-
#shift ⇒ Object
see Hash#shift.
-
#sort ⇒ Object
see Hash#sort when called with a block, the keys passed to the block will be the original keys.
-
#store(key, value) ⇒ Object
see Hash#store.
-
#to_a ⇒ Object
see Hash#to_a.
-
#to_hash ⇒ Object
see Hash#to_hash.
-
#to_s ⇒ Object
see Hash#to_s.
-
#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 59 60 61 62 63 64 |
# 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() do |hash,key| if key.is_a? TofuKey then yield(hash, decode(key)) else yield(hash, key) end end else super end end |
Class Method Details
.[](*args) ⇒ Object
see Hash.[]
78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/tofuhash/tofuhash.rb', line 78 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 |
.try_convert(obj) ⇒ Object
see Hash#try_convert (ruby 1.9.x +)
331 332 333 334 335 336 337 338 339 |
# File 'lib/tofuhash/tofuhash.rb', line 331 def TofuHash.try_convert( obj ) h = super obj return nil if h.nil? result = TofuHash.new h.each_pair do |key,value| result[key] = value end return result end |
Instance Method Details
#==(obj) ⇒ Object
see Hash#== comparison will use TofuKey comparison to retain the desired behavior.
272 273 274 275 276 277 278 |
# File 'lib/tofuhash/tofuhash.rb', line 272 def == obj return false unless obj.is_a? Hash obj.each_pair do |key, value| return false unless (v = fetch( key ) {|k| Missing }).eql? value end return true end |
#[](key) ⇒ Object
see Hash#[]
134 135 136 |
# File 'lib/tofuhash/tofuhash.rb', line 134 def [] key super( encode(key) ) end |
#[]=(key, value) ⇒ Object
see Hash#[]=
94 95 96 |
# File 'lib/tofuhash/tofuhash.rb', line 94 def []= key,value super( encode(key), value ) end |
#decode(key) ⇒ Object
see Hash#decode
72 73 74 |
# File 'lib/tofuhash/tofuhash.rb', line 72 def decode( key ) key.decode end |
#default(key = nil) ⇒ Object
see Hash#default
186 187 188 |
# File 'lib/tofuhash/tofuhash.rb', line 186 def default(key = nil) super end |
#delete(key, &block) ⇒ Object
see Hash#delete
191 192 193 194 195 196 197 |
# File 'lib/tofuhash/tofuhash.rb', line 191 def delete(key, &block) if block_given? then super(encode(key)) {|e| yield e.decode } else super(encode(key)) end end |
#delete_if(&block) ⇒ Object
see Hash#delete_if
174 175 176 177 178 |
# File 'lib/tofuhash/tofuhash.rb', line 174 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.
181 182 183 |
# File 'lib/tofuhash/tofuhash.rb', line 181 def delete_unless #:yield: delete_if{ |key, value| ! yield(key, value) } end |
#each(&block) ⇒ Object
see Hash#each
101 102 103 104 105 |
# File 'lib/tofuhash/tofuhash.rb', line 101 def each &block self.regular_each_pair do |key, value| block.call( [decode(key), value] ) end end |
#each_key(&block) ⇒ Object
see Hash#each_key
108 109 110 111 112 |
# File 'lib/tofuhash/tofuhash.rb', line 108 def each_key &block self.regular_each_pair do |key, value| block.call(decode(key)) end end |
#each_key_encoded ⇒ Object
executes the block on each key as it is stored with TofuHash (e.g. the TofuKey)
115 116 117 118 119 |
# File 'lib/tofuhash/tofuhash.rb', line 115 def each_key_encoded self.regular_each_pair do |key, value| yield key end end |
#each_pair(&block) ⇒ Object
see Hash#each_pair
122 123 124 125 126 |
# File 'lib/tofuhash/tofuhash.rb', line 122 def each_pair &block self.regular_each_pair do |key,value| block.call( decode(key), value ) end end |
#encode(key) ⇒ Object
see Hash#encode
67 68 69 |
# File 'lib/tofuhash/tofuhash.rb', line 67 def encode( key ) TofuKey.new(key) end |
#fetch(key, default = Missing) ⇒ Object
see Hash#fetch
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 |
# File 'lib/tofuhash/tofuhash.rb', line 203 def fetch( key, default = Missing ) if block_given? then if default == Missing then super(encode(key)) {|e| yield e.decode } else super(encode(key),default) {|e| yield e.decode } end else if default == Missing then super(encode(key)) else super(encode(key),default) end end end |
#has_key?(key) ⇒ Boolean Also known as: include?, key?, member?
see Hash#has_key? also aliased as include?, key?, and member?
140 141 142 |
# File 'lib/tofuhash/tofuhash.rb', line 140 def has_key? key super( encode(key) ) end |
#index(value) ⇒ Object
see Hash#index
220 221 222 223 224 |
# File 'lib/tofuhash/tofuhash.rb', line 220 def index( value ) result = super return decode(result) if result.is_a? TofuKey return result end |
#keys ⇒ Object
see Hash#keys
148 149 150 151 |
# File 'lib/tofuhash/tofuhash.rb', line 148 def keys # collect will call each which decodes the key self.collect { |k,v| k } end |
#merge(other_hash, &block) ⇒ Object
see Hash#merge
281 282 283 |
# File 'lib/tofuhash/tofuhash.rb', line 281 def merge( other_hash, &block ) self.dup.merge!(other_hash, &block) end |
#merge!(other_hash) ⇒ Object Also known as: update
see Hash#merge! see Hash#update
287 288 289 290 291 292 293 294 295 296 297 298 299 300 |
# File 'lib/tofuhash/tofuhash.rb', line 287 def merge!( other_hash ) other_hash.each_pair do |key,value| if block_given? then if self.has_key? key then self[key] = yield( key, self[key], value ) else self[key] = value end else self[key] = value end end self end |
#regular_delete_if ⇒ Object
171 |
# File 'lib/tofuhash/tofuhash.rb', line 171 alias :regular_delete_if :delete_if |
#regular_each_pair ⇒ Object
98 |
# File 'lib/tofuhash/tofuhash.rb', line 98 alias :regular_each_pair :each_pair |
#regular_fetch ⇒ Object
199 |
# File 'lib/tofuhash/tofuhash.rb', line 199 alias :regular_fetch :fetch |
#regular_reject! ⇒ Object
308 |
# File 'lib/tofuhash/tofuhash.rb', line 308 alias_method 'regular_reject!','reject!' |
#reject(&block) ⇒ Object
see Hash#reject
304 305 306 |
# File 'lib/tofuhash/tofuhash.rb', line 304 def reject( &block ) self.dup.delete_if(&block) end |
#reject!(&block) ⇒ Object
see Hash#reject!
310 311 312 313 314 |
# File 'lib/tofuhash/tofuhash.rb', line 310 def reject!( &block ) self.regular_reject! do |key, value| block.call( decode(key), value) end end |
#replace(other_hash) ⇒ Object
see Hash#replace
227 228 229 230 231 232 |
# File 'lib/tofuhash/tofuhash.rb', line 227 def replace( other_hash ) self.clear other_hash.each_pair do |key, value| self[key]=value end end |
#select ⇒ Object
see Hash#select
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 |
# File 'lib/tofuhash/tofuhash.rb', line 235 def select if RUBY_VERSION >= "1.9" result = {} each_pair do |key,value| result[key]=value if yield(key,value) end return result else result = [] each_pair do |key,value| result << [key,value] if yield key,value end return result end end |
#shift ⇒ Object
see Hash#shift
252 253 254 255 256 |
# File 'lib/tofuhash/tofuhash.rb', line 252 def shift result = super result[0] = decode(result[0]) return result end |
#sort ⇒ Object
see Hash#sort when called with a block, the keys passed to the block will be the original keys.
260 261 262 263 264 265 266 267 268 |
# File 'lib/tofuhash/tofuhash.rb', line 260 def sort return super if RUBY_VERSION >= "1.9" if block_given? result = super {|kv1,kv2| yield( [decode(kv1[0]),kv1[1]], [decode(kv2[0]),kv2[1]] ) } else result = super end result.collect {|a| [a[0].decode, a[1]] } end |
#store(key, value) ⇒ Object
see Hash#store
129 130 131 |
# File 'lib/tofuhash/tofuhash.rb', line 129 def store key, value super( encode(key), value ) end |
#to_a ⇒ Object
see Hash#to_a
163 164 165 166 167 168 169 |
# File 'lib/tofuhash/tofuhash.rb', line 163 def to_a aux = [] self.each do |key,value| aux << [ key, value ] end aux end |
#to_hash ⇒ Object
see Hash#to_hash
317 318 319 320 321 322 323 |
# File 'lib/tofuhash/tofuhash.rb', line 317 def to_hash result = Hash.new self.each_pair do |key, value| result[key]=value end result end |
#to_s ⇒ Object
see Hash#to_s
326 327 328 |
# File 'lib/tofuhash/tofuhash.rb', line 326 def to_s self.to_hash.to_s end |
#values_at(*args) ⇒ Object
see Hash#values_at
156 157 158 159 160 |
# File 'lib/tofuhash/tofuhash.rb', line 156 def values_at( *args ) values = Array.new args.each { |key| values << self[key] } values end |