Class: FuzzyHash

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

Instance Method Summary collapse

Constructor Details

#initialize(init_hash = nil) ⇒ FuzzyHash

Returns a new instance of FuzzyHash.



5
6
7
8
9
10
11
12
# File 'lib/fuzzy_hash.rb', line 5

def initialize(init_hash = nil)
  @fuzzies = []
  @hash_reverse = {}
  @fuzzies_reverse = {}
  @fuzzy_hash = {}
  @hash = {}
  init_hash.each{ |key,value| self[key] = value } if init_hash
end

Instance Method Details

#==(o) ⇒ Object



27
28
29
30
31
# File 'lib/fuzzy_hash.rb', line 27

def ==(o)
  o.is_a?(FuzzyHash)
  o.send(:hash) == hash &&
  o.send(:fuzzies) == fuzzies
end

#[](key) ⇒ Object



86
87
88
89
90
# File 'lib/fuzzy_hash.rb', line 86

def [](key)
  (hash.key?(key) && hash[key])  ||
    ((lookup = fuzzy_lookup(key)) && lookup && lookup.first) ||
    fuzzy_hash[key]
end

#[]=(key, value) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/fuzzy_hash.rb', line 54

def []=(key, value)
  if Regexp === key
    fuzzies.delete_if{|f| f.first.inspect.hash == key.inspect.hash}
    fuzzies_reverse.delete_if{|k, v| v[1].inspect.hash == key.inspect.hash}
    hash_reverse.delete_if{|k,v| v.inspect.hash == key.inspect.hash}

    fuzzy_hash[key] = value
    fuzzies << [key, value]
    reset_fuzz_test!
    fuzzies_reverse[value] = [fuzzies.size - 1, key, value]
  else
    hash[key] = value
    hash_reverse.delete_if{|k,v| v.hash == key.hash}
    hash_reverse[value] = key
  end
  value
end

#clearObject



14
15
16
17
18
19
# File 'lib/fuzzy_hash.rb', line 14

def clear
  hash.clear
  fuzzies.clear
  hash_reverse.clear
  fuzzies_reverse.clear
end

#delete_value(value) ⇒ Object



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

def delete_value(value)
  hash.delete(hash_reverse[value]) || ((rr = fuzzies_reverse[value]) && fuzzies.delete_at(rr[0]))
end

#eachObject



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

def each
  hash.each{|k,v| yield k,v }
  fuzzies.each{|v| yield v.first, v.last }
end

#empty?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/fuzzy_hash.rb', line 33

def empty?
  hash.empty? && fuzzies.empty?
end

#keysObject



37
38
39
# File 'lib/fuzzy_hash.rb', line 37

def keys
  hash.keys + fuzzy_hash.keys
end

#match_with_result(key) ⇒ Object



92
93
94
95
96
97
98
# File 'lib/fuzzy_hash.rb', line 92

def match_with_result(key)
  if hash.key?(key)
    [hash[key], key]
  else
    fuzzy_lookup(key)
  end
end

#replace(src, dest) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/fuzzy_hash.rb', line 72

def replace(src, dest)
  if hash_reverse.key?(src)
    key = hash_reverse[src]
    hash[key] = dest
    hash_reverse.delete(src)
    hash_reverse[dest] = key
  elsif fuzzies_reverse.key?(src)
    key = fuzzies_reverse[src]
    fuzzies[rkey[0]] = [rkey[1], dest]
    fuzzies_reverse.delete(src)
    fuzzies_reverse[dest] = [rkey[0], rkey[1], dest]
  end
end

#sizeObject Also known as: count



21
22
23
# File 'lib/fuzzy_hash.rb', line 21

def size
  hash.size + fuzzies.size
end

#valuesObject



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

def values
  hash.values + fuzzies.collect{|r| r.last}
end