Class: Ivar::CheckAllManager

Inherits:
Object
  • Object
show all
Defined in:
lib/ivar/check_all_manager.rb

Overview

Manages automatic inclusion of Ivar::Checked in classes and modules

Instance Method Summary collapse

Constructor Details

#initializeCheckAllManager

Returns a new instance of CheckAllManager.



8
9
10
11
# File 'lib/ivar/check_all_manager.rb', line 8

def initialize
  @trace_point = nil
  @mutex = Mutex.new
end

Instance Method Details

#disablevoid

This method returns an undefined value.

Disables automatic inclusion of Ivar::Checked in classes and modules.



51
52
53
54
55
56
57
58
# File 'lib/ivar/check_all_manager.rb', line 51

def disable
  @mutex.synchronize do
    if @trace_point
      @trace_point.disable
      @trace_point = nil
    end
  end
end

#enable(project_root, &block) ⇒ void

This method returns an undefined value.

Enables automatic inclusion of Ivar::Checked in all classes and modules defined within the project root.

Parameters:

  • project_root (String)

    The project root directory path

  • block (Proc)

    Optional block. If provided, auto-checking is only active for the duration of the block. Otherwise, it remains active indefinitely.



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
46
47
# File 'lib/ivar/check_all_manager.rb', line 20

def enable(project_root, &block)
  disable if @trace_point
  root_pathname = Pathname.new(project_root)
  @mutex.synchronize do
    # :end means "end of module or class definition" in TracePoint
    @trace_point = TracePoint.new(:end) do |tp|
      next unless tp.path
      file_path = Pathname.new(File.expand_path(tp.path))
      if file_path.to_s.start_with?(root_pathname.to_s)
        klass = tp.self
        next if klass.included_modules.include?(Ivar::Checked)
        klass.include(Ivar::Checked)
      end
    end

    @trace_point.enable
  end

  if block
    begin
      yield
    ensure
      disable
    end
  end

  nil
end

#enabled?Boolean

Returns whether check_all is currently enabled

Returns:

  • (Boolean)

    true if check_all is enabled, false otherwise



62
63
64
# File 'lib/ivar/check_all_manager.rb', line 62

def enabled?
  @mutex.synchronize { !@trace_point.nil? && @trace_point.enabled? }
end

#trace_pointTracePoint?

Returns the current trace point (mainly for testing)

Returns:

  • (TracePoint, nil)

    The current trace point or nil if not enabled



68
69
70
# File 'lib/ivar/check_all_manager.rb', line 68

def trace_point
  @mutex.synchronize { @trace_point }
end