Class: Delorean::Ruby::Whitelists::Matchers::Method

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(method_name:, match_to: nil) {|_self| ... } ⇒ Method

Returns a new instance of Method.

Yields:

  • (_self)

Yield Parameters:



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

def initialize(method_name:, match_to: nil)
  @method_name = method_name
  @match_to = match_to
  @arguments_matchers = []

  yield self if block_given?
end

Instance Attribute Details

#arguments_matchersObject (readonly)

Returns the value of attribute arguments_matchers.



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

def arguments_matchers
  @arguments_matchers
end

#match_toObject (readonly)

Returns the value of attribute match_to.



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

def match_to
  @match_to
end

#method_nameObject (readonly)

Returns the value of attribute method_name.



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

def method_name
  @method_name
end

Instance Method Details

#called_on(klass, with: []) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/delorean/ruby/whitelists/matchers/method.rb', line 18

def called_on(klass, with: [])
  matcher = Ruby::Whitelists::Matchers::Arguments.new(
    called_on: klass, method_name: method_name, with: with
  )

  arguments_matchers << matcher
end

#match!(klass:, args:) ⇒ Object



35
36
37
# File 'lib/delorean/ruby/whitelists/matchers/method.rb', line 35

def match!(klass:, args:)
  matcher(klass: klass).match!(args: args)
end

#match_to?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/delorean/ruby/whitelists/matchers/method.rb', line 39

def match_to?
  !match_to.nil?
end

#matcher(klass:) ⇒ Object



26
27
28
29
30
31
32
33
# File 'lib/delorean/ruby/whitelists/matchers/method.rb', line 26

def matcher(klass:)
  matcher = arguments_matchers.find do
    |matcher_object| klass <= matcher_object.called_on
  end

  raise "no such method #{method_name} for #{klass}" if matcher.nil?
  matcher
end