Class: Delorean::Ruby::Whitelists::Matchers::Arguments

Inherits:
Object
  • Object
show all
Defined in:
lib/delorean/ruby/whitelists/matchers/arguments.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(called_on:, method_name:, with: []) ⇒ Arguments



8
9
10
11
12
# File 'lib/delorean/ruby/whitelists/matchers/arguments.rb', line 8

def initialize(called_on:, method_name:, with: [])
  @called_on = called_on
  @method_name = method_name
  @with = with
end

Instance Attribute Details

#called_onObject (readonly)

Returns the value of attribute called_on.



6
7
8
# File 'lib/delorean/ruby/whitelists/matchers/arguments.rb', line 6

def called_on
  @called_on
end

#method_nameObject (readonly)

Returns the value of attribute method_name.



6
7
8
# File 'lib/delorean/ruby/whitelists/matchers/arguments.rb', line 6

def method_name
  @method_name
end

#withObject (readonly)

Returns the value of attribute with.



6
7
8
# File 'lib/delorean/ruby/whitelists/matchers/arguments.rb', line 6

def with
  @with
end

Instance Method Details

#match!(args:) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/delorean/ruby/whitelists/matchers/arguments.rb', line 14

def match!(args:)
  raise "too many args to #{method_name}" if args.size > with.size

  with.each_with_index do |s, i|
    arg_signature = Array(s)

    arg = args[i]

    # Sometimes signature contains extra elements that can be nil.
    # In that case we allow it to not be passed.
    # For example .first and .first(4)
    next if arg_signature.member?(nil) && i >= args.size

    next if valid_argument?(arg_signature: arg_signature, arg: arg)

    bad_arg_error(arg_signature: arg_signature, index: i, arg: arg)
  end
end