Class: PotatoDebugger::Hook

Inherits:
Object
  • Object
show all
Includes:
PotatoDebugger
Defined in:
lib/potato_debugger/hooks.rb

Constant Summary

Constants included from PotatoDebugger

VERSION

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass, **args) ⇒ Hook

Returns a new instance of Hook.



7
8
9
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/potato_debugger/hooks.rb', line 7

def initialize(klass, **args)
  @cache = {}
  @class = klass
  @overview ||= []
  @options = {}

  args.each {|arg, value| @options[arg] = value }

  klass.instance_variable_set(:@debugger_instance, self)
  klass.class.prepend(HookHelper)
  self.class.attr_reader :cache, :overview

  klass.instance_variables.each do |instance_var|
    method_name = "#{instance_var}="
    method_name[0] = ''
    attribute = instance_var.to_s[1..25]

    @cache[attribute.to_sym] = []

    klass.define_singleton_method("tracked_#{attribute}") { ap @debugger_instance.cache[attribute] }

    HookHelper.define_method(method_name) do |value|
      log(value, attribute.to_sym, send(attribute), __method__, pry: @debugger_instance&.options)
    end

    klass.define_singleton_method("tracked") { |key, *opts|
      options = {}
      opts.each {|o| options[o] = true}

      if opts.empty?
        ap @debugger_instance.cache[key]
      else
        ap @debugger_instance.cache[key].filter do |e|
          options.key? e
        end
      end
    }
  end
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



5
6
7
# File 'lib/potato_debugger/hooks.rb', line 5

def options
  @options
end