Module: Context Private

Overview

This module is part of a private API. This module may only be used in the Homebrew/brew repository. Third parties should avoid using this module if possible, as it may be removed or changed without warning.

Module for querying the current execution context.

Defined Under Namespace

Classes: ContextStruct

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.currentObject

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.



16
17
18
19
20
21
22
23
24
# File 'context.rb', line 16

def self.current
  if (current_context = Thread.current[:context])
    return current_context
  end

  synchronize do
    @current ||= ContextStruct.new
  end
end

.current=(context) ⇒ Object

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.



10
11
12
13
14
# File 'context.rb', line 10

def self.current=(context)
  synchronize do
    @current = context
  end
end

Instance Method Details

#debug?Boolean

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:

  • (Boolean)


51
52
53
# File 'context.rb', line 51

def debug?
  Context.current.debug?
end

#quiet?Boolean

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:

  • (Boolean)


56
57
58
# File 'context.rb', line 56

def quiet?
  Context.current.quiet?
end

#verbose?Boolean

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:

  • (Boolean)


61
62
63
# File 'context.rb', line 61

def verbose?
  Context.current.verbose?
end

#with_context(**options) ⇒ Object

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'context.rb', line 65

def with_context(**options)
  old_context = Context.current

  new_context = ContextStruct.new(
    debug:   options.fetch(:debug, old_context&.debug?),
    quiet:   options.fetch(:quiet, old_context&.quiet?),
    verbose: options.fetch(:verbose, old_context&.verbose?),
  )

  Thread.current[:context] = new_context

  begin
    yield
  ensure
    Thread.current[:context] = old_context
  end
end