Module: Sqreen::CallCountable

Included in:
Rules::RuleCB
Defined in:
lib/sqreen/call_countable.rb

Overview

A module that will dynamically had call_counts to the pre/post/failing callbacks

Constant Summary collapse

PRE =
'pre'.freeze
POST =
'post'.freeze
FAILING =
'failing'.freeze
COUNT_CALLS =
'sqreen_call_counts'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#call_count_intervalObject (readonly)

Returns the value of attribute call_count_interval.



57
58
59
# File 'lib/sqreen/call_countable.rb', line 57

def call_count_interval
  @call_count_interval
end

#call_countsObject (readonly)

Returns the value of attribute call_counts.



56
57
58
# File 'lib/sqreen/call_countable.rb', line 56

def call_counts
  @call_counts
end

Instance Method Details

#count_callback_calls(count) ⇒ Object

Hook the necessary callback function The module being decorated is expected to have a record_observation & rulespack_id & rule_name method available (like RuleCallback)

Parameters:

  • count (Hash)

    hash of callback names => count



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/sqreen/call_countable.rb', line 15

def count_callback_calls(count)
  base = self.class
  @call_count_interval = 0
  return if count.to_i == 0
  @call_counts = {}
  @call_count_interval = count
  @call_count_names = {}
  %w(pre post failing).each do |cb|
    next unless base.method_defined?(cb)
    @call_counts[cb] = 0
    @call_count_names[cb] = "#{rulespack_id}/#{rule_name}/#{cb}".freeze
    defd = base.instance_variable_defined?("@call_count_hooked_#{cb}")
    next if defd && base.instance_variable_get("@call_count_hooked_#{cb}")
    base.send(:alias_method, "#{cb}_without_count", cb)
    base.send(:alias_method, cb, "#{cb}_with_count")
    base.instance_variable_set("@call_count_hooked_#{cb}", true)
  end
end

#failing_with_count(rv, inst, args, budget = nil, &block) ⇒ Object



50
51
52
53
54
# File 'lib/sqreen/call_countable.rb', line 50

def failing_with_count(rv, inst, args, budget = nil, &block)
  ret = failing_without_count(rv, inst, args, budget, &block)
  count_calls('failing')
  ret
end

#post_with_count(rv, inst, args, budget = nil, &block) ⇒ Object



44
45
46
47
48
# File 'lib/sqreen/call_countable.rb', line 44

def post_with_count(rv, inst, args, budget = nil, &block)
  ret = post_without_count(rv, inst, args, budget, &block)
  count_calls('post')
  ret
end

#pre_with_count(inst, args, budget = nil, &block) ⇒ Object



38
39
40
41
42
# File 'lib/sqreen/call_countable.rb', line 38

def pre_with_count(inst, args, budget = nil, &block)
  ret = pre_without_count(inst, args, budget, &block)
  count_calls('pre')
  ret
end