Class: Scribble::Support::Matcher
- Inherits:
-
Object
- Object
- Scribble::Support::Matcher
- Defined in:
- lib/scribble/support/matcher.rb
Instance Method Summary collapse
-
#base_matches ⇒ Object
Name and receiver matching.
-
#expect(arg_class) ⇒ Object
Argument expectations.
-
#initialize(registry, name, receiver, args) ⇒ Matcher
constructor
A new instance of Matcher.
-
#match ⇒ Object
Get first match.
-
#match_args(method) ⇒ Object
Match a single method.
-
#step_arg(arg_class) ⇒ Object
Args cursor helpers.
- #step_args(arg_class, max = nil) ⇒ Object
- #unexpected ⇒ Object
Constructor Details
#initialize(registry, name, receiver, args) ⇒ Matcher
Returns a new instance of Matcher.
4 5 6 |
# File 'lib/scribble/support/matcher.rb', line 4 def initialize registry, name, receiver, args @registry, @name, @receiver, @args = registry, name, receiver, args end |
Instance Method Details
#base_matches ⇒ Object
Name and receiver matching
10 11 12 13 14 |
# File 'lib/scribble/support/matcher.rb', line 10 def base_matches @base_matches ||= @registry.methods.select do |method| method.method_name == @name && @receiver.is_a?(method.receiver_class) end end |
#expect(arg_class) ⇒ Object
Argument expectations
31 32 33 34 35 36 37 38 |
# File 'lib/scribble/support/matcher.rb', line 31 def expect arg_class if @max_cursor.nil? || @cursor > @max_cursor @expected, @max_cursor = [], @cursor end if arg_class && @cursor == @max_cursor @expected << @registry.class_name(arg_class) end end |
#match ⇒ Object
Get first match
65 66 67 68 69 70 71 |
# File 'lib/scribble/support/matcher.rb', line 65 def match @match ||= base_matches.select do |method| @args.size >= method.min_arity && (method.max_arity.nil? || @args.size <= method.max_arity) end.select do |method| match_args method end.first || raise(Unmatched.new @base_matches, @expected || [], unexpected, @max_cursor) end |
#match_args(method) ⇒ Object
Match a single method
48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/scribble/support/matcher.rb', line 48 def match_args method @cursor = 0 method.signature.each do |element| if element.is_a? Array step_args *element elsif !step_arg element expect element; return false end end if @cursor < @args.size expect nil; return false end method end |
#step_arg(arg_class) ⇒ Object
Args cursor helpers
18 19 20 |
# File 'lib/scribble/support/matcher.rb', line 18 def step_arg arg_class @cursor += 1 if @cursor < @args.size && @args[@cursor].is_a?(arg_class) end |
#step_args(arg_class, max = nil) ⇒ Object
22 23 24 25 26 27 |
# File 'lib/scribble/support/matcher.rb', line 22 def step_args arg_class, max = nil index = 0; while index += 1 break unless step_arg arg_class break if max && index == max end end |
#unexpected ⇒ Object
40 41 42 43 44 |
# File 'lib/scribble/support/matcher.rb', line 40 def unexpected if @max_cursor && @max_cursor < @args.size @registry.class_name @args[@max_cursor].class end end |