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



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

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)


46
47
48
# File 'lib/pdk/context.rb', line 46

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



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

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:



68
69
70
71
# File 'lib/pdk/context.rb', line 68

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



56
57
58
# File 'lib/pdk/context.rb', line 56

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)


38
39
40
# File 'lib/pdk/context.rb', line 38

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.



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

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



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

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