Class: ParameterExpansion

Inherits:
Object show all
Defined in:
lib/parameter_expansion.rb

Overview

Parameter expansion utility for markdown_exec Handles different invocation types for parameter substitution

Defined Under Namespace

Classes: NewVariable

Class Method Summary collapse

Class Method Details

.expand_parameter(param, invocation, value, unique: nil) ⇒ Array

REQ-001: Expands a parameter based on invocation type and value ARCH-003: Template method pattern - delegates to specific handlers

Invocation codes:

  • First letter: Input type (C=command, E=expression, L=literal, V=variable)

  • Second letter: Output type (C=command, E=expression, L=literal, V=variable) If second letter is missing, defaults to Q (literal)

Examples:

expand_parameter("PARAM", "cc", "ls -la") 
# => ["ls -la", nil] (command as command)

expand_parameter("PARAM", "ce", "ls -la")
# => ["${PARAM}", NewVariable object] (command as expression)

expand_parameter("PARAM", "ll", "hello world")
# => ["hello world", nil] (literal as literal)

expand_parameter("PARAM", "c", "ls -la")
# => ["ls -la", nil] (command as literal, defaults to Q)

expand_parameter("PARAM", "e", "hello world")
# => ["hello world", nil] (expression as literal, defaults to Q)

Parameters:

  • param (String)

    The parameter name to be substituted

  • invocation (String)

    The 1 or 2-letter invocation code (e.g., “c”, “cc”, “ce”, “cl”, etc.)

  • value (String)

    The value to insert into the expansion

Returns:

  • (Array)

    Array containing [expansion_string, new_variable_details]



351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
# File 'lib/parameter_expansion.rb', line 351

def self.expand_parameter(param, invocation, value, unique: nil)
  return [value, nil] if invocation.nil? || invocation.empty?

  # unique = rand(1e4) if unique.nil?
  if unique.nil?
    # unique from a global counter
    @@unique ||= 0
    @@unique += 1
    unique = @@unique
  end
  
  input_type = invocation[0]&.upcase
  output_type = invocation[1]&.upcase || 'Q'  # Default to Q if second character missing

ww 'input_type:', input_type
ww 'output_type:', output_type
  # ARCH-001: Strategy pattern for input type handling
  case input_type
  when 'C' # IMPL-001: Command substitution
    expand_command_input(param, output_type, value, invocation, unique: unique)
  when 'E' # IMPL-002: Evaluated expression
    expand_expression_input(param, output_type, value, invocation, unique: unique)
  when 'L', 'Q' # REQ-004: Literal (accept both L and Q for backward compatibility)
    expand_literal_input(param, output_type, value, invocation, unique: unique)
  when 'V' # IMPL-004: Variable reference
    expand_variable_input(param, output_type, value, invocation)
  else
    # Default to raw literal if no valid input type
    [value, nil]
  end
end

.expand_parameter_string(param, invocation, value, unique: rand(1e4)) ⇒ Object

Convenience method that returns just the expansion string (backward compatibility)



384
385
386
387
# File 'lib/parameter_expansion.rb', line 384

def self.expand_parameter_string(param, invocation, value, unique: rand(1e4))
  result, _new_var = expand_parameter(param, invocation, value, unique: unique)
  result
end

.expand_parameters(parameter_hash, unique: rand(1e4)) ⇒ Object

Process multiple parameters and collect all new variables



390
391
392
393
394
395
396
397
398
399
400
401
# File 'lib/parameter_expansion.rb', line 390

def self.expand_parameters(parameter_hash, unique: rand(1e4))
  expansions = {}
  new_variables = []
  
  parameter_hash.each do |param, (invocation, value)|
    expansion, new_var = expand_parameter(param, invocation, value, unique: unique)
    expansions[param] = expansion
    new_variables << new_var if new_var
  end
  
  [expansions, new_variables]
end