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.



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

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



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

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

Instance Method Details

#[](key) ⇒ Object



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

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

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

  nil
end

#[]=(key, val) ⇒ Object



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

def []=(key, val)
  case key
  when Regexp
    @regexes << key
  end
  @hash[key] = val
end

#respond_to_missing?(*args) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#update(value) ⇒ Object



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

def update(value)
  value.each do |key, val|
    self[key] = val
  end

  self
end