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

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

Constant Summary collapse

ANYTHING =
:_delorean_anything

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Arguments.



12
13
14
15
16
# File 'lib/delorean/ruby/whitelists/matchers/arguments.rb', line 12

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.



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

def called_on
  @called_on
end

#method_nameObject (readonly)

Returns the value of attribute method_name.



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

def method_name
  @method_name
end

#withObject (readonly)

Returns the value of attribute with.



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

def with
  @with
end

Instance Method Details

#match!(args:) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/delorean/ruby/whitelists/matchers/arguments.rb', line 18

def match!(args:)
  return if with == ANYTHING

  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