Exception: Scribble::Support::Unmatched
- Inherits:
-
Exception
- Object
- Exception
- Scribble::Support::Unmatched
- Defined in:
- lib/scribble/support/unmatched.rb
Instance Attribute Summary collapse
-
#base_matches ⇒ Object
readonly
Merge unmatched exceptions to generate merged error.
-
#expected ⇒ Object
readonly
Merge unmatched exceptions to generate merged error.
-
#max_cursor ⇒ Object
readonly
Merge unmatched exceptions to generate merged error.
-
#unexpected ⇒ Object
readonly
Merge unmatched exceptions to generate merged error.
Instance Method Summary collapse
- #arg_to(call) ⇒ Object
- #expected_sentence ⇒ Object
-
#human_arities ⇒ Object
Error message helpers.
-
#initialize(base_matches, expected, unexpected, max_cursor) ⇒ Unmatched
constructor
A new instance of Unmatched.
- #merge(unmatched) ⇒ Object
-
#to_error(call) ⇒ Object
To error.
- #unexpected_sentence(call) ⇒ Object
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_matches ⇒ Object (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 |
#expected ⇒ Object (readonly)
Merge unmatched exceptions to generate merged error
52 53 54 |
# File 'lib/scribble/support/unmatched.rb', line 52 def expected @expected end |
#max_cursor ⇒ Object (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 |
#unexpected ⇒ Object (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_sentence ⇒ Object
23 24 25 |
# File 'lib/scribble/support/unmatched.rb', line 23 def expected_sentence "Expected #{Support::Utilities.to_sentence @expected}" end |
#human_arities ⇒ Object
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 |