Class: Roast::Workflow::ContextPathResolver

Inherits:
Object
  • Object
show all
Defined in:
lib/roast/workflow/context_path_resolver.rb

Overview

Determines the context path for workflow and step classes

Class Method Summary collapse

Class Method Details

.resolve(klass) ⇒ String

Determine the directory where the actual class is defined

Parameters:

  • klass (Class)

    The class to find the context path for

Returns:

  • (String)

    The directory path containing the class definition



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/roast/workflow/context_path_resolver.rb', line 11

def resolve(klass)
  # Try to get the file path where the class is defined
  path = if klass.name&.include?("::")
    # For namespaced classes like Roast::Workflow::Grading::Workflow
    # Convert the class name to a relative path
    class_path = klass.name.underscore + ".rb"
    # Look through load path to find the actual file
    $LOAD_PATH.map { |p| File.join(p, class_path) }.find { |f| File.exist?(f) }
  end

  # Fall back to trying to get the source location
  if path.nil? && klass.instance_methods(false).any?
    # Try to get source location from any instance method
    method = klass.instance_methods(false).first
    source_location = klass.instance_method(method).source_location
    path = source_location&.first
  end

  # Return directory containing the class definition
  # or the current directory if we can't find it
  File.dirname(path || Dir.pwd)
end

.resolve_for_instance(instance) ⇒ String

Resolve context path for an instance

Parameters:

  • instance (Object)

    The instance to find the context path for

Returns:

  • (String)

    The directory path containing the class definition



37
38
39
# File 'lib/roast/workflow/context_path_resolver.rb', line 37

def resolve_for_instance(instance)
  resolve(instance.class)
end