Class: RuboCop::RSpec::Hook

Inherits:
Concept
  • Object
show all
Defined in:
lib/rubocop/rspec/hook.rb

Overview

Wrapper for RSpec hook

Instance Method Summary collapse

Methods inherited from Concept

#eql?, #hash, #initialize, #to_node

Methods included from Language::NodePattern

#block_or_numblock_pattern, #block_pattern, #numblock_pattern, #send_pattern

Methods included from Language

#example_group?, #example_group_with_body?, #explicit_rspec?, #hook?, #include?, #let?, #rspec?, #shared_group?, #spec_group?, #subject?

Constructor Details

This class inherits a constructor from RuboCop::RSpec::Concept

Instance Method Details

#example?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/rubocop/rspec/hook.rb', line 24

def example?
  scope.equal?(:each)
end

#extract_metadata(node) ⇒ Object



8
9
10
11
12
# File 'lib/rubocop/rspec/hook.rb', line 8

def_node_matcher :extract_metadata, <<~PATTERN
  (block
    (send _ _ #valid_scope? ? $...) ...
  )
PATTERN

#knowable_scope?Boolean

Returns:

  • (Boolean)


18
19
20
21
22
# File 'lib/rubocop/rspec/hook.rb', line 18

def knowable_scope?
  scope_argument.nil? ||
    scope_argument.sym_type? ||
    scope_argument.hash_type?
end

#metadataObject



38
39
40
41
42
43
# File 'lib/rubocop/rspec/hook.rb', line 38

def 
  ((node) || [])
    .map { |meta| (meta) }
    .flatten
    .inject(&:merge)
end

#nameObject



14
15
16
# File 'lib/rubocop/rspec/hook.rb', line 14

def name
  node.method_name
end

#scopeObject



28
29
30
31
32
33
34
35
36
# File 'lib/rubocop/rspec/hook.rb', line 28

def scope
  return :each if scope_argument&.hash_type?

  case scope_name
  when nil, :each, :example then :each
  when :context, :all       then :context
  when :suite               then :suite
  end
end