Class: Praxis::Types::FuzzyHash

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

Instance Method Summary collapse

Constructor Details

#initialize(value = {}) ⇒ FuzzyHash

Returns a new instance of FuzzyHash.



5
6
7
8
9
# File 'lib/praxis/types/fuzzy_hash.rb', line 5

def initialize(value={})
  @hash = {}
  @regexes = []
  update(value)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(*args, &block) ⇒ Object



38
39
40
# File 'lib/praxis/types/fuzzy_hash.rb', line 38

def method_missing(*args, &block)
  @hash.send(*args, &block)
end

Instance Method Details

#[](k) ⇒ Object



27
28
29
30
31
32
33
34
35
36
# File 'lib/praxis/types/fuzzy_hash.rb', line 27

def [](k)
  return @hash[k] if @hash.key?(k)

  k = k.to_s
  @regexes.each do |regex|
    return @hash[regex] if regex.match(k)
  end

  nil
end

#[]=(k, v) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/praxis/types/fuzzy_hash.rb', line 19

def []=(k,v)
  case k
  when Regexp
    @regexes << k
  end
  @hash[k] = v
end

#respond_to_missing?(*args) ⇒ Boolean

Returns:

  • (Boolean)


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

def respond_to_missing?(*args)
  @hash.respond_to?(*args)
end

#update(value) ⇒ Object



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

def update(value)
  value.each do |k,v|
    self[k] = v
  end

  self
end