Class: Ivar::Policy

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

Overview

Base class for all ivar checking policies

Instance Method Summary collapse

Instance Method Details

#find_closest_match(ivar, known_ivars) ⇒ Symbol?

Find the closest match for a variable name

Parameters:

  • ivar (Symbol)

    The variable to find a match for

  • known_ivars (Array<Symbol>)

    List of known variables

Returns:

  • (Symbol, nil)

    The closest match or nil if none found



20
21
22
23
24
# File 'lib/ivar/policies.rb', line 20

def find_closest_match(ivar, known_ivars)
  finder = DidYouMean::SpellChecker.new(dictionary: known_ivars)
  suggestions = finder.correct(ivar.to_s)
  suggestions.first&.to_sym if suggestions.any?
end

#format_warning(ref, suggestion) ⇒ String

Format a warning message for an unknown instance variable

Parameters:

  • ref (Hash)

    Reference to an unknown instance variable

  • suggestion (Symbol, nil)

    Suggested correction or nil

Returns:

  • (String)

    Formatted warning message



30
31
32
33
34
# File 'lib/ivar/policies.rb', line 30

def format_warning(ref, suggestion)
  ivar = ref[:name]
  suggestion_text = suggestion ? "Did you mean: #{suggestion}?" : ""
  "#{ref[:path]}:#{ref[:line]}: warning: unknown instance variable #{ivar}. #{suggestion_text}\n"
end

#handle_unknown_ivars(unknown_refs, klass, allowed_ivars) ⇒ Object

Handle unknown instance variables

Parameters:

  • unknown_refs (Array<Hash>)

    References to unknown instance variables

  • klass (Class)

    The class being checked

  • allowed_ivars (Array<Symbol>)

    List of allowed instance variables

Raises:

  • (NotImplementedError)


12
13
14
# File 'lib/ivar/policies.rb', line 12

def handle_unknown_ivars(unknown_refs, klass, allowed_ivars)
  raise NotImplementedError, "Subclasses must implement handle_unknown_ivars"
end