class Scribble::Support::Unmatched

Attributes

base_matches[R]

Merge unmatched exceptions to generate merged error

expected[R]

Merge unmatched exceptions to generate merged error

max_cursor[R]

Merge unmatched exceptions to generate merged error

unexpected[R]

Merge unmatched exceptions to generate merged error

Public Class Methods

new(base_matches, expected, unexpected, max_cursor) click to toggle source
# File lib/scribble/support/unmatched.rb, line 4
def initialize base_matches, expected, unexpected, max_cursor
  @base_matches, @expected, @unexpected, @max_cursor =
    base_matches, expected, unexpected, max_cursor
end

Public Instance Methods

arg_to(call) click to toggle source
# File lib/scribble/support/unmatched.rb, line 19
def arg_to call
  "#{Support::Utilities.ordinalize @max_cursor + 1} argument to '#{call.name}'"
end
expected_sentence() click to toggle source
# File lib/scribble/support/unmatched.rb, line 23
def expected_sentence
  "Expected #{Support::Utilities.to_sentence @expected}"
end
human_arities() click to toggle source

Error message helpers

# File lib/scribble/support/unmatched.rb, line 11
def human_arities
  @base_matches.map do |method|
    [method.min_arity, method.max_arity]
  end.uniq.map do |min, max|
    max ? (min == max ? min.to_s : "#{min}-#{max}") : "#{min}+"
  end
end
merge(unmatched) click to toggle source
# File lib/scribble/support/unmatched.rb, line 54
def merge unmatched
  @base_matches += unmatched.base_matches

  unless @max_cursor.nil?
    if unmatched.max_cursor > @max_cursor
      @max_cursor = unmatched.max_cursor
      @expected   = unmatched.expected
      @unexpected = unmatched.unexpected
    elsif unmatched.max_cursor == @max_cursor
      @expected   += unmatched.expected
    end
  end
  self
end
to_error(call) click to toggle source

To error

# File lib/scribble/support/unmatched.rb, line 33
def to_error call
  if @base_matches.empty?
    Errors::Undefined.new "Undefined #{'variable or ' if call.allow_variable}"\
      "method '#{call.name}' #{call.line_and_column}"
  elsif @expected.empty? && @unexpected.nil?
    Errors::Arity.new "Wrong number of arguments (#{call.args.size} "\
      "for #{Support::Utilities.to_sentence human_arities}) "\
      "for '#{call.name}' #{call.line_and_column}"
  elsif @expected.empty?
    Errors::Argument.new "Unexpected #{arg_to call}, #{unexpected_sentence call}"
  elsif @unexpected.nil?
    Errors::Argument.new "#{expected_sentence} as #{arg_to call} #{call.line_and_column}"
  else
    Errors::Argument.new "#{expected_sentence} as #{arg_to call}, #{unexpected_sentence call}"
  end
end
unexpected_sentence(call) click to toggle source
# File lib/scribble/support/unmatched.rb, line 27
def unexpected_sentence call
  "got #{@unexpected} #{call.args[@max_cursor].line_and_column}"
end