class Scribble::Support::Matcher
Public Class Methods
new(registry, name, receiver, args)
click to toggle source
# File lib/scribble/support/matcher.rb, line 4 def initialize registry, name, receiver, args @registry, @name, @receiver, @args = registry, name, receiver, args end
Public Instance Methods
base_matches()
click to toggle source
Name and receiver matching
# 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)
click to toggle source
Argument expectations
# 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()
click to toggle source
Get first match
# 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)
click to toggle source
Match a single method
# 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)
click to toggle source
Args cursor helpers
# 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)
click to toggle source
# 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()
click to toggle source
# 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