Module: Tina4::ImportHelper::ConstMissing

Defined in:
lib/tina4/import_helper.rb

Overview

Hook 1 -- Tina4::. Only fires when a bare Tina4::<Name> constant is asked for and does not exist. Ruby dispatches const_missing on the module itself (via its singleton class), so a prepended ConstMissing#const_missing runs first and re-raises with a helpful hint.

Instance Method Summary collapse

Instance Method Details

#const_missing(name) ⇒ Object

Raises:

  • (NameError)


122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/tina4/import_helper.rb', line 122

def const_missing(name)
  real = Tina4.constants.map(&:to_s)
  matches = Tina4::ImportHelper.close_matches(name.to_s, real)

  message =
    if matches.any?
      "uninitialized constant Tina4::#{name}. Did you mean Tina4::#{matches.first}?"
    else
      examples = Tina4::ImportHelper.some_tina4_constants
      hint =
        if examples.any?
          " Real Tina4 constants include: #{examples.map { |c| "Tina4::#{c}" }.join(', ')}."
        else
          ""
        end
      "uninitialized constant Tina4::#{name}.#{hint}"
    end

  # NameError.new(msg, name) records the missing constant symbol, so
  # code doing `rescue NameError => e; e.name` still works.
  raise NameError.new(message, name)
end