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:



15
16
17
18
19
20
21
22
# File 'lib/delorean/ruby/whitelists/matchers/method.rb', line 15

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

  yield self if block_given?
end

Instance Attribute Details

#arguments_matchersObject (readonly)

Returns the value of attribute arguments_matchers.



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

def arguments_matchers
  @arguments_matchers
end

#arguments_matchers_hashObject (readonly)

Returns the value of attribute arguments_matchers_hash.



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

def arguments_matchers_hash
  @arguments_matchers_hash
end

#match_toObject (readonly)

Returns the value of attribute match_to.



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

def match_to
  @match_to
end

#method_nameObject (readonly)

Returns the value of attribute method_name.



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

def method_name
  @method_name
end

Instance Method Details

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



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/delorean/ruby/whitelists/matchers/method.rb', line 24

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

  arguments_matchers_hash[klass] = matcher

  arguments_matchers << matcher

  # Sort matchers by reversed ancestors chain length, so
  # matcher method would find the closest ancestor in hierarchy
  arguments_matchers.sort_by! do |obj|
    -obj.called_on.ancestors.size
  end
end

#extend_matcher {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



63
64
65
# File 'lib/delorean/ruby/whitelists/matchers/method.rb', line 63

def extend_matcher
  yield self if block_given?
end

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



55
56
57
# File 'lib/delorean/ruby/whitelists/matchers/method.rb', line 55

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

#match_to?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/delorean/ruby/whitelists/matchers/method.rb', line 59

def match_to?
  !match_to.nil?
end

#matcher(klass:) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/delorean/ruby/whitelists/matchers/method.rb', line 40

def matcher(klass:)
  # Optimization hack: Look for exact class matcher in hash.
  # If it's not found, search for ancestor classes matchers in array
  matcher = @arguments_matchers_hash[klass]
  return matcher unless matcher.nil?

  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