Exception: Scribble::Support::Unmatched

Inherits:
Exception
  • Object
show all
Defined in:
lib/scribble/support/unmatched.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_matches, expected, unexpected, max_cursor) ⇒ Unmatched

Returns a new instance of Unmatched.



4
5
6
7
# 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

Instance Attribute Details

#base_matchesObject (readonly)

Merge unmatched exceptions to generate merged error



52
53
54
# File 'lib/scribble/support/unmatched.rb', line 52

def base_matches
  @base_matches
end

#expectedObject (readonly)

Merge unmatched exceptions to generate merged error



52
53
54
# File 'lib/scribble/support/unmatched.rb', line 52

def expected
  @expected
end

#max_cursorObject (readonly)

Merge unmatched exceptions to generate merged error



52
53
54
# File 'lib/scribble/support/unmatched.rb', line 52

def max_cursor
  @max_cursor
end

#unexpectedObject (readonly)

Merge unmatched exceptions to generate merged error



52
53
54
# File 'lib/scribble/support/unmatched.rb', line 52

def unexpected
  @unexpected
end

Instance Method Details

#arg_to(call) ⇒ Object



19
20
21
# File 'lib/scribble/support/unmatched.rb', line 19

def arg_to call
  "#{Support::Utilities.ordinalize @max_cursor + 1} argument to '#{call.name}'"
end

#expected_sentenceObject



23
24
25
# File 'lib/scribble/support/unmatched.rb', line 23

def expected_sentence
  "Expected #{Support::Utilities.to_sentence @expected}"
end

#human_aritiesObject

Error message helpers



11
12
13
14
15
16
17
# 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) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
# 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) ⇒ Object

To error



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# 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) ⇒ Object



27
28
29
# File 'lib/scribble/support/unmatched.rb', line 27

def unexpected_sentence call
  "got #{@unexpected} #{call.args[@max_cursor].line_and_column}"
end