Class: Containers::FuzzyHash

Inherits:
Object
  • Object
show all
Defined in:
lib/duct_tape/fuzzy_hash.rb

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ FuzzyHash

Returns a new instance of FuzzyHash.



3
4
5
6
7
8
9
# File 'lib/duct_tape/fuzzy_hash.rb', line 3

def initialize(&block)
  @mapping_proc = block_given? ? block : lambda { |key| [*key] }
  @auto_array = Containers::AutoassociativeArray.new
  @hash = {}
  @values = {}
  self
end

Instance Method Details

#[](key) ⇒ Object



11
12
13
14
15
16
# File 'lib/duct_tape/fuzzy_hash.rb', line 11

def [](key)
  mapped_key = @mapping_proc[key]
  type_assert(mapped_key, Array)
  match = @auto_array.partial_match(*mapped_key)
  @values[match[0]]
end

#[]=(key, val) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/duct_tape/fuzzy_hash.rb', line 18

def []=(key, val)
  mapped_key = @mapping_proc[key]
  type_assert(mapped_key, Array)
  @hash[key] = val
  @auto_array.insert(*mapped_key)
  @values[mapped_key] = val
end

#clearObject



35
36
37
38
39
40
# File 'lib/duct_tape/fuzzy_hash.rb', line 35

def clear
  @hash.clear
  @values.clear
  @auto_array.clear
  self
end

#dupObject



65
66
67
68
69
# File 'lib/duct_tape/fuzzy_hash.rb', line 65

def dup
  ret = self.class.new(&@mapping_proc)
  @hash.each { |key,val| ret[key] = val }
  ret
end

#empty?Boolean

Returns:



26
27
28
# File 'lib/duct_tape/fuzzy_hash.rb', line 26

def empty?
  @hash.empty?
end

#inspectObject



42
43
44
# File 'lib/duct_tape/fuzzy_hash.rb', line 42

def inspect
  @hash.inspect
end

#keysObject



46
47
48
# File 'lib/duct_tape/fuzzy_hash.rb', line 46

def keys
  @hash.keys
end

#lengthObject Also known as: size



30
31
32
# File 'lib/duct_tape/fuzzy_hash.rb', line 30

def length
  @hash.size
end

#to_sObject

TODO def method_missing(*args, &block)

ret = @hash.__send__(*args, &block)
rebuild_hash
ret

end



61
62
63
# File 'lib/duct_tape/fuzzy_hash.rb', line 61

def to_s
  @hash.to_s
end

#valuesObject



50
51
52
# File 'lib/duct_tape/fuzzy_hash.rb', line 50

def values
  @hash.values
end