Class: Sqreen::CBTree

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/sqreen/cb_tree.rb

Instance Method Summary collapse

Constructor Details

#initializeCBTree

Callbacks tree: class methods position



16
17
18
# File 'lib/sqreen/cb_tree.rb', line 16

def initialize
  @by_class = {}
end

Instance Method Details

#add(cb) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/sqreen/cb_tree.rb', line 20

def add(cb)
  @by_class[cb.klass] = {} unless @by_class[cb.klass]

  cb_klass = @by_class[cb.klass]
  cb_klass[cb.method] ||= [nil, nil, nil]

  methods = cb_klass[cb.method]

  if cb.pre?
    methods[0] ||= []
    add_to_array(methods[0], cb)
  end
  if cb.post?
    methods[1] ||= []
    add_to_array(methods[1], cb)
  end
  if cb.failing? # rubocop:disable Style/GuardClause
    methods[2] ||= []
    add_to_array(methods[2], cb)
  end
end

#eachObject



81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/sqreen/cb_tree.rb', line 81

def each
  @by_class.each_value do |values|
    values.each_value do |cbs|
      cbs.each do |cb_ary|
        next unless cb_ary
        cb_ary.each do |cb|
          yield cb
        end
      end
    end
  end
end

#get(klass, method) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/sqreen/cb_tree.rb', line 61

def get(klass, method)
  k = @by_class[klass]
  unless k
    Sqreen.log.debug { format('Error: no cb registered for class %s (%s)', klass.inspect, klass.class) }
    Sqreen.log.debug { inspect }
    return [nil, nil, nil]
  end
  cbs = k[method]
  unless cbs
    Sqreen.log.debug { format('Error: no cbs registered for method %s.%s', klass, method) }
    Sqreen.log.debug { inspect }
    return [nil, nil, nil]
  end
  cbs
rescue StandardError => e
  Sqreen.log.warn "we catched an exception getting cbs: #{e.inspect}"
  Sqreen::RemoteException.record(e)
  [nil, nil, nil]
end

#remove(cb) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/sqreen/cb_tree.rb', line 42

def remove(cb)
  by_meth = @by_class[cb.klass]
  return unless by_meth
  types = by_meth[cb.method]

  if cb.pre? && types[0]
    types[0].delete(cb)
    types[0] = nil if types[0].empty?
  end
  if cb.post? && types[1]
    types[1].delete(cb)
    types[1] = nil if types[1].empty?
  end
  if cb.failing? && types[2] # rubocop:disable Style/GuardClause
    types[2].delete(cb)
    types[2] = nil if types[2].empty?
  end
end