Class: PDK::Context::AbstractContext Abstract

Inherits:
Object
  • Object
show all
Defined in:
lib/pdk/context.rb

Overview

This class is abstract.

Abstract class which all PDK Contexts will subclass from.

Direct Known Subclasses

ControlRepo, Module, None

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context_path) ⇒ AbstractContext

Returns a new instance of AbstractContext.

Parameters:

  • context_path (String)

    The path where this context was created from e.g. Dir.pwd



51
52
53
54
# File 'lib/pdk/context.rb', line 51

def initialize(context_path)
  @context_path = context_path
  @root_path = nil
end

Instance Attribute Details

#context_pathString (readonly)

The path used to create this context, for example the current working directory. This can be different from root_path For example a Module context_path could be /path/to/module/manifests/ but the root_path will be /path/to/module as that is the root of the Module context

Returns:

  • (String)


48
49
50
# File 'lib/pdk/context.rb', line 48

def context_path
  @context_path
end

Instance Method Details

#display_nameObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method is abstract.

The friendly name to display for this context



65
# File 'lib/pdk/context.rb', line 65

def display_name; end

#parent_contextPDK::Context::AbstractContext, Nil

The context which this context is in. For example a Module Context (/controlrepo/site/profile) can be inside of a Control Repo context (/controlrepo) The default is to search in the parent directory of this context

Returns:



70
71
72
73
# File 'lib/pdk/context.rb', line 70

def parent_context
  # Default detection is just look for the context in the parent directory of this context
  @parent_context || PDK::Context.create(File.dirname(root_path))
end

#pdk_compatible?Boolean

Whether the current context is compatible with the PDK e.g. in a Module context, whether it has the correct metadata.json content

Returns:

  • (Boolean)

    Default is not compatible



58
59
60
# File 'lib/pdk/context.rb', line 58

def pdk_compatible?
  false
end

#root_pathString

The root of this context, for example the module root when inside a module. This can be different from context_path For example a Module context_path could be /path/to/module/manifests/ but the root_path will be /path/to/module as that is the root of the Module context. Defaults to the context_path if not set.

Returns:

  • (String)


40
41
42
# File 'lib/pdk/context.rb', line 40

def root_path
  @root_path || @context_path
end

#to_debug_logObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Writes the current context information, and parent contexts, to the PDK Debug Logger. This is mainly used by the PDK CLI when in debug mode to assist users to figure out why the PDK is misbehaving.



78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/pdk/context.rb', line 78

def to_debug_log
  current = self
  depth = 1
  loop do
    PDK.logger.debug("Detected #{current.display_name} at #{current.root_path.nil? ? current.context_path : current.root_path}")
    current = current.parent_context
    break if current.nil?
    depth += 1
    # Circuit breaker in case there are circular references
    break if depth > 20
  end
  nil
end

#to_sObject

:nocov: There’s nothing to test here



93
94
95
# File 'lib/pdk/context.rb', line 93

def to_s
  "#<#{self.class}:#{object_id}>#{context_path}"
end