Class: Ivar::RaisePolicy

Inherits:
Policy
  • Object
show all
Defined in:
lib/ivar/policies.rb

Overview

Policy that raises an exception for unknown instance variables

Instance Method Summary collapse

Methods inherited from Policy

#find_closest_match, #format_warning

Instance Method Details

#handle_unknown_ivars(unknown_refs, _klass, allowed_ivars) ⇒ Object

Handle unknown instance variables by raising an exception

Raises:

  • (NameError)


80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/ivar/policies.rb', line 80

def handle_unknown_ivars(unknown_refs, _klass, allowed_ivars)
  return if unknown_refs.empty?

  # Get the first unknown reference
  ref = unknown_refs.first
  ivar = ref[:name]
  suggestion = find_closest_match(ivar, allowed_ivars)
  suggestion_text = suggestion ? " Did you mean: #{suggestion}?" : ""

  # Raise an exception with location information
  message = "#{ref[:path]}:#{ref[:line]}: unknown instance variable #{ivar}.#{suggestion_text}"
  raise NameError, message
end