Class: Gardefou::Profile

Inherits:
Object
  • Object
show all
Defined in:
lib/gardefou/profile.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config: nil, max_calls: nil, on_violation: nil, on_violation_max_calls: nil, on_violation_duplicate_call: nil) ⇒ Profile

Returns a new instance of Profile.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/gardefou/profile.rb', line 10

def initialize(config: nil, max_calls: nil, on_violation: nil,
               on_violation_max_calls: nil, on_violation_duplicate_call: nil)
  # Load base data from file or hash
  data = {}

  if config.is_a?(String)
    # Load from file
    content = File.read(config)
    data = if config.end_with?('.yaml', '.yml')
             YAML.safe_load(content)
           else
             JSON.parse(content)
           end
  elsif config.is_a?(Hash)
    data = config.dup
  end

  # Override with explicit options
  data['max_calls'] = max_calls unless max_calls.nil?
  data['on_violation'] = on_violation unless on_violation.nil?
  data['on_violation_max_calls'] = on_violation_max_calls unless on_violation_max_calls.nil?
  data['on_violation_duplicate_call'] = on_violation_duplicate_call unless on_violation_duplicate_call.nil?

  # Assign settings with defaults
  @max_calls = data['max_calls'] || -1
  @on_violation = data['on_violation'] || 'raise'
  @on_violation_max_calls = data['on_violation_max_calls'] || @on_violation
  @on_violation_duplicate_call = data['on_violation_duplicate_call'] || @on_violation

  @call_count = 0
  @call_signatures = Set.new

  # Track which rules were explicitly configured
  @max_calls_enabled = data.key?('max_calls') && @max_calls >= 0
  @dup_enabled = data.key?('on_violation_duplicate_call')
end

Instance Attribute Details

#call_countObject (readonly)

Returns the value of attribute call_count.



8
9
10
# File 'lib/gardefou/profile.rb', line 8

def call_count
  @call_count
end

#max_callsObject (readonly)

Returns the value of attribute max_calls.



8
9
10
# File 'lib/gardefou/profile.rb', line 8

def max_calls
  @max_calls
end

#on_violationObject (readonly)

Returns the value of attribute on_violation.



8
9
10
# File 'lib/gardefou/profile.rb', line 8

def on_violation
  @on_violation
end

#on_violation_duplicate_callObject (readonly)

Returns the value of attribute on_violation_duplicate_call.



8
9
10
# File 'lib/gardefou/profile.rb', line 8

def on_violation_duplicate_call
  @on_violation_duplicate_call
end

#on_violation_max_callsObject (readonly)

Returns the value of attribute on_violation_max_calls.



8
9
10
# File 'lib/gardefou/profile.rb', line 8

def on_violation_max_calls
  @on_violation_max_calls
end

Instance Method Details

#check(fn_name = nil, args = [], kwargs = {}) ⇒ Object



47
48
49
50
# File 'lib/gardefou/profile.rb', line 47

def check(fn_name = nil, args = [], kwargs = {})
  check_max_call if @max_calls_enabled
  check_duplicate(fn_name, args, kwargs) if @dup_enabled
end