Class: SpecForge::Error::MissingVariableError

Inherits:
Error
  • Object
show all
Defined in:
lib/spec_forge/error.rb

Overview

Raised when a referenced variable is not defined in the current context

Provides helpful suggestions for similar variable names using spell checking.

Instance Method Summary collapse

Constructor Details

#initialize(variable_name, available_variables: []) ⇒ MissingVariableError

Returns a new instance of MissingVariableError.



190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/spec_forge/error.rb', line 190

def initialize(variable_name, available_variables: [])
  message = "Undefined variable \"#{variable_name}\""

  checker = DidYouMean::SpellChecker.new(dictionary: available_variables)
  suggestions = checker.correct(variable_name.to_s)

  message += ". #{DidYouMean::Formatter.message_for(suggestions)}" if suggestions.size > 0

  if available_variables.size > 0 && available_variables.size <= 5
    message += ".\nAvailable: #{available_variables.join_map(", ", &:in_quotes)}"
  end

  super(message)
end