Class: SpecForge::Attribute::Variable

Inherits:
Attribute
  • Object
show all
Includes:
Chainable
Defined in:
lib/spec_forge/attribute/variable.rb

Overview

Represents a variable reference in a template

Variables are resolved at runtime by looking up values stored in the current execution context. Supports dot notation for accessing nested properties (e.g., "response.body.id").

Examples:

Simple variable

Variable.new("user_id")  # Looks up :user_id in context

Nested access

Variable.new("response.body.token")  # response[:body][:token]

Constant Summary

Constants included from Chainable

Chainable::NUMBER_REGEX

Instance Attribute Summary

Attributes included from Chainable

#header, #invocation_chain, #keyword

Instance Method Summary collapse

Methods included from Chainable

#resolved, #value

Constructor Details

#initializeVariable

Creates a new variable attribute

Parameters:

  • input (String)

    The variable path (e.g., "user_id", "response.body.id")



28
29
30
31
32
33
34
35
36
37
# File 'lib/spec_forge/attribute/variable.rb', line 28

def initialize(...)
  super

  # Unset the keyword to keep chainable from displaying it in error messages
  @keyword = nil

  sections = @input.split(".")
  @header = sections.first&.to_sym
  @invocation_chain = sections[1..] || []
end

Instance Method Details

#base_objectObject

Returns the base object for this variable from the current context

Looks up the variable name in the current Forge context's variables. Raises an error if the variable is not defined.

Returns:

  • (Object)

    The value stored under this variable name

Raises:



49
50
51
52
53
54
55
56
57
# File 'lib/spec_forge/attribute/variable.rb', line 49

def base_object
  variables = @options[:context] || Forge.context&.variables || {}

  if !variables.key?(variable_name)
    raise Error::MissingVariableError.new(variable_name, available_variables: variables.keys)
  end

  variables[variable_name]
end