Class: BBK::App::Matchers::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/bbk/app/matchers/base.rb

Direct Known Subclasses

DeliveryInfo, Full, Headers, Payload

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#ruleObject (readonly)

Returns the value of attribute rule.



6
7
8
# File 'lib/bbk/app/matchers/base.rb', line 6

def rule
  @rule
end

Instance Method Details

#==(other) ⇒ Object



12
13
14
# File 'lib/bbk/app/matchers/base.rb', line 12

def ==(other)
  self.class == other.class && rule == other.rule
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/bbk/app/matchers/base.rb', line 16

def eql?(other)
  self == other
end

#hashObject



8
9
10
# File 'lib/bbk/app/matchers/base.rb', line 8

def hash
  (self.class.to_s + rule.to_s).hash
end

#keys_deep(data) ⇒ Object



20
21
22
23
24
25
26
27
# File 'lib/bbk/app/matchers/base.rb', line 20

def keys_deep(data)
  data.inject([]) do |res, p|
    k, _v = p
    res.push k
    res += keys_deep(data[k]) if data[k].is_a? Hash
    res
  end
end

#match_impl(rule, data) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/bbk/app/matchers/base.rb', line 29

def match_impl(rule, data)
  result = rule.each_with_object({}.with_indifferent_access) do |p, res|
    k, v = p

    if v == :any && data.key?(k.to_sym)
      res[k.to_sym] = data[k.to_sym]
    elsif v.is_a? Hash
      res[k.to_sym] = match_impl(v, data[k.to_sym] || {})
    elsif v == data[k.to_sym]
      res[k.to_sym] = data[k.to_sym]
    end
  end

  result.keys.size == rule.keys.size && keys_deep(result).count >= keys_deep(rule).count ? result : nil
end